将-x调试命令捕获到Bash中的文件中 [英] Capture -x debug commands into a file in Bash

查看:57
本文介绍了将-x调试命令捕获到Bash中的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using set -x in bash prints the shell-expanded commands to stderr. I would like to redirect these to a file or pipe. But not the whole output - only some commands. Something like:

set -x command.txt  ### <-- command.txt param is made up
echo $A $B
set +x

这会将调试输出放到命令中. txt.

This would put the debug output to command. txt.

可以做到吗?

推荐答案

bash 4.1或更高版本中:

With bash 4.1 or later:

#!/bin/bash

exec 5> command.txt
BASH_XTRACEFD="5"

echo -n "hello "

set -x
echo -n world
set +x

echo "!"

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

Output to stdout (FD 1):

hello world!

输出到command.txt(FD 5):

Output to command.txt (FD 5):

+ echo -n world
+ set +x

这篇关于将-x调试命令捕获到Bash中的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆