bash - 重定向 set -x / set -o xtrace 到标准输出
问题描述
我正在使用set -x
(也称为set -o xtrace
)在执行的脚本中记录命令及其参数。我发现将命令及其参数重定向到标准错误是有问题的。我需要 stderr 通道来存储真正的错误,这就是为什么 using./script.sh --arg ${ARG} 2>1
不是解决方案。有没有办法重定向到标准输出命令及其由set -x
命令生成的参数?
解决方案
您可以使用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
推荐阅读
- excel - 基于其他组合框Excel VBA的依赖组合框列表
- android - 无法在嵌套视图中使用数据绑定
- hibernate - Hibernate 为相同的代码生成不同的查询
- driver - 在我的笔记本电脑中更新图形驱动程序会导致黑屏?
- c - 是什么导致此堆缓冲区溢出?
- kotlin - 流动
.distinctUntilChanged() 不起作用 - c++ - 融合 IMU 和 Pressure_height 时,来自 hector_localization 堆栈的 Velocity 发布的主题不正确
- git - 修改远程仓库中的 .git 文件夹
- expect - 在 linux expect 命令中跳过迭代
- sql - 如何使用名称中带有点的大量数据库?