ssh invocation in script function

杀马特。学长 韩版系。学妹 提交于 2020-01-21 11:46:27

问题


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:


ssh is consuming stdin. Pass -n.



来源:https://stackoverflow.com/questions/4932774/ssh-invocation-in-script-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!