bash 写入 .log 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15811420/
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 05:06:31  来源:igfitidea点击:

Write into .log file

linuxbash

提问by JohnDoeTheOne

I'm making a script and every time something is done I would like to write into my custom .log file. How do I do that?

我正在制作一个脚本,每次完成某些事情时,我都想写入我的自定义 .log 文件。我怎么做?

And in the end.. I'd just like to read it with Bash,.. do I just use cat?

最后..我只想用Bash阅读它,..我只使用cat吗?

Thanks.

谢谢。

回答by gsmaker

The simplest syntax I always use is 2>&1 | tee -a file_name.log.

我一直使用的最简单的语法是2>&1 | tee -a file_name.log.

The syntax can be used after a command or execution of a file. e.g.

该语法可在命令或文件执行后使用。例如

find . -type f 2>&1 | tee -a file_name.log

or

或者

./test.sh 2>&1 | tee -a file_name.log

回答by James

Just cat <log message here> >> custom.log.

只是cat <log message here> >> custom.log

The >>means add on to the bottom of the file rather than >which would delete the contents of the file and then write the message.

>>方法对添加到该文件,而不是底部>这将删除该文件的内容,然后写邮件。