bash --redirect 输出到日志文件(名称中包含日期)

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

bash --redirect output to log file (with day date in the name)

linuxbashlogging

提问by Orsius

I just want to to redirect my script output to a file, to do so I try to redirect the output of a simple command and it works for a particular syntax and not for the other. Could you please have a look and give me any advice, Thanks in advance. Rgds, O.

我只想将我的脚本输出重定向到一个文件,为此我尝试重定向一个简单命令的输出,它适用于特定语法而不适用于其他语法。能否请您看看并给我任何建议,在此先感谢。罗格斯,O.

This one is working fine:

这个工作正常:

du -h > "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"

and this doesn't:

而这不会:

du -h > /var/log/mytst."$(date +"%D--%H:%M:%S")".log 2>&1

Any idea?

任何的想法?

回答by Marcos Dione

Look at this output:

看看这个输出:

$ echo "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"
/var/log/mytst.2014-08-11_13:54.log
$ echo /var/log/mytst."$(date +"%D--%H:%M:%S")".log
/var/log/mytst.08/11/14--13:54:00.log

The second one spects a tree hierarchy, as the /'s in the output of date +%Dare treated as directory separators. Your question does not state howthe second version 'does not work', so I would bet the error is No such file or directory.

第二个是树层次结构,因为/的输出中的date +%D被视为目录分隔符。你的问题没有说明第二个版本如何“不起作用”,所以我敢打赌错误是No such file or directory.