【问题标题】:Check for response via shell script通过 shell 脚本检查响应
【发布时间】:2018-12-02 21:57:36
【问题描述】:

请原谅这个基本问题,但我没有什么想法可以尝试,而且在编写任何脚本时我还是个初学者。在关闭 VM 之前,我需要检查 VM 以查看它是否已打开。这是我正在尝试的:

(忽略变量,我在其他几个函数中使用它们,它们都工作正常)

function powerOff(){
    ssh $IP_ADDR 'vim-cmd vmsvc/power.getstate '$VM_ID' | grep Powered'
    if [ "${1}" == "Powered off" ]; then 
        echo "The VM is already off"
    elif [ "${1}" == "Powered on" ]; then
        ssh $CENTOS_IP 'init 0'
    else
        echo "You are horrible at this and your script is failing"
    fi
}

“power.getstate”返回“Powered on”或“Powered off”,因为我正在使用它。

我正在一个已经运行的虚拟机上测试这个,所以我希望响应是“这个虚拟机已经启动”。相反,我得到了 getstate 命令的输出(这很好,但我什至不想看到输出),然后它告诉我我在这方面很糟糕。有什么建议吗?

【问题讨论】:

    标签: shell scripting centos7


    【解决方案1】:

    在变量中捕获结果。

    function powerOff(){
        powerfield=$(ssh $IP_ADDR 'vim-cmd vmsvc/power.getstate '$VM_ID' | grep Powered')
        if [ "${powerfield}" == "Powered off" ]; then 
            echo "The VM is already off"
        elif [ "${powerfield}" == "Powered on" ]; then
            ssh $CENTOS_IP 'init 0'
        else
            echo "You are horrible at this and your script is failing"
        fi
    }
    

    【讨论】:

    • 啊哈!谢谢你!我在 if 命令中缺少 if 语句周围的 "" 和 {}。你是救生员!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多