Reputation: 649
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.
Upvotes: 2
Views: 664
Reputation: 8164
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 -
printf
instead of echo
to output the crontab entry.date '+\%a'
produces output like \Sun
(for some versions of the date
program). You probably want date '+%a'
.date '+\%a'
is necessary because the command is in a crontab
.)%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 ]
).==
with [...]
is not portable between shells. Use =
unless you are sure that the crontab entries will only be run with Bash.Upvotes: 1
Reputation: 785481
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.
Upvotes: 2