Bash:Expect脚本不存储退出代码的值

时间:2018-11-09 10:48:46

标签: bash expect

我正在使用bash Expect脚本,并想在变量末尾记录diff命令的退出代码。但是,它无法识别。如何制作此记录并存储值?

/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
send "wget -r  --spider --user user--password password server/php/site_index.php -P /data/tmp/wget_result_after \r"
expect { 
            "Downloaded" 
       }
send "set exitCodeDb 1 \r"
expect { 
            "*]# " 
       }
send "diff --brief /data/tmp/db1 /data/tmp/db2 && exitCodeDb=0 || exitCodeDb=1 \r"
expect { 
            "*]# " 
       }  
send "echo \"Exit code for DB diff is $exitCodeDb\" \r"  
 expect { 
            "*]# " 
       }

EOF

我得到了错误

 can't read "exitCodeDb": no such variable

1 个答案:

答案 0 :(得分:2)

问题在于变量$exitCodeDbexpect解释。

您实际上想传递带有bash变量的字符串,但是对于expect,这只是一个字符串。

要通过美元符号,您可以使用荣誉{...}

set str {echo -e "Exit code for DB diff is $exitCodeDb"}
send "$str\r"