有没有一种方法可以锁定Bash shell中的‘set +e’,使得调用‘set -e’的源脚本无法覆盖?
同时,子shell仍然能够‘set -e’。这种情况是否可行?
这是否可能实现?
有没有一种方法可以锁定Bash shell中的‘set +e’,使得调用‘set -e’的源脚本无法覆盖?
同时,子shell仍然能够‘set -e’。这种情况是否可行?
这是否可能实现?
暂时使用函数覆盖内置的 set
命令:
#!/bin/bash
set() { [[ "$1" != "-e" ]] && command set "${@}"; }
# source your script here
unset set # if you want to use set -e again
unset set
,则可以在脚本中多次使用该函数。但这并非万无一失,因为例如可以通过在您的源文件中使用set -x -e
来绕过它。 - Cyruscommand
命令来轻松实现。它也可以自行unset
该函数。 - John Bollingerset() { command set "${@}"; setres=$? ; if [[ -o errexit ]] ; then set +e && echo "A process just attempted to 'set -e'" && false ; else return $setres ; fi }
- user1797455
set -e
的脚本很可能是因为它们在某些情况下依赖它才能正常运行。事实上,一些脚本可能会在没有显式运行set
的情况下也依赖于它。 - John Bollingerset +e
完成什么任务? - John Bollingererrexit
的作用。 - user1797455