将 shell 脚本中的 echo 输出重定向到日志文件 [英] Redirect echo output in shell script to logfile
本文介绍了将 shell 脚本中的 echo 输出重定向到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含很多 echo
的 shell 脚本.我想将输出重定向到日志文件.我知道有命令调用 cmd >logfile.txt
,或者在文件 echo 'xy' > 中做logfile.txt
,但是否可以简单地在脚本中设置文件名,然后自动将所有回显写入该文件?
I have a shell script with lots of echo
in it. I would like to redirect the output to a logfile.
I know there is the command call cmd > logfile.txt
, or to do it in the file echo 'xy' > logfile.txt
, but is it possible to simply set the filename in the script which then automatically writes all echo's to this file?
推荐答案
您可以在脚本顶部添加以下行:
You can add this line on top of your script:
#!/bin/bash
# redirect stdout/stderr to a file
exec >logfile.txt 2>&1
OR else 仅重定向 stdout 使用:
OR else to redirect only stdout use:
exec > logfile.txt
这篇关于将 shell 脚本中的 echo 输出重定向到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文