Ssh call in script function

I wrote a simple script that calls a function in a while loop. I decided the while loop was working correctly. In the do section, I call the function. This also works great. However, as soon as I execute the command on the remote host using ssh in the function implementation, this seems to break the while loop. For the first iteration, the function call succeeds, the command is called on the remote host, and the result is returned as expected. However, then the script ends as if I had executed the output in the implementation of the function, which I havent't.

#!/bin/bash function update_relevant_domUs() { if [ $# -eq 0 ] then fatal not enough arguments fi if [ $# -gt 2 ] then fatal "unsupported number of arguments $#" fi if [ $# -eq 2 ] && [ "$1" != "Domain-0" ] && [ "$1" != "Name" ] then #printf "$NAME \t $STATE\n" local cmd="ssh root@ $1 /usr/bin/zypper --non-interactive refresh" printf "Executing command: $cmd\n" #`ssh root@ $1 echo \$PATH` local res=`$cmd` local ret=$? printf "Ret: $ret - Report: \n $res \n\f" fi return 0 } xm list | while read NAME ID MEM VCPUS STATE TIME; do update_relevant_domUs $NAME $STATE; done 

If I replace the string

 local res=`$cmd` 

with

 local res=`echo $cmd` 

The outer while loop executes as expected. Any help on this would be greatly appreciated.

Best wishes,

ajag

+4
source share
1 answer

ssh consumes stdin. Pass -n .

+5
source

Source: https://habr.com/ru/post/1338836/


All Articles