4

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 \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 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

1 Answer 1

5

ssh is consuming stdin. Pass -n.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.