我正在尝试在 bash 中构建一个预执行检查器(使用bash-preexec.sh
)
为了在预执行挂钩失败时使命令失败,我需要运行shopt -s extdebug
(当 trap DEBUG 命令返回非零 RC 时使命令失败)。
如果我shopt -s extdebug
在我的 shell 中以命令的形式运行,那么这可以很好地工作(预执行检查器只是在包含单词“FAIL”的文件中搜索“OK”,因此命令不应该执行):
> grep shopt .bashrc
> bash
bash-4.1$ echo TEST2
TEST2 <=== Expected, since I didn't turn on extdebug yet
bash-4.1$ shopt -s extdebug > /dev/null 2>&1
bash-4.1$ echo TEST2
bash-4.1$ # WHAT WAS EXPECTED. Command failed to execute.
但是,当我将命令添加到我的 .bashrc 时,它没有任何效果:
> grep shopt .bashrc
shopt -s extdebug > /dev/null 2>&1
> bash
bash: /usr/share/bashdb/bashdb-main.inc: No such file or directory
bash: warning: cannot start debugger; debugging mode disabled
bash-4.1$ echo TEST2
TEST2
我做错了什么,如何在 .bashrc 中执行它(我需要在用户登录时执行它)?
答案1
好的,找到答案了。
set -o functrace > /dev/null 2>&1
是必需的(除了shopt -s extdebug
),以使该功能在子 shell 中工作。并且不知何故,.bashrc
被视为实际交互式 shell 的超级 shell,从某种意义上说,如果您不添加它,它将不会在交互式命令提示符上生效。