将 bash 脚本的输出写入带有日期的文件

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

Write output of bash script to file with date

linuxbashconsole

提问by Devin Dixon

I'm trying to create a log file of a bash script that is run with the data. I currently have this:

我正在尝试创建一个使用数据运行的 bash 脚本的日志文件。我目前有这个:

bash script.sh > /var/log/%Y-%m-%d_%H:%M.log

The problem is the log file actual writes %Y-%m-%d_%H:%M and not the date. Is there way to get the date to actually write out the date by only running it in the console?

问题是日志文件实际写入 %Y-%m-%d_%H:%M 而不是日期。有没有办法通过只在控制台中运行来获取实际写出日期的日期?

回答by Blender

Pass that string to date:

将该字符串传递给date

bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"