我有一个 TCL Expect 脚本,它运行 scp 命令来将配置文件上传到我的 DNS 服务器:
#!/usr/bin/expect -f
set config "~/dnsmasq.conf"
spawn /usr/bin/scp "$config" [email protected]:/etc/dnsmasq.conf
expect {
-re ".*yes.*no.*" {
exp_send "yes\r"
exp_continue
}
-re ".*password.*" {
exp_send "$password\r"
expect {
-re ".*denied.*" {
exit 7
}
}
}
}
如果找不到 scp 实用程序,我想返回特定的错误代码。目前,脚本以状态 1 退出。如果脚本以状态 7 退出,我可以处理该问题,因为我知道这是访问被拒绝错误。 Apache 日志中显示的错误是:
couldn't execute "/usr/bin/scp": no such file or directory
while executing
"spawn /usr/bin/scp "$config" [email protected]:/etc/dnsmasq.conf"
此时如何返回错误代码 5 或其他内容?
答案1
最好的方法是检查它是否存在并且可执行:
if { ! ([file exists /usr/bin/scp] && [file executable /usr/bin/scp])} {
puts stderr "/usr/bin/scp does not exist or is not executable"
exit 7
}
spawn /usr/bin/scp ...