2014-08-31 18 views
8

在bash中使用set -x將shell-expanded命令打印到stderr。 我想重定向這些文件或管道。 但不是整個輸出 - 只有一些命令。 喜歡的東西:Linux Bash - 捕獲-x調試命令到文件中

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

這將使調試輸出命令。文本。

可以這樣做嗎?

+0

找到此,http://stackoverflow.com/questions/3173131/redirect-copy-of-stdout-to-log-file-from-within-bash-script-itself,只適用於標準輸出。 – 2014-08-31 15:36:02

回答

10

隨着bash 4.1或更高:

#!/bin/bash 

exec 5> command.txt 
BASH_XTRACEFD="5" 

echo -n "hello " 

set -x 
echo -n world 
set +x 

echo "!" 

輸出到stdout(FD 1):

hello world! 

輸出到command.txt(FD 5):

+ echo -n world 
+ set +x