Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ Modulus Operator

Posted by: etangman Jun 12 2008, 06:55 AM

Alright, I am attempting to add records to a database, but I need them to be divisible by 30 so that I can update them concurrently.
In order to do this I intended to use `date +%s` to get the current time in seconds, and then add 1 until the number was divisible by 30.
To simplify things I made $now equal to a number I knew was divisible by 30 for testing, but the modulus operator doesn't work.
I'm assuming my syntax is wrong, but everything I've looked at makes it look right to me. The increment operator wouldn't work either...
Any help would be much appreciated because I'm at a standstill with this nonsense. Thanks.

#now=`date +%s`
now=1213204110
tmp=0

if [ $now%30 != 0 ]
then
while [ $now%30 != 0 ]
do
tmp=`expr $now + 1`
now=$tmp
done
fi

echo $now

Posted by: michaelk Jun 13 2008, 05:58 PM

Lots of help can be found on bash shell scripts by googling. However, here is one method to accomplish the task.

CODE
now=`date +%s`
#let now=1213204110
divisor=30

let "remainder = $now % $divisor"

until [ "$remainder" == 0 ]
do
   let "now +=1"
   let "remainder = $now % $divisor"
   echo $now
done

echo $now is divisible by 30

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)