Command syntax error in cron

SCO OSR 507, user's shell is old Bourne sh
The same command is OK when run using now, but fails when run in cron, see below:

1) using now, see how it worked and I see resulting DT=2018 in the mail

 
 $ at now
{ dt=`/usr/gnu/bin/date '+%Y'`; echo "DT=$dt"; }
job 1522867418.a-6605:0 at Wed Apr  4 11:43:38 2018
$ mail
SCO OpenServer Mail Release 5.0.7  Type ? for help.
"/usr/spool/mail/test": 1 message 1 new
>N  1 cron@test.testdo Wed Apr  4 19:43   19/639   Output of one of your cron 
& 
Message  1:
From cron Wed Apr  4 18:43:43 2018
Return-Path: <cron>
... snipped for brevity ...
Subject: Output of one of your cron jobs
Date: Wed, 4 Apr 2018 11:43:42 -0700 (PDT)
Status: R
 DT=2018
 

2) using cron

 
 $ crontab -l
 44 11 * * * { dt=`/usr/gnu/bin/date '+%Y'`; echo "DT=$dt"; }
 $ mail
 $ mail
SCO OpenServer Mail Release 5.0.7  Type ? for help.
"/usr/spool/mail/test": 1 message 1 new
>N  1 cron@test.testdo Wed Apr  4 19:44   19/696   Output of one of your cron 
& 
Message  1:
From cron Wed Apr  4 18:44:01 2018
Return-Path: <cron>
Received: (from cron@localhost)
...
Subject: Output of one of your cron jobs
Date: Wed, 4 Apr 2018 11:44:00 -0700 (PDT)
Status: R
 /bin/sh: syntax error at line 1: `end of file' unexpected
 
*************************************************
Cron: The previous message is the standard output and standard error
     of the following cron job, executed on your behalf:
{ dt=`/usr/gnu/bin/date '+
& 
 

Please let me know what can be the problem here
Thanks in advance

The % character is magic in crontab, one needs to backslash-escape it.
Within ' ' one needs to put the \% outside, for example

echo 'this is a % sign'

must become

echo 'this is a '\%' sign'

in order to work with crontab (and still work outside crontab).
Here is no need to escape the + and Y, so simply omit the ' ' and have \%

44 11 * * * { dt=`/usr/gnu/bin/date +\%Y`; echo "DT=$dt"; }

(And there is no need for the { } braces.)

1 Like

Thank you, I did not know about cron treating % as a special character