Linux Bash 忽略特定命令的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11231937/
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
Bash ignoring error for a particular command
提问by Vivek Goel
I am using following options
我正在使用以下选项
set -o pipefail
set -e
In bash script to stop execution on error. I have ~100 lines of script executing and I don't want to check return code of every line in the script.
在 bash 脚本中停止执行错误。我有大约 100 行脚本正在执行,我不想检查脚本中每一行的返回代码。
But for one particular command, I want to ignore the error. How can I do that?
但是对于一个特定的命令,我想忽略该错误。我怎样才能做到这一点?
采纳答案by Igor Chubin
The solution:
解决方案:
particular_script || true
Example:
例子:
$ cat /tmp/1.sh
particular_script()
{
false
}
set -e
echo one
particular_script || true
echo two
particular_script
echo three
$ bash /tmp/1.sh
one
two
three
will be never printed.
three
永远不会打印。
Also, I want to add that when pipefail
is on,
it is enough for shell to think that the entire pipe has non-zero exit code
when one of commands in the pipe has non-zero exit code (with pipefail
off it must the last one).
另外,我想补充一点,当pipefail
打开时,当管道中的一个命令具有非零退出代码时,shell 认为整个管道具有非零退出代码就足够了(pipefail
关闭它必须是最后一个) .
$ set -o pipefail
$ false | true ; echo $?
1
$ set +o pipefail
$ false | true ; echo $?
0
回答by Lars Kotthoff
Just add || true
after the command where you want to ignore the error.
只需|| true
在要忽略错误的命令后面添加即可。
回答by Lily Finley
More concisely:
更简洁:
! particular_script
From the POSIX specificationregarding set -e
(emphasis mine):
从关于(强调我的)的POSIX 规范set -e
:
When this option is on, if a simple command fails for any of the reasons listed in Consequences of Shell Errors or returns an exit status value >0, and is not part of the compound list following a while, until, or if keyword, and is not a part of an AND or OR list, and is not a pipeline preceded by the ! reserved word, then the shell shall immediately exit.
当此选项打开时,如果简单命令由于外壳错误的后果中列出的任何原因而失败或返回退出状态值 >0,并且在 while、until 或 if 关键字之后不属于复合列表的一部分,并且不是 AND 或 OR 列表的一部分,也不是以! 保留字,则外壳应立即退出。
回答by Timo
Instead of "returning true", you can also use the "noop" or null utility (as referred in the POSIX specs) :
and just "do nothing". You'll save a few letters. :)
除了“返回 true”,您还可以使用“noop”或 null 实用程序(如POSIX 规范中所述):
并且“什么都不做”。你会保存几个字母。:)
#!/usr/bin/env bash
set -e
man nonexistentghing ||?:
echo "It's ok.."
回答by Arslan Qadeer
Don't stop and also save exit status
不要停止并保存退出状态
Just in case if you want your script not to stop if a particular command fails and you also want to save error code of failed command:
以防万一,如果您希望脚本在特定命令失败时不停止,并且您还想保存失败命令的错误代码:
set -e
EXIT_CODE=0
command || EXIT_CODE=$?
echo $EXIT_CODE
回答by Payman
I have been using the snippet below when working with CLI tools and I want to know if some resource exist or not, but I don't care about the output.
在使用 CLI 工具时,我一直在使用下面的代码片段,我想知道某些资源是否存在,但我不关心输出。
if [ -z "$(cat no_exist 2>&1 >/dev/null)" ]; then
echo "none exist actually exist!"
fi
回答by Foto Blysk
while || true
is preferred one, but you can also do
while|| true
是首选之一,但您也可以这样做
var=$(echo $(exit 1)) # it shouldn't fail
回答by Q-life
I kind of like this solution :
我有点喜欢这个解决方案:
: `particular_script`
The command/script between the back ticks is executed and its output is fed to the command ":" (which is the equivalent of "true")
执行后记号之间的命令/脚本并将其输出馈送到命令“:”(相当于“true”)
$ false
$ echo $?
1
$ : `false`
$ echo $?
0
edit: Fixed ugly typo
编辑:修正了丑陋的错字
回答by volingas
If you want to prevent your script failing andcollect the return code:
如果您想防止脚本失败并收集返回码:
command () {
return 1 # or 0 for success
}
set -e
command && returncode=$? || returncode=$?
echo $returncode
returncode
is collected no matter whether command succeeds or fails.
returncode
无论命令成功还是失败,都会被收集。
回答by Konard
No solutions worked for me from here, so I found another one:
从这里没有解决方案对我有用,所以我找到了另一个:
set +e
find "./csharp/Platform.$REPOSITORY_NAME/obj" -type f -iname "*.cs" -delete
find "./csharp/Platform.$REPOSITORY_NAME.Tests/obj" -type f -iname "*.cs" -delete
set -e
This is useful for CI & CD. This way the error messages are printed but the whole script continues to execute.
这对 CI 和 CD 很有用。这样会打印错误消息,但整个脚本会继续执行。