Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

linux - 如何将输出重定向到文件和标准输出(How to redirect output to a file and stdout)

In bash, calling foo would display any output from that command on the stdout.

(在bash中,调用foo将在stdout上显示该命令的所有输出。)

Calling foo > output would redirect any output from that command to the file specified (in this case 'output').

(调用foo > output会将该命令的所有输出重定向到指定的文件(在本例中为“ output”)。)

Is there a way to redirect output to a file and have it display on stdout?

(有没有一种方法可以将输出重定向到文件在stdout上显示?)

  ask by SCdF translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The command you want is named tee :

(您想要的命令名为tee :)

foo | tee output.file

For example, if you only care about stdout:

(例如,如果您只关心标准输出:)

ls -a | tee output.file

If you want to include stderr, do:

(如果要包括stderr,请执行以下操作:)

program [arguments...] 2>&1 | tee outfile

2>&1 redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout.

(2>&1将通道2(stderr /标准错误)重定向到通道1(stdout /标准输出),以便将两者都写为stdout。)

It is also directed to the given output file as of the tee command.

(从tee命令开始,它也被定向到给定的输出文件。)

Furthermore, if you want to append to the log file, use tee -a as:

(此外,如果要追加到日志文件,请使用tee -a作为:)

program [arguments...] 2>&1 | tee -a outfile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...