I got rather bored so i decided to create a script that creates a countdown, and shows hours:minutes:seconds till that time.
It works fine until the seconds of the actual time reaches 8, then it tries to use it to work out the difference as in "SECONDDIFF=$[SECOND-ALARMSECOND]"
Here's my code where I get the problem :
ALARMHOUR=`echo "$ALARMTIME" | cut -d : -f 1 `
ALARMMINUTE=`echo "$ALARMTIME" | cut -d : -f 2 `
ALARMSECOND=`echo "$ALARMTIME" | cut -d : -f 3 `
...
TIME=`date "+%H:%M:%S"`
HOUR=`echo "$TIME" | cut -d : -f 1 `
MINUTE=`echo "$TIME" | cut -d : -f 2 `
SECOND=`echo "$TIME" | cut -d : -f 3 `
SECONDDIFF=$[SECOND-ALARMSECOND]
SECONDDIFF=$[60-SECONDDIFF]
The line i get the error at is SECONDDIFF=$[SECOND-ALARMSECOND] and
The error i'm getting is :
/bin/TermTime (unstable): line 35: 08: value too great for base (error token is "08")
I simply cannot even guess how to fix this, i simply have no idea, Help me please someone?
What shell are you using?
There's several ways better than running cut 3 times to process one line.
A way that'll work in all shells:
VAR="12:34:56"
OLDIFS="$IFS"
IFS=":"; set -- $VAR; IFS="$OLDIFS"
HH=$1
MM=$2
SS=$3
This will overwrite your $1,$2,$3 parameters.
Other ways may be available depending what your shell is.
1 Like
I'm using BASH... On a mac running lion if that helps
It does.
Try X=$((Y-Z)) instead of X=$[Y-Z].
BASH can also do this:
read A B C <<<$(date +"%H %M %S")
Hundreds of times faster than running three external utilities to process one line.
or:
#!/bin/ksh
eval "$( date '+ h=%H =%H m=%M s=%S' )"
echo $h $m $s
Thank you for the help speeding up the execution, however I only posted for the problem i was getting... Any idea what it is and how to sort it out?
Check my last post. I answer your question directly and tell you how to fix it.
Oh ok thank you, I didn't see that bit... somehow.. Thank you
---------- Post updated at 06:41 PM ---------- Previous update was at 06:36 PM ----------
So @Corona, Whats the difference between '[' and '(('?
Just tested it, it hasn't fixed the problem... And i thought it had
$(( is BASH arithmetic syntax, $[ isn't. I don't regognize this $[ syntax at all, for any language I know.
If it doesn't work, please post what you tried and what happened.
This is the body of the code i use (been updated with the '((' that you recomended)
ALARMHOUR=`echo "$ALARMTIME" | cut -d : -f 1 `
ALARMMINUTE=`echo "$ALARMTIME" | cut -d : -f 2 `
ALARMSECOND=`echo "$ALARMTIME" | cut -d : -f 3 `
for (( ;; )) do
clear
TIME=`date "+%H:%M:%S"`
read HOUR MINUTE SECOND <<<$(date +"%H %M %S")
SECONDDIFF=$((SECOND-ALARMSECOND))
SECONDDIFF=$((60-SECONDDIFF))
if [ $SECONDDIFF -lt 10 ]; then
SECONDDIFF="0$SECONDDIFF"
fi
MINUTEDIFF=$(($MINUTE-$ALARMMINUTE))
MINUTEDIFF=$((59-MINUTEDIFF))
if [ $MINUTEDIFF -lt 10 ]; then
MINUTEDIFF ="0$MINUTEDIFF"
fi
HOURDIFF=$(($HOUR-$ALARMHOUR))
if [ $HOURDIFF -lt 0 ]; then
HOURDIFF=$((HOURDIFF-HOURDIFF-HOURDIFF))
elif [ $HOURDIFF -gt 0 ]; then
HOURDIFF=$((24-HOURDIFF))
fi
if [ $HOURDIFF -lt 10 ]; then
HOURDIFF="0$HOURDIFF"
fi
echo "Current time : $HOUR:$MINUTE:$SECOND"
echo ""
echo " Alarm time : $ALARMTIME"
echo ""
echo " Difference : $HOURDIFF:$MINUTEDIFF:$SECONDDIFF"
...
sleep 1
done
It cycles through that, updating every second, but when SECONDS reaches 08, it exits with the following error :
/bin/TermTime (unstable): line 33: 08: value too great for base (error token is "08")
I have tried setting the SECONDS variable in the form :
if [ $SECONDS = 8 ]; then
SECONDS=8
fi
Didn't expect it to work and it didn't, but i thought i would try it
The strange thing is that i don't think it does this with MINUTES or HOURS, just SECONDS...
What do you suggest?
Ah -- BASH assumes numbers beginning with 0 are octal.
Do
STRING="${STRING#0}"
to strip off the leading zero when present. Do this for hours, minutes, and seconds.
Also:
IFS=":" read ALARMHOUR ALARMMINUTE ALARMSECOND <<<"$ALARMTIME"
will be hundreds of times faster than running cut three times to process one string...
---------- Post updated at 01:34 PM ---------- Previous update was at 01:30 PM ----------
Also: Your TIME variable doesn't appear to do anything anymore.
1 Like
Thank you, just waiting for it to come round and...It works. Thank you so much, this has been bugging me since last night sometime...
My final question is : How can i stop it from saying "Current time : 19:44:1"
and make it say "Current time : 19:44:01" without screwing it all up?
Never mind, Thank you so much Corona, you've been a great help
printf "Current time: %02d:%02d:%02d\n" $HH $MM $SS