Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: BASH: How to repeat an action every 2 seconds?

  1. #1
    Join Date
    May 2007
    Location
    Norway
    Beans
    Hidden!
    Distro
    Gutsy Gibbon Testing

    Lightbulb BASH: How to repeat an action every 2 seconds?

    Hi there,

    How do I repeat something for example every 2 seconds? Let's say I'd want to echo 'lol' every two seconds, how should I do that? Thanks!
    You only know one thing for sure.

  2. #2
    Join Date
    Apr 2007
    Location
    Norway
    Beans
    30
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: BASH: How to repeat an action every 2 seconds?

    Quote Originally Posted by loathsome View Post
    Hi there,

    How do I repeat something for example every 2 seconds? Let's say I'd want to echo 'lol' every two seconds, how should I do that? Thanks!
    Code:
    while [ "true" ]
    do
      echo "lol"
      sleep 2
    done

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: BASH: How to repeat an action every 2 seconds?

    man sleep

  4. #4
    Join Date
    May 2007
    Location
    Norway
    Beans
    Hidden!
    Distro
    Gutsy Gibbon Testing

    Re: BASH: How to repeat an action every 2 seconds?

    Thanks a bunch.

    I've got a lot of PHP experience, but bash is all new to me
    You only know one thing for sure.

  5. #5
    Join Date
    May 2007
    Location
    Norway
    Beans
    Hidden!
    Distro
    Gutsy Gibbon Testing

    Re: BASH: How to repeat an action every 2 seconds?

    Also, how you do do something equivalent to this?

    Code:
    do
        blabla
        $variable++;
    done
    Increase a numeric variable by one each time the loop runs.
    You only know one thing for sure.

  6. #6
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: BASH: How to repeat an action every 2 seconds?

    Here is one way:
    Code:
    let x=0
    while [ "true" ]
    do
        echo "lol($x)"
        let ++x
        sleep 1
    done

  7. #7
    Join Date
    May 2007
    Location
    Norway
    Beans
    Hidden!
    Distro
    Gutsy Gibbon Testing

    Re: BASH: How to repeat an action every 2 seconds?

    Quote Originally Posted by WW View Post
    Here is one way:
    Code:
    let x=0
    while [ "true" ]
    do
        echo "lol($x)"
        let ++x
        sleep 1
    done
    Thanks, worked flawlessly!
    You only know one thing for sure.

  8. #8
    Join Date
    Nov 2008
    Beans
    5

    Question Re: BASH: How to repeat an action every 2 seconds?

    I have a very similar question... Is there a way to do this without the sleep function?

    I'm pretty much looking for this converted to code: 'if it has been 2 seconds'



    Any help would be greatly appreciated =)
    Last edited by ninja23; November 5th, 2008 at 01:25 PM.

  9. #9

    Re: BASH: How to repeat an action every 2 seconds?

    the only way I can think of is using date +%S to get the current second. If you create a loop that constantly does date +%S and compared it to the last baselined second, you would find 2 seconds... I think that is unnecessary processing though...

    Code:
    sec=`date +%S`
    
    while [1]; do 
        sec1=`date +%S`
        (( amt_slept = $sec - $sec1 ))
    
        if [ $amt_slept -eq 2 ]; then 
             <EXECUTE CODE>
             sec=`date +%S`
        fi
    
    
    done

  10. #10
    Join Date
    Nov 2008
    Beans
    5

    Re: BASH: How to repeat an action every 2 seconds?

    Quote Originally Posted by reality1011 View Post
    the only way I can think of is using date +%S to get the current second. If you create a loop that constantly does date +%S and compared it to the last baselined second, you would find 2 seconds... I think that is unnecessary processing though...

    Code:
    sec=`date +%S`
    
    while [1]; do 
        sec1=`date +%S`
        (( amt_slept = $sec - $sec1 ))
    
        if [ $amt_slept -eq 2 ]; then 
             <EXECUTE CODE>
             sec=`date +%S`
        fi
    
    
    done
    I can't get it to work... Could it be written as 'if it has been 2 seconds' the 'if' ?

    Thanks =)

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

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