Bash 忽略特定命令的错误 [英] Bash ignoring error for a particular command

查看:34
本文介绍了Bash 忽略特定命令的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下选项

set -o pipefail
set -e

在 bash 脚本中停止执行错误.我有大约 100 行脚本正在执行,我不想检查脚本中每一行的返回代码.

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.

但是对于一个特定的命令,我想忽略该错误.我该怎么做?

But for one particular command, I want to ignore the error. How can I do that?

推荐答案

解决方案:

particular_script || true

示例:

$ 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 永远不会打印.

另外,我想补充一点,当 pipefail 开启时,shell 认为整个管道具有非零退出代码就足够了当管道中的命令之一具有非零退出代码(pipefail 关闭时,它必须是最后一个).

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).

$ set -o pipefail
$ false | true ; echo $?
1
$ set +o pipefail
$ false | true ; echo $?
0

这篇关于Bash 忽略特定命令的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆