bash - 如何设置一个bash变量

  显示原文与译文双语对照的内容
0 0

我希望下面的expect脚本在主bash脚本中设置一个变量。 换句话说,我想将 $SSH_SUCCESS 设置为传递/失败字符串。 我试过使用 $expect_out 和 $::env(SSH_SUCCESS),但不成功。 我怎样设置一个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 {
"0" {
 send_user"SSH test: PASSEDr"
 SSH_SUCCESS="PASSED"
 }
"1" {
 send_user"SSH test: FAILEDr"
 SSH_SUCCESS="FAILED"
 }
sleep 1
send_user"n"
exit
EOF 
 echo $SSH_SUCCESS
时间:原作者:4个回答

0 0

我不知道期望但是我觉得这样

SSH_SUCCESS=$(expect <<EOF
...
expect {
"0" {
 puts"PASSED"
 }
"1" {
 puts"FAILED"
 }
...
EOF
)
echo $SSH_SUCCESS
原作者:
0 0

没有办法设定一个变量。

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

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: PASSEDr"
} else {
 send_user"SSH test: FAILEDr"
}
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}
原作者:
...