bash 如果发生错误,从 shell 脚本执行 shell 命令而不停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1075083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:18:26  来源:igfitidea点击:

Execute a shell command from a shell script without stopping if error occurs

linuxbasherror-handling

提问by JorgeO

In a sort of try/catch form I want to execute a bash that doesn't stop if an error occurs.

在一种尝试/捕获形式中,我想执行一个如果发生错误就不会停止的 bash。

The specific bash is:

具体的bash是:

#!/bin/sh

invoke-rc.d tomcat stop

rm -fr /var/webapps/
cp -R $WEBAPP /var/webapps/
invoke-rc.d tomcat start

I want to exec "invoke-rc.d tomcat stop" and even if Tomcat is not running, continue to execute the other bash commands.

我想执行“invoke-rc.d tomcat stop”,即使Tomcat没有运行,也要继续执行其他bash命令。

回答by Tim Post

Try:

尝试:

invoke-rc.d tomcat stop > /dev/null 2>&1 || true

A little background:

一点背景:

user@tower: # true
user@tower: # echo $?
0
user@tower: # false
user@tower: # echo $?
1

user@tower: # which true
/bin/true
user@tower: # which false
/bin/false

The realsolution is looking at the tomcat init script to see how it knows if tomcat is running :) That way, you don't pester it needlessly.

真正的解决方案是在看tomcat的启动脚本,看看它是如何知道如果Tomcat运行:)这样一来,你不纠缠这不必要。

See this poston the other suggestion to unset / set +e. While it would solve your immediate problem, you may find that you need the recently unset behavior in your own script, especially since you are copying files.

请参阅有关取消设置/设置 +e 的其他建议的这篇文章。虽然它可以解决您当前的问题,但您可能会发现您需要在自己的脚本中使用最近未设置的行为,尤其是在复制文件时。

This is one of the biggest reasons why true and false were made, other than making Makefiles behave as expected in a variety of build environments.

这是生成 true 和 false 的最大原因之一,而不是让 Makefile 在各种构建环境中按预期运行。

Also, set +e is not entirely portable, i.e. some versions of Solaris (and even Dash) .. but I doubt that this is a concern for you.

此外, set +e 并非完全可移植,即某些版本的 Solaris(甚至 Dash).. 但我怀疑这是否是您的问题。

回答by Adam Rosenfield

Disable the "exit immediately" option with set +e, run your command, then optionally re-enable it with set -e:

禁用“立即退出”选项set +e,运行您的命令,然后可以选择重新启用它set -e

set +e
invoke-rc.d tomcat stop
set -e  # optional

See section 4.3.1of the Bash manual for an explanation of the setbuiltin and all of its various options (of which there are many).

请参阅Bash 手册的第 4.3.1 节以了解set内置函数及其所有各种选项(其中有很多)。

回答by JorgeO

Use bash's set commandto temporarily disable exit-on-nonzero behaviour.

使用bash 的 set 命令暂时禁用非零退出行为。

set +e
invoke-rc.d tomcat stop
set -e

回答by Vatine

If invoke-rc.d tomcat stopis the only thing you want to protect against failing, maybe invoke-rc.d tomcat stop || truemay do it? That should never have a non-zero exit status.

如果invoke-rc.d tomcat stop是你唯一想防止失败的东西,也许invoke-rc.d tomcat stop || 真的可以吗?那永远不应该有非零退出状态。

回答by Vaibhav

Try redirecting the standard error to a file ...something like 2> myerror.

尝试将标准错误重定向到文件...类似于 2> myerror.