【发布时间】:2021-06-23 04:36:49
【问题描述】:
我正在使用set -x(也称为set -o xtrace)在我的脚本中记录执行的命令及其参数。我发现将命令及其参数重定向到标准错误是有问题的。我需要 stderr 通道来仅存储真正的错误,这就是为什么使用 ./script.sh --arg ${ARG} 2>1 不是解决方案的原因。有没有办法重定向到 set -x 命令生成的 stdout 命令及其参数?
【问题讨论】:
我正在使用set -x(也称为set -o xtrace)在我的脚本中记录执行的命令及其参数。我发现将命令及其参数重定向到标准错误是有问题的。我需要 stderr 通道来仅存储真正的错误,这就是为什么使用 ./script.sh --arg ${ARG} 2>1 不是解决方案的原因。有没有办法重定向到 set -x 命令生成的 stdout 命令及其参数?
【问题讨论】:
您可以使用BASH_XTRACEFD 重定向跟踪输出。
BASH_XTRACEFD
If set to an integer corresponding to a valid file
descriptor, bash will write the trace output generated
when set -x is enabled to that file descriptor. The file
descriptor is closed when BASH_XTRACEFD is unset or
assigned a new value. Unsetting BASH_XTRACEFD or
assigning it the empty string causes the trace output to
be sent to the standard error. Note that setting
BASH_XTRACEFD to 2 (the standard error file descriptor)
and then unsetting it will result in the standard error
being closed.
类似:
#!/bin/bash
exec 10> /tmp/xtrace
export BASH_XTRACEFD=10
...
# rest of the script
【讨论】:
export BASH_XTRACEFD=1,它将打印到标准输出