bash 忽略 shell 脚本中的特定错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17830326/
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
Ignoring specific errors in a shell script
提问by Stunt
I have a small snippet of a shell script which has the potential to throw many errors. I have the script currently set to globally stop on all errors. However i would like for this small sub-section is slightly different.
我有一小段 shell 脚本,它有可能引发许多错误。我将脚本当前设置为全局停止所有错误。但是我想对于这个小子部分略有不同。
Here is the snippet:
这是片段:
recover database using backup controlfile until cancel || true;
auto
I'm expecting this to eventually throw a "file not found" error. However i would like to continue executing on this error. For any other error i would like the script to stop.
我希望这最终会抛出“找不到文件”错误。但是我想继续执行这个错误。对于任何其他错误,我希望脚本停止。
What would be the best method of achieving this?
实现这一目标的最佳方法是什么?
Bash Version 3.00.16
Bash 版本 3.00.16
回答by devnull
In order to prevent bash to ignore error for specific commands you can say:
为了防止 bash 忽略特定命令的错误,您可以说:
some-arbitrary-command || true
This would make the script continue. For example, if you have the following script:
这将使脚本继续。例如,如果您有以下脚本:
$ cat foo
set -e
echo 1
some-arbitrary-command || true
echo 2
Executing it would return:
执行它会返回:
$ bash foo
1
z: line 3: some-arbitrary-command: command not found
2
In the absence of || true
in the command line, it'd have produced:
如果没有|| true
在命令行中,它会产生:
$ bash foo
1
z: line 3: some-arbitrary-command: command not found
Quote from the manual:
手册中的引用:
The shell does not exit if the command that fails is part of the command list immediately following a
while
oruntil
keyword, part of the test in anif
statement, part of any command executed in a&&
or||
list except the command following the final&&
or||
, any command in a pipeline but the last, or if the command's return status is being inverted with!
. A trap onERR
, if set, is executed before the shell exits.
如果失败的命令是紧跟在命令列表的一部分shell不会退出
while
或until
关键字在测试中,部分if
声明,在执行任何命令的一部分&&
或||
以下的最终除了命令列表&&
或||
在任何命令一个管道但最后一个,或者如果命令的返回状态正在被!
.ERR
如果设置了一个陷阱,则在 shell 退出之前执行。
EDIT: In order to change the behaviour such that in the execution should continue only ifexecuting some-arbitrary-command
returned file not found
as part of the error, you can say:
编辑:为了改变行为,只有当执行作为错误的一部分some-arbitrary-command
返回时才应继续执行file not found
,您可以说:
[[ $(some-arbitrary-command 2>&1) =~ "file not found" ]]
As an example, execute the following (no file named MissingFile.txt
exists):
例如,执行以下命令(不MissingFile.txt
存在命名的文件):
$ cat foo
#!/bin/bash
set -u
set -e
foo() {
rm MissingFile.txt
}
echo 1
[[ $(foo 2>&1) =~ "No such file" ]]
echo 2
$(foo)
echo 3
This produces the following output:
这会产生以下输出:
$ bash foo
1
2
rm: cannot remove `MissingFile.txt': No such file or directory
Note that echo 2
was executed but echo 3
wasn't.
请注意,echo 2
已执行但未执行echo 3
。
回答by Chris Cogdon
Use:
用:
command || :
: is a bash built-in that always returns success. And, as discussed above, || short-circuits so the RHS is only executed if the LHS fails (returns non-zero).
: 是一个内置的 bash,它总是返回成功。并且,如上所述,|| 短路,因此仅当 LHS 失败(返回非零值)时才执行 RHS。
The above suggestions to use 'true' will also work, but are inefficient as 'true' is an external program.
上述使用 'true' 的建议也适用,但效率低下,因为 'true' 是一个外部程序。