Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing SSH output as variable in bash script

Tags:

I've been struggling with this problem when writing a bash script. Basically, I want to measure the time of a program on a remote server, so I use the command: /usr/bin/time -f %e sh -c "my command > /dev/null 2>&1" to execute the program. However, it appears that I cannot capture the output of my command (SSH) to a variable at all. In fact, the result (time) keeps getting printed out to stdout.

The full code is:

respond=$(ssh ${fromNode} /usr/bin/time "-f" "%e" "'sh' '-c' 'virsh migrate --live ${VM} qemu+ssh://${toNode}/system --verbose > /dev/null 2>&1'") 

The value of respond is just empty, though the time is printed out to the standard output.

like image 375
Andy Dang Avatar asked Aug 21 '12 05:08

Andy Dang


People also ask

How do you store output of a shell script in a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...] arg1 arg2 ...'

Can I SSH in a bash script?

Bash script SSH is a common tool for Linux users. It is needed when you want to run a command from a local server or a Linux workstation. SSH is also used to access local Bash scripts from a local or remote server.

How do I print a variable in bash?

Use the -v flag with printf to assign a shell variable to store the data instead of printing it in standard output. The output prints the stored variable contents. Note: Read our tutorial to learn everything about bash functions.


2 Answers

"time" command prints result to stderr, not to stdout. Thus it is not piped into your variable.

You should reroute stderr to stdout to achieve what you want:

 result=$(ssh host time "command" 2>&1) 

And your full code can look something like this:

 respond=$(ssh ${fromNode} /usr/bin/time "-f" "%e" "'sh' '-c' 'virsh migrate --live ${VM} qemu+ssh://${toNode}/system > /dev/null 2>&1'" 2>&1) 
like image 173
Rogach Avatar answered Sep 22 '22 12:09

Rogach


Try swapping the order of your redirections around (to 2>&1 >/dev/null). Your current code is sending both stdout and stderr to /dev/null (so I'm kind of curious as to why anything is printed at all).

Why is this necessary? The syntax 2>&1 means 'duplicate stdout (descriptor 1) as stderr (descriptor 2)'; in effect, stderr is made into a copy of the current stdout. If you put >/dev/null first, then stdout is first redirected to /dev/null, and then stderr is pointed at the current stdout, i.e. /dev/null.

But if you put >/dev/null second, stderr will first become a copy of the current stdout (the normal output stream), before stdout is redirected. So the command's stderr prints to the tty (or the interpreter) as if it came from stdout, while stdout is silenced. This is the behaviour you want.

From man bash:

Note that the order of redirections is significant. For example, the command

ls > dirlist 2>&1 

directs both standard output and standard error to the file dirlist, while the command

ls 2>&1 > dirlist 

directs only the standard output to file dirlist, because the standard error was duplicated as standard output before the standard output was redirected to dirlist.

like image 38
nneonneo Avatar answered Sep 19 '22 12:09

nneonneo