gpt4 book ai didi

bash - 在没有变量扩展的情况下在 bash 中打印命令的踪迹

转载 作者:行者123 更新时间:2023-11-29 09:35:07 27 4
gpt4 key购买 nike

我有一个脚本,我正在使用 set -x 打印命令执行时的踪迹。

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.

有没有办法获得这种行为,但打印变量扩展?我正在捕获日志输出并将其发送到集中式记录器,但我不希望输出某些敏感变量的值。

最佳答案

您可以像这样将陷阱 DEBUG 与 $BASH_COMMAND 一起使用

#!/bin/bash

set -T

trap 'echo "$PS4$BASH_COMMAND"' DEBUG

i=1234

echo "$i"

(echo $i)

这将打印

+ i=1234
+ echo "$i"
1234
+ echo $i
1234

BASH_COMMAND
The command currently being executed or about to be executed, unless the shell is executing a command as the result of a trap, in which case it is the command executing at the time of the trap.

-T
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.

trap
If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

关于bash - 在没有变量扩展的情况下在 bash 中打印命令的踪迹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com