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
bash --redirect output to log file (with day date in the name)
提问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 +%D
are 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
.