通过 shell 脚本检查响应

Check for response via shell script

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

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

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

"power.getstate" returns "Powered on" 或 "Powered off" 因为我正在搜索它。

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

在变量中捕获结果。

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
}