这就是我的 shell 脚本的一部分。
#! /bin/sh
sftp -i $IdentityFile $ServerAddress << EOF
command 1 #Execute in the remote
command 2 #Execute in the remote
bye
EOF
command 3 #Execute locally
根据我目前对脚本的了解,如果一个命令无法执行,控制权只会传递给下一个命令。但是如果sftp命令无法在上面的块中建立网络连接怎么办?这是否意味着command 1
和command 2
将在当地执行?或者控件会跳转到command 3
?
如何在 sftp 中捕获可能的错误并将控件定向到command 3
?如果可能的话,我可以使用?
变量检测错误以采取某些先发制人的行动吗?一些指导会很棒。
有几件事要做。
首先,您需要将输入提取为函数,以允许使用command3进行管道传输,这样会更清晰:
function sfpInstruction() {
cat << EOF
command 1
command 2
bye
EOF
}
因此,您的 sftp 指令可以更改为:
tmpFile="/tmp/errorFile.txt"
sftp -i $IdentityFile $ServerAddress $( sfpInstruction ) 2>"$tmpFile" || command3
这样的方式:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句