相关疑难解决方法(0)

我如何知道 set -e 选项是否已启用?

我正在编写一个 shell 函数,它将从许多不同的地方调用,我想忽略函数内部发生的所有错误,禁用(可能启用)set -eshell 选项,执行set +e.

问题是我不知道是否设置了该选项,所以我不知道是否真的需要禁用它,更重要的是,如果我需要在最后再次设置它。

那么,我怎么知道它是否已设置?有没有办法忽略 shell 函数中所有命令的错误,-e如果设置了则忽略shell 选项?

linux bash command-line

11
推荐指数
1
解决办法
3128
查看次数

是否可以检查 -e 是否在 bash 脚本中设置?

如果 shell 函数需要特定的 -e/+e 设置才能工作,是否可以在本地设置该设置,然后在退出该函数之前将其恢复到以前的设置?

myfunction()
{
   # Query here if -e is set and remember in a variable?
   # Or push the settings to then pop at the end of the function?
   set +e
   dosomething
   doanotherthing
   # Restore -e/+e as appropriate, don't just do unconditional   set -e
}
Run Code Online (Sandbox Code Playgroud)

bash shell-script

10
推荐指数
2
解决办法
2705
查看次数

标签 统计

bash ×2

command-line ×1

linux ×1

shell-script ×1