重定向bash脚本所有输出使用set -x时 [英] redirect all output in a bash script when using set -x
本文介绍了重定向bash脚本所有输出使用set -x时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有了设置-x
中有一个bash脚本。是否有可能重定向这个脚本和它的所有输出到文件的调试打印?理想情况下,我想这样做的:
I have a bash script that has set -x
in it. Is it possible to redirect the debug prints of this script and all its output to a file? Ideally I would like to do something like this:
#!/bin/bash
set -x
(some magic command here...) > /tmp/mylog
echo "test"
和取得
+ echo test
test
在/ tmp / mylog,而不是在标准输出输出。
output in /tmp/mylog, not in stdout.
推荐答案
这是我刚才一派,我记得我用这个前一段时间...
This is what I've just googled and I remember myself using this some time ago...
使用exec来重定向都在脚本的所有命令的标准输出和标准错误:
Use exec to redirect both standard output and standard error of all commands in a script:
#!/bin/bash
logfile=$$.log
exec > $logfile 2>&1
有关多个重定向魔术退房高级Bash脚本编程指南 - I / O重定向 :)
For more redirection magic check out Advanced Bash Scripting Guide - I/O Redirection :)
这篇关于重定向bash脚本所有输出使用set -x时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文