Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How do I loop a bash script?

  1. #1
    Join Date
    Jul 2012
    Beans
    123

    How do I loop a bash script?

    Let's say I were to make a backup script for a certain file or directory, is there a command I can put at the end of the script that tells the script to loop?

  2. #2
    Join Date
    Apr 2006
    Beans
    800
    Distro
    Lubuntu 12.04 Precise Pangolin

    Re: How do I loop a bash script?

    It sounds like you want to use cron, see https://help.ubuntu.com/community/CronHowto

  3. #3
    Join Date
    Sep 2006
    Beans
    7,294
    Distro
    Lubuntu Development Release

    Re: How do I loop a bash script?

    For repeating the script, cron is the way to go.

    If you really want a loop and continuous execution, you could put a while loop in the script itself.

  4. #4
    Join Date
    Jul 2012
    Beans
    123

    Re: How do I loop a bash script?

    So basically putting 'done' at the end loops it?

  5. #5
    Join Date
    May 2008
    Location
    SoCal
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do I loop a bash script?

    Quote Originally Posted by linuxvstheworld View Post
    So basically putting 'done' at the end loops it?
    If you want to create a loop in the script you should do this.
    -BAB1

  6. #6
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: How do I loop a bash script?

    I think there's some confusion here. If looping a script (repeating an element of it over and over until a condition is met) is really want you want to do, then you might have a look into something like 'while', 'until', or 'for'. See here (half way down) for more info:

    http://mywiki.wooledge.org/BashGuide...ndConditionals

    If you want to repeat the whole script at certain times (which is what it seems like based on what you say the script is doing), then something like 'cron' or some other scheduler might be the way to go. The 'done' statement just ends a 'do' statement (often in conjunction with a loop):

    Code:
     while <condition>; do <something>; done
    Perhaps a more specific example of your script and / or what you want to do with it would be helpful.

  7. #7
    Join Date
    Feb 2005
    Location
    Melbourne, Australia
    Beans
    13,505
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How do I loop a bash script?

    Quote Originally Posted by linuxvstheworld View Post
    ...........
    is there a command I can put at the end of the script that tells the script to loop?
    Code:
    exec $0
    Regards, David.
    Please use the Forum search and Wiki search for immediate help
    Please mark your thread as Solved when appropriate
    New to technical forums?: How To Ask Questions The Smart Way

  8. #8
    Join Date
    Jul 2012
    Beans
    123

    Re: How do I loop a bash script?

    Quote Originally Posted by dcstar View Post
    Code:
    exec $0
    I just made a test script doing echo test and put the command you mentioned and it worked! I'll try out other methods also
    that were suggested here, thanks!

  9. #9
    Join Date
    Jul 2012
    Beans
    123

    Re: How do I loop a bash script?

    Is there a command like

    exec $0

    That makes it loop a certain amount of times?

  10. #10
    Join Date
    Sep 2006
    Beans
    7,294
    Distro
    Lubuntu Development Release

    Re: How do I loop a bash script?

    To loop it a set number of times, you can use conditionals like while or for inside the script itself. That will not mix with exec, though.

Page 1 of 2 12 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
  •