在脚本函数调用的ssh [英] ssh invocation in script function

查看:112
本文介绍了在脚本函数调用的ssh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的脚本,调用whil​​e循环的功能。我已经确定,while循环正常工作。在做节中,我调用一个函数。这也能正常工作。但是,只要我在功能的实现使用ssh远程主机上执行命令,这似乎打破while循环调用。对于第一次迭代函数调用成功,则调用该命令的远程主机上,并如预期的那样返回结果。但是那么脚本结束,如果我做了在功能实现,我havent't退出。

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

如果我更换行

local res=`$cmd`

local res=`echo $cmd`

按预期执行while循环外。任何帮助将大大AP preciated。

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

最好的问候,

ajag

推荐答案

SSH 消耗标准输入。通 -n

这篇关于在脚本函数调用的ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆