加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 源文件如何继承陷阱DEBUG?

发布时间:2020-12-16 01:37:24 所属栏目:安全 来源:网络整理
导读:运行以下代码段: #!/bin/bashfunction preexec (){ echo -e "n--preexec command: $BASH_COMMAND"}trap 'preexec' DEBUGfunction testfunc (){ echo "testfunc called $1"}testfunc "main"source "source.sh"exit 0 其中source.sh是 #!/bin/bashtestfunc "
运行以下代码段:
#!/bin/bash

function preexec ()
{
    echo -e "n-->preexec command: $BASH_COMMAND"
}
trap 'preexec' DEBUG

function testfunc ()
{
    echo "testfunc called $1"
}

testfunc "main"
source "source.sh"

exit 0

其中source.sh是

#!/bin/bash

testfunc "source"

得到:

-->preexec command: testfunc "main"
testfunc called main

-->preexec command: source "source.sh"
testfunc called source

-->preexec command: exit 0

这意味着源文件中的每个命令都不会被DEBUG陷阱捕获.
事实上,如果我添加该行

trap 'preexec' DEBUG

在source.sh内部作为第二行,一切都按照需要工作(源文件中的命令也被捕获).

如何将此作为默认行为,以避免为我需要提供的任何文件重复上述行?
换句话说:有没有机会告诉源文件继承DEBUG陷阱?

我解决了主文件中的设置:
set -o functrace

来自BASH的人:

If set,any traps on DEBUG and RETURN are inherited by shell
functions,command substitutions,and commands executed in a subshell
environment. The DEBUG and RETURN traps are normally not inherited in
such cases.

作为’source’的shell函数,它们是继承的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读