我有一個 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。 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 ...