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

问题是变量$exitCodeDbexpect解释。

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

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

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

最新更新