简体   繁体   English

如果SSH中的任何命令返回非零值,如何退出shell脚本

[英]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.

相关问题 如果任何命令返回非零值,则中止 shell 脚本 - Aborting a shell script if any command returns a non-zero value 以下 SSH 命令以非零退出状态响应 - The following SSH command responded with a non-zero exit status 查找:如何强制非零退出状态 - find: How to force a non-zero exit status R中的非零退出状态 - Non-Zero Exit Status in R python 返回非零退出状态 1 - python returned non-zero exit status 1 如果两台机器上都缺少文件,如何以非零状态退出shell脚本? - How to exit out of the shell script with non zero status if the files are missing in both the machines? Linux CLI watch switch -e, --errexit 意外退出,“命令退出,非零状态,按一个键退出” - Linux CLI watch switch -e, --errexit unexpected exit with “command exit with a non-zero status, press a key to exit” "subprocess.CalledProcessError:命令“df”返回非零退出状态 1" - subprocess.CalledProcessError: Command 'df' returned non-zero exit status 1 运行 java 时,Gradle 任务以非零退出值 100 完成 - Gradle task run finished with non-zero exit value 100 when running java Gradle项目刷新失败。 在linux bean上使用android studio 2.3完成非零退出值2 - Gradle project refresh failed. finished with non-zero exit value 2 with android studio 2.3 on linux bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM