erratic sh arithmetic
Heinrich Bjerregaard
heinrich at icloud.com
Fri Jul 10 06:25:00 UTC 2020
> Message: 9
> Date: Thu, 9 Jul 2020 21:21:26 -0600
> From: Gary Aitken <ubuntu at dreamchaser.org>
> To: ubuntu-users at lists.ubuntu.com
> Subject: erratic sh arithmetic
> Message-ID: <dc87b4c2-2f75-a72a-add3-b1c029d66059 at dreamchaser.org>
> Content-Type: text/plain; charset=utf-8; format=flowed
>
> 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
>
In sh and bash numbers with a leading zero is interpreted as an octal number.
The digits 8 and 9 are not valid for octal numbers so just remove the leading zeros…
/hebj
More information about the ubuntu-users
mailing list