如何设置bash变量

时间:2013-07-23 21:36:53

标签: bash variables expect

我希望以下expect脚本在主bash脚本中设置变量。换句话说,我想将$ SSH_SUCCESS设置为通过/失败字符串。我尝试过使用$ expect_out和$ :: env(SSH_SUCCESS),但都没有成功。如何设置预期的bash变量?

expect << EOF
log_user 0
log_file $TEST_LOG
set timeout 5
spawn ssh root@$RADIO_IP

.....
....expect script, echoing the return of an SSH command...

send "echo\$?\n"
expect {
  "0" {
        send_user "SSH test: PASSED\r"
        SSH_SUCCESS="PASSED"
      }
  "1" {
        send_user "SSH test: FAILED\r"
        SSH_SUCCESS="FAILED"
      }


sleep 1
send_user "\n"
exit

EOF    

  echo $SSH_SUCCESS

2 个答案:

答案 0 :(得分:1)

我不知道Expect,但我认为它就是这样的

SSH_SUCCESS=$(expect <<EOF
...
expect {
  "0" {
        puts "PASSED"
      }
  "1" {
        puts "FAILED"
      }
...
EOF
)

echo $SSH_SUCCESS

答案 1 :(得分:0)

无法设置变量。

这是确定Expect退出状态的另一种方法。

expect << EOF
log_user 0
log_file $TEST_LOG
set timeout 5
spawn ssh root@$RADIO_IP

.....
....expect script, echoing the return of an SSH command...

send "echo\$?\n"

expect -re "\[0-9\]+$"
#set latest command exit status to valiable "exit_code"
set exit_status \$expect_out(0,string)

if { \$exit_status == 0 } {
    send_user "SSH test: PASSED\r"
} else {
    send_user "SSH test: FAILED\r"
}

sleep 1
send_user "\n"
# exit expect as latest command exit status
exit \$exit_status
EOF

if [ $? -eq 0 ];then
    SSH_SUCCESS="PASSED"
else
    SSH_SUCCESS="FAILED"
fi
echo ${SSH_SUCCESS}