[英]bash script copy output from set -x to file, called from within script rather than using tee
I am wondering if there is a way to write set -x
output to a file from within a script, rather than call a tee
from the command prompt. 我想知道是否有一种方法可以将set -x
输出从脚本内写入文件,而不是从命令提示符处调用tee
。
For example, I usually use myScript.sh 2>&1 | tee mylog.log
例如,我通常使用myScript.sh 2>&1 | tee mylog.log
myScript.sh 2>&1 | tee mylog.log
form the command prompt. 在命令提示符处myScript.sh 2>&1 | tee mylog.log
。 This copies the set -x as I expect to the log file. 如预期的那样,这会将设置-x复制到日志文件。
Is there a way to internalise this within myScript.sh
so I can set it as a flag to be turned off if I do not need to debug. 有没有一种方法可以在myScript.sh
进行内部myScript.sh
因此如果不需要调试,可以将其设置为要关闭的标志。 running only myScript.sh
from the command prompt. 从命令提示符仅运行myScript.sh
。
thx Art 艺术
Inside your script place this line at top: 在脚本中,将以下行放在顶部:
#!/bin/bash
[[ $1 == "debug" ]] && { exec 2>err.txt; set -x; }
# rest of the script
Now when you call your script as: 现在,当您以以下方式调用脚本时:
./myScript.sh debug
You will get a file created as err.txt
containing output of set -x
(with other error, if any) 您将获得一个名为err.txt
的文件, err.txt
包含set -x
输出(如果有其他错误,则有其他错误)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.