Capturing SSH output as variable in bash script

2019-03-12 18:00发布

问题:

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.

回答1:

"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)


回答2:

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.



标签: bash ssh stdout