Mod calculations and "bad number "

TwentyFourSeven
TwentyFourSeven used Ask the Experts™
on
MINUTE=`date +"%M"`
MINUTE_MOD=$((MINUTE%15))
]

Open in new window


Most of the time, the above works nicely.

Until you get to the start of the hour.....

08: bad number `08'
09: bad number `09'


Where am I going wrong ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

Commented:
Hi,
which shell dou you actually use? I assume it's not really bsh, or is it?
With bash your script should work fine, but if not I think getting rid of the leading zero could help -
MINUTE=`date +"%M"`
MINUTE=$[MINUTE##0}
MINUTE_MOD=$((MINUTE%15))
 With bsh you should do
MINUTE=`date +"%M"`
MINUTE_MOD=`expr MINUTE % 15`
wmp

 

Author

Commented:
>> which shell dou you actually use? I assume it's not really bsh, or is it?

#!/bin/sh
Most Valuable Expert 2013
Top Expert 2013

Commented:
/bin/sh is in most cases a  (hard-) link to the system's default shell.
I assume /bin/sh and /bin/bash are of exactly the same size?
Btw. I hit the wrong key here, sorry -
MINUTE=${MINUTE##0}
 

Author

Commented:
$ ls -la /bin/sh
-r-xr-xr-x  3 root  bin  336632 Mar 18 02:32 /bin/sh
$ openssl sha1 /bin/sh
SHA1(/bin/sh)= f9e4cef44ea02c0ce944ea530dae0a344e64de23
$ openssl sha1 /bin/bash
/bin/bash: No such file or directory

Author

Commented:
(Underlying OS is OpenBSD)
Most Valuable Expert 2013
Top Expert 2013
Commented:
OK,
OpenBSD has its own reimplementation of the Bourne shell, which I'm not really familiar with.
But nevertheless, my suggestion
MINUTE=${MINUTE##0}
should work in that shell.
wmp
 

Author

Commented:
Yeah..... OpenBSD is fun.... ;-)


$ cat test.sh                                                                                                                                                                                                   
#!/bin/sh

MINUTE=`date +"%M"` 
MINUTE=$[MINUTE##0}
MINUTE_MOD=$((MINUTE%15))


echo $MINUTE_MOD

$ sh test.sh                                                                                                                                                                                                    
test.sh[5]: $[MINUTE##0}: unexpected `$'

Open in new window

Most Valuable Expert 2013
Top Expert 2013

Commented:
Yep, that's due to my typo, which I corrected subsequently ...
MINUTE=${MINUTE##0}

Author

Commented:
oops..... sorry about that !

That works.

Give me 10 minutes or so until we get to the new hour for it to test ;-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial