1

我希望以下期望脚本在主 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
4

2 回答 2

1

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

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

echo $SSH_SUCCESS
于 2013-07-24T06:11:58.330 回答
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}
于 2013-07-24T13:20:54.343 回答