bash 将调试和标准输出重定向到不同的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19208716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:11:23  来源:igfitidea点击:

Redirect debug and STDOUT to different files

linuxbashshellredirect

提问by Sriharsha Kalluru

Is there a way to redirect my debug output to a file and normal STDOUT on screen.

有没有办法将我的调试输出重定向到文件和屏幕上的正常 STDOUT。

$ cat test1.sh
#!/bin/bash

echo HAI
echo BYE

$ sh -x test1.sh
+ echo HAI
HAI
+ echo BYE
BYE

I want to redirect the debug output to file and normal output on the screen.

我想将调试输出重定向到文件和屏幕上的正常输出。

$ sh -x test1.sh >file1
+ echo HAI
+ echo BYE

But I can redirect the output to a file and ending the debug output on screen.

但是我可以将输出重定向到一个文件并在屏幕上结束调试输出。

采纳答案by Sriharsha Kalluru

I used alias to give the complete command and using the following way

我使用别名来给出完整的命令并使用以下方式

$ alias mytest='sh -x test1.sh 2>out.test'
$ mytest
HAI
BYE
$ cat out.test
+ echo HAI
+ echo BYE

Thanks every one..

谢谢大家..

回答by

Use 2>:

使用2>

$ sh -x test1.sh 2> /dev/null
HAI
BYE

See https://stackoverflow.com/a/818284/1907906

https://stackoverflow.com/a/818284/1907906

回答by user1502952

Default file descriptors on linux system are:

linux系统上的默认文件描述符是:

stdin - 0
stdout - 1
stderr - 2

to redirect to file,

重定向到文件,

sh test1.sh 1> file

to redirect error(stderr) to console(stdout)

将错误(stderr)重定向到控制台(stdout)

sh test1.sh 2>&1