逃避双人双语和单句话

发布于 2025-01-20 22:24:03 字数 412 浏览 1 评论 0原文

我有一个 Cron 作业,每季度在每月的第一个星期日运行:

0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh

现在我想使用 echo 自动安装它,例如:

(crontab -l ; echo "0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh") | crontab -

虽然转义 \"Sun"\ 工作正常,但我无法转义 "$(date '+\%a')" 因为这是双引号 echo 命令中单引号和双引号与 \ 的混合。

I have a Cron Job running quarterly on every first Sunday of the Month:

0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh

Now I want to install it automatically using echo like:

(crontab -l ; echo "0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh") | crontab -

while escaping \"Sun"\ works fine I can't escape "$(date '+\%a')" as this is a mix of single and double quotes with \ within double quoted echo command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2025-01-27 22:24:03

你不需要使用疯狂的逃跑。使用 bash 提供的 [[ ... ]] 而不是旧的(且容易出错)[ ... ]

使用:

0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh

所以 要自动安装它,请使用:

(crontab -l ; echo '0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh') | crontab -

PS:请确保在 crontab 文件中的该行之前设置 SHELL=/bin/bash

You don't need to use crazy escaping. Use [[ ... ]] provided by bash instead of old (and error prone) [ ... ]:

So use:

0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh

And to install it automatically use:

(crontab -l ; echo '0 0 1-7 3,6,9,12 * [[ $(date +\%a) == Sun ]] && /script.sh') | crontab -

PS: Make sure to set SHELL=/bin/bash in the crontab file before this line.

寻找一个思念的角度 2025-01-27 22:24:03

您可以使用此处文档来避免引用 crontab 条目:

read -r crontab_entry <<'END_CRONTAB_ENTRY'
0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh
END_CRONTAB_ENTRY

(crontab -l; printf '%s\n' "$crontab_entry") | crontab -
  • 请参阅 为什么 printf 比 echo 更好? 的已接受且优秀的答案以获取解释为什么我使用 printf 而不是 echo 来输出 crontab 条目。
  • 请注意,date '+\%a' 生成类似于 \Sun 的输出(对于 date 程序的某些版本)。您可能需要日期“+%a”
    更新:戈登·戴维森 (Gordon Davisson) 的评论解释了 date 中的反斜杠'+\%a' 是必需的,因为该命令位于 crontab 中。)
  • 另请注意,%a 日期格式生成的字符串取决于语言环境。它可能不是周日的 Sun。使用数字日期更安全(例如 [ "$(date '+%w')" -eq 0 ])。
  • 最后,请注意,将 ==[...] 一起使用在 shell 之间不可移植。使用 = 除非您确定 crontab 条目只能使用 Bash 运行。

You could use a here document to avoid having to quote the crontab entry:

read -r crontab_entry <<'END_CRONTAB_ENTRY'
0 0 1-7 3,6,9,12 * [ "$(date '+\%a')" == "Sun" ] && /script.sh
END_CRONTAB_ENTRY

(crontab -l; printf '%s\n' "$crontab_entry") | crontab -
  • See the accepted, and excellent, answer to Why is printf better than echo? for an explanation of why I used printf instead of echo to output the crontab entry.
  • Note that date '+\%a' produces output like \Sun (for some versions of the date program). You probably want date '+%a'.
    (Update: the comment by Gordon Davisson explains that the backslash in date '+\%a' is necessary because the command is in a crontab.)
  • Note also that the string produced by the %a date format depends on the locale. It could be something other than Sun for Sunday. It's safer to use a numeric day (e.g. [ "$(date '+%w')" -eq 0 ]).
  • Finally, note that using == with [...] is not portable between shells. Use = unless you are sure that the crontab entries will only be run with Bash.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文