我正在编写一个 shell 函数,它将从许多不同的地方调用,我想忽略函数内部发生的所有错误,禁用(可能启用)set -eshell 选项,执行set +e.
问题是我不知道是否设置了该选项,所以我不知道是否真的需要禁用它,更重要的是,如果我需要在最后再次设置它。
那么,我怎么知道它是否已设置?有没有办法忽略 shell 函数中所有命令的错误,-e如果设置了则忽略shell 选项?
如果 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)