Help - Search - Members - Calendar
Full Version: Modulus Operator
Linuxhelp > Support > Programming in Linux
etangman
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
michaelk
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.