相关疑难解决方法(0)

Bash忽略特定命令的错误

我正在使用以下选项

set -o pipefail
set -e
Run Code Online (Sandbox Code Playgroud)

在bash脚本中停止执行错误.我有100个脚本执行,我不想检查脚本的返回代码.但对于特定的脚本,我想忽略错误.我怎样才能做到这一点 ?

linux bash

399
推荐指数
9
解决办法
23万
查看次数

如何在shell函数中获得效果并使用"set -e"?

set -e(或以脚本开头#!/bin/sh -e)对于在出现问题时自动轰炸非常有用.它使我不必错误地检查可能失败的每个命令.

如何在函数内获得相应的内容?

例如,我有以下脚本在出错时立即退出并出现错误退出状态:

#!/bin/sh -e

echo "the following command could fail:"
false
echo "this is after the command that fails"
Run Code Online (Sandbox Code Playgroud)

输出如预期:

the following command could fail:
Run Code Online (Sandbox Code Playgroud)

现在我想把它包装成一个函数:

#!/bin/sh -e

my_function() {
    echo "the following command could fail:"
    false
    echo "this is after the command that fails"
}

if ! my_function; then
    echo "dealing with the problem"
fi

echo "run this all the time regardless of the success of my_function"
Run Code Online (Sandbox Code Playgroud)

预期产量:

the following command could fail: …
Run Code Online (Sandbox Code Playgroud)

shell sh

32
推荐指数
6
解决办法
2万
查看次数

标签 统计

bash ×1

linux ×1

sh ×1

shell ×1