[英]How to exit from shell script if any of the command in SSH returns non-zero value
I have a shell script with below ssh command to perform few actions, 我在下面的ssh命令中有一个shell脚本来执行一些操作,
$SSH $user@$remoteIpAddress "sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "
Directory $remoteLocation/yyy is not available and it will display the error as, 目录$ remoteLocation / yyy不可用,并将错误显示为:
./test.sh: line 6: cd: /opt/test/yyy: No such file or directory ./test.sh:第6行:cd:/ opt / test / yyy:没有这样的文件或目录
and it is proceeding to the next line. 它正在进行下一行。 My scenario is to exit from the shell script itself if any command in ssh returns non-zero value.
我的情况是,如果ssh中的任何命令返回非零值,则退出shell脚本本身。 I could add set -e to exit from shell but i am not sure how to handle it in ssh.
我可以添加set -e从shell退出,但是我不确定如何在ssh中处理它。 Thanks in advance.
提前致谢。
EDIT : 编辑:
I have few lines of command below the SSH command. 我在SSH命令下面有几行命令。 I need a solution to exit entirely from the script and not to execute any lines below the ssh command.
我需要一个解决方案以完全退出脚本,而不执行ssh命令下面的任何行。
Keep in mind that your set -e
idea applies just as well to the shell you open on the other end with ssh. 请记住,您的
set -e
想法同样适用于使用ssh在另一端打开的外壳。 So you can prepend your ssh commands with that to ensure an error terminates the ssh session: 因此,您可以在ssh命令之前添加该命令,以确保错误终止ssh会话:
$SSH $user@$remoteIpAddress "set -e; sudo rm -rf $remoteLocation/xxx/*; cd $remoteLocation/yyy; .... ... "
Then the ssh shell will exit with non-zero error code as soon as an error is encountered, and your script will exit as well if you have set -e
earlier in your script. 然后,一旦遇到错误,ssh shell将以非零错误代码退出,并且如果您在脚本的前面
set -e
则脚本也将退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.