Capturing SSH output as variable in bash script

前端 未结 2 957
北荒
北荒 2020-12-28 16:21

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/b

2条回答
  •  情书的邮戳
    2020-12-28 16:59

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

提交回复
热议问题