crontab 中的 % 有何特殊之处? [英] How is % special in crontab?

查看:23
本文介绍了crontab 中的 % 有何特殊之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 crontab 中,你能做这样的事情吗?

In crontab, can you do something like this?

* * * * * echo $( date +%F) >> /path/date.txt

推荐答案

您的 crontab 行的实际问题不是 $() 或反引号.问题是百分号%.它在 crontabs 中有特殊的含义.

The actual problem of your crontab line is not the $() or the backquotes. The problem is the percent sign %. It has a special meaning in crontabs.

来自手册页:

...
Percent-signs (%) in the command, unless escaped with backslash (), 
will be changed into newline characters, and all data after the 
first % will be sent to the command  as standard input.
...

如果你用 转义百分号,它应该会按预期工作:

If you escape the percent sign with it should work as expected:

* * * * * echo $(date +\%F) >> /tmp/date.txt

* * * * * echo `date +\%F` >> /tmp/date2.txt

两者都在我的网站上工作.

both work on my site.

这篇关于crontab 中的 % 有何特殊之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆