In a sort of try/catch form I want to execute a bash that doesn't stop if an error occurs.
在某种try/catch表单中,我希望执行一个在发生错误时不会停止的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.
我想执行“发票管理”。即使tomcat没有运行,也要继续执行其他bash命令。
5 个解决方案
#1
28
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 real solution 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 init脚本,看看它如何知道tomcat是否正在运行:)这样,您就不必不必要地纠缠它了。
See this post on 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.
除了让makefile在各种构建环境中按照预期的方式运行之外,这也是为什么要创建true和false的最大原因之一。
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)的一些版本。但我怀疑这不是你的问题。
#2
28
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.1 of the Bash manual for an explanation of the set
builtin and all of its various options (of which there are many).
参见Bash手册第4.3.1节,了解关于set build和它的所有各种选项(其中有许多选项)的解释。
#3
4
Use bash's set command to temporarily disable exit-on-nonzero behaviour.
使用bash的set命令临时禁用异常-非零行为。
set +e
invoke-rc.d tomcat stop
set -e
#4
1
If invoke-rc.d tomcat stop is the only thing you want to protect against failing, maybe invoke-rc.d tomcat stop || true may do it? That should never have a non-zero exit status.
如果invoke-rc。d tomcat停止是惟一希望防止失败的东西,可能是invoke-rc。d tomcat停止||真的可以做吗?不应该有非零的退出状态。
#5
-3
Try redirecting the standard error to a file ...something like 2> myerror.
尝试将标准错误重定向到一个文件……2 > myerror。
#1
28
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 real solution 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 init脚本,看看它如何知道tomcat是否正在运行:)这样,您就不必不必要地纠缠它了。
See this post on 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.
除了让makefile在各种构建环境中按照预期的方式运行之外,这也是为什么要创建true和false的最大原因之一。
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)的一些版本。但我怀疑这不是你的问题。
#2
28
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.1 of the Bash manual for an explanation of the set
builtin and all of its various options (of which there are many).
参见Bash手册第4.3.1节,了解关于set build和它的所有各种选项(其中有许多选项)的解释。
#3
4
Use bash's set command to temporarily disable exit-on-nonzero behaviour.
使用bash的set命令临时禁用异常-非零行为。
set +e
invoke-rc.d tomcat stop
set -e
#4
1
If invoke-rc.d tomcat stop is the only thing you want to protect against failing, maybe invoke-rc.d tomcat stop || true may do it? That should never have a non-zero exit status.
如果invoke-rc。d tomcat停止是惟一希望防止失败的东西,可能是invoke-rc。d tomcat停止||真的可以做吗?不应该有非零的退出状态。
#5
-3
Try redirecting the standard error to a file ...something like 2> myerror.
尝试将标准错误重定向到一个文件……2 > myerror。