Results 1 to 2 of 2

Thread: Interval processing script, interesting problem...

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Interval processing script, interesting problem...

    Hi

    I need to execute a function on set intervals, in seconds.
    Came up with this:
    Code:
    #!/bin/bash
    INTERVAL=2
    while true; do
    	SECOND=`date +"%S"`
    	(($SECOND % $INTERVAL)) || echo "execute on second: $SECOND"
    	sleep 1	
    done
    Basically execution should happen if the current second mod the interval is equal to zero.

    Sample output:
    execute on second: 00
    execute on second: 02
    execute on second: 04
    execute on second: 06
    ./timer.sh: line 4: ((: 08: value too great for base (error token is "08")
    execute on second: 08
    ./timer.sh: line 4: ((: 09: value too great for base (error token is "09")
    execute on second: 09
    execute on second: 10
    execute on second: 12
    execute on second: 14
    execute on second: 16
    execute on second: 18
    execute on second: 20
    execute on second: 22
    execute on second: 24

    Anybody knows what is happening there on second 8 and 9?

    Tested on CentOs and OSX.

  2. #2
    Junior Member
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default solved ;-)

    Seconds is 01, 02 ,03 etc...

    Apparently when a number has a leading zero, c sees it as an octal number.

    This is the fix:
    Code:
    #!/bin/bash
    INTERVAL=2
    while true; do
    	SECOND=`date +"%S"`
    	((10#$SECOND % $INTERVAL)) || echo "execute on second: $SECOND"
    	sleep 1	
    done
    That tells bash that seconds is base 10

    Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem in script
    By sania in forum Getting started tutorials
    Replies: 0
    Last Post: 14th September 2007, 04:25 PM
  2. problem with System + MySQL backup script
    By massoo in forum Shell scripting
    Replies: 1
    Last Post: 20th August 2007, 09:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •