Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
863 views
in Technique[技术] by (71.8m points)

bash - ssh invocation in script function

I have written a simple script that calls a function in a while loop. I have determined that the while loop works correctly. In the do section I call a function. This also works fine. However as soon as I execute a command on a remote host using ssh in the function implementation this seems to break the calling while loop. For the first iteration the function call succeeds, the command is invoked on the remote host and the result is returned as expected. However then the script ends as if I had done an exit in the function implementation 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  $STATE
"
              local cmd="ssh root@$1 /usr/bin/zypper --non-interactive refresh"
              printf "Executing command: $cmd
"
              #`ssh root@$1 echo $PATH`
              local res=`$cmd`
              local ret=$?
              printf "Ret: $ret - Report: 
 $res 
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 line

local res=`$cmd`

with

local res=`echo $cmd`

The outer while loop is executed as expected. Any Help on this would be greatly appreciated.

Best Regards,

ajag

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ssh is consuming stdin. Pass -n.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...