简体   繁体   English

我如何在linux的exec命令中执行多个命令

[英]How can I execute more than one command in exec command of linux expect

I'm trying to detect a host is available by using expect of Linux. 我正在尝试使用Linux的期望值来检测主机是否可用。

Now, I can using the following commands via bash to get the return value and check host status. 现在,我可以通过bash使用以下命令来获取返回值并检查主机状态。

#!/usr/bin/bash
nc -z $DUT_IP 22 && echo $?

I want to do the same things via expect. 我想通过期望做同样的事情。 But seems I failed. 但似乎我失败了。

#!/usr/bin/expect --   
set DUT_IP "192.168.1.9"
set result [exec nc -z $DUT_IP 22] 
send_user "$result\n\n"

I got the following error messages: 我收到以下错误消息:

    invalid port &&
        while executing
    "exec nc -z $DUT_IP 22 && echo $?"
        invoked from within
    "set result [exec nc -z $DUT_IP 22 && echo $?] "
        (file "reboot.exp" line 44)

Use of && to separate commands is a shell construct, not an expect construct. 使用&&分隔命令是shell构造,而不是Expect构造。 You can explicitly launch a shell for that command list: 您可以显式启动该命令列表的外壳程序:

set result [exec sh -c "nc -z $DUT_IP 22 && echo $?"]

Note that will only print the exit status if the command succeeded , thus result will either be "0" or empty. 请注意,只有在命令成功执行后 ,才会显示退出状态,因此结果将为“ 0”或为空。 You want to use ; 您要使用; instead of && 代替&&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM