I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output.
I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
Some may find this useful. Integer values in variable substitution, where the trick is using
$(())
double brackets:Just to be different:
You need to use either
$(command-here)
or
example
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a special way, see at very bottom of this!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
Nested sample:
reading more than one variable (with bashisms)
If I just want Used value:
you could see array variable:
Then:
But I prefer this:
1st
read foo
will just skip header line (variable$foo
will contain something likeFilesystem 1K-blocks Used Available Use% Mounted on
)Sample function for populating some variables:
Nota:
declare
line is not required, just for readability.About
sudo cmd | grep ... | cut ...
(Please avoid useless
cat
! So this is just 1 fork less:All pipes (
|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.So using
sed
for sample, will limit subprocess to only one fork:And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
or
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using background long-running tasks
2nd Edit 2018-02-12: In order to prevent multiple forks like
or
And because
date
andbc
could work line by line:We could use long running background process to make jobs repetitively, without having to initiate new fork for each request:
(of course, FD
5
and6
have to be unused!)... From there, you could use this process by:Into a function
newConnector
You may found my
newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file wich could be sourced for use or just run for demo)Sample:
The function
myBc
let you use the background task with simple syntax, and for date:From there, if you want to end one of background process, you just have to close his fd:
wich is not needed, because all fd close when main process finish.
As they have already indicated to you, you should use 'backticks'.
The alternative proposed
$(command)
works as well, and it also easier to read, but note that it is valid only with bash or korn shells (and shells derived from those), so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.I know three ways to do:
1) Functions are suitable for such tasks:
Invoke it by saying
func
2) Also another suitable solution could be eval:
3) The third one is using variables directly:
you can get output of third solution in good way:
and also in nasty way: