将-x调试命令捕获到Bash中的文件中

Ond*_*žka 15 debugging bash

set -x在bash中使用将shell扩展命令打印到stderr.我想将它们重定向到文件或管道.但不是整个输出 - 只有一些命令.就像是:

set -x command.txt  ### <-- command.txt param is made up
echo $A $B
set +x
Run Code Online (Sandbox Code Playgroud)

这会将调试输出发送到命令.文本.

可以这样做吗?

Cyr*_*rus 19

随着bash4.1或更高版本:

#!/bin/bash

exec 5> command.txt
BASH_XTRACEFD="5"

echo -n "hello "

set -x
echo -n world
set +x

echo "!"
Run Code Online (Sandbox Code Playgroud)

输出到标准输出(FD 1):

hello world!
Run Code Online (Sandbox Code Playgroud)

输出到command.txt(FD 5):

+ echo -n world
+ set +x
Run Code Online (Sandbox Code Playgroud)

  • @activedecay:*如果您有权使用记录器命令,则可以使用[this](https://unix.stackexchange.com/a/155553/74329)通过带有时间戳,脚本名称和行号的系统日志来编写调试输出。* (2认同)