我想将流程的标准输出和标准错误重定向到单个文件。我在巴什怎么做?
当前回答
bash your_script.sh 1>file.log 2>&1
1> log指示shell将标准输出发送到file.log文件,2>&1告诉它将标准错误(文件描述符2)重定向到标准输出(文件描述符1)。
注意:正如liw.fi指出的那样,顺序很重要,2>&11>file.log不起作用。
其他回答
奇怪的是,这是有效的:
yourcommand &> filename
但这会产生语法错误:
yourcommand &>> filename
syntax error near unexpected token `>'
您必须使用:
yourcommand 1>> filename 2>&1
bash your_script.sh 1>file.log 2>&1
1> log指示shell将标准输出发送到file.log文件,2>&1告诉它将标准错误(文件描述符2)重定向到标准输出(文件描述符1)。
注意:正如liw.fi指出的那样,顺序很重要,2>&11>file.log不起作用。
do_something 2>&1 | tee -a some_file
这会将标准错误重定向到标准输出,将标准输出重定向到some_file并将其打印到标准输出。
在考虑使用exec2>&1之类的情况下,如果可能的话,我发现使用如下Bash函数重写代码更容易阅读:
function myfunc(){
[...]
}
myfunc &>mylog.log
“最简单”的方法(仅限Bash 4):
ls * 2>&- 1>&-