erratic sh arithmetic

Gary Aitken ubuntu at dreamchaser.org
Fri Jul 10 03:21:26 UTC 2020


I'm seeing crazy erratic behavior in sh arithmetic executing in
a cron script as root:

I've had a cron job running for several weeks now with the
following statement:

week=$(( ( $day + 6 - $dow ) / 7 + 1 ))

where
  day is 01-31
  dow is 1-7

This morning, however, the script did not run properly and
failed:

arithmetic expression: expecting ')': " ( 08 + 6 - 3 ) / 7 + 1 "

replacing the expression with the following works, at least when
executing from a login shell:

week=`expr \( $day + 6 - $dow \) / 7 + 1`

I understand that traditional sh required the use of "expr" to evaluate
arithmetic.  What I don't understand is why it works *sometimes*
when running as a cron job.  The crontab entry looks like:
... root    /root/scripts/backup-scheduled-tar.sh

This is a gcloud compute engine system running 16.04.1-Ubuntu.
The system has been up 15 days and the change in behavior
occurred during that time -- the script worked two days ago,
but did not work last night.

The script executes as root; root's default shell is /bin/bash,
but the script starts with #!/bin/sh.

Any ideas as to what may be going on?

Gary




More information about the ubuntu-users mailing list