LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2010, 02:53 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Rep: Reputation: 51
Bash: how do I write an infinite loop?


Hi:
I want to do a Bash script that does only one thing: to execute program foo for ever and ever. I know I can use 'while' and 'until'. In a pseudo code of mine it'd be
Code:
while TRUE
    foo
# TRUE could be '1 == 1' which, obviously, is always true.
done;
Any hint would be welcome. Thanks.

Last edited by stf92; 11-13-2010 at 05:50 AM.
 
Old 11-11-2010, 02:57 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
while [ 1 ]
do
...
done

You might want to look at the Advanced Bash-Scripting Guide.
 
1 members found this post helpful.
Old 11-11-2010, 03:00 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Something like this?
Code:
#!/bin/bash

while true
do
:
done
Hope this helps.
 
1 members found this post helpful.
Old 11-11-2010, 03:03 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Any of the bash looping facilities described here (except the first form of for) can be used to construct an infinite loop.
 
1 members found this post helpful.
Old 11-11-2010, 04:35 AM   #5
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
Yet another way:
Code:
while (:) ; do 
...
done;
 
Old 11-11-2010, 04:53 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
Most kind of you, Nylex for having replied to my post. Something funny happened with the script.
Code:
bash-3.1# cat u03.sh
#!/usr/bin/bash

while [ 1 ] do

        ls /home/selui/fascination.mp3
done #THIS IS LINE 8
bash-3.1# ./u03.sh 
./u03.sh: line 8: syntax error near unexpected token `done'
./u03.sh: line 8: `done'
bash-3.1#
Code:
bash-3.1# cat u04.sh
#!/usr/bin/bash

while [ 1 ] 
do
        ls /home/selui/fascination.mp3
done
bash-3.1#
The latter works fine (u04.sh). Why, is something I'll need a little time to investigate.

I extend my thanks to both druuna and catkin.
 
Old 11-11-2010, 05:00 AM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
goldenbarb has made me see why u03.sh got an error. I wrote 'while [ 1 ] do' while I should have written 'while [ 1 ]; do' or

while
do

That is to say: 'while' is a "sentence" and 'do' is another one. Either I write sentences in different lines or I separate them by ';' (semicolon).

But Nylex's 'while [ 1 ]' construct intrigues me most. Both the use of square brackets (a complicated matter, I know) and the meaning of '1' in that construct. I suspect I could have written (pseudo code)

repeat
...
until [ 0 ]

with the same result.

Last edited by stf92; 11-11-2010 at 05:14 AM.
 
Old 11-11-2010, 07:40 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by stf92 View Post
But Nylex's 'while [ 1 ]' construct intrigues me most. Both the use of square brackets (a complicated matter, I know) and the meaning of '1' in that construct. I suspect I could have written (pseudo code)
I don't really know that much, but the '[' is the "test" command in Bash, so you might want to look that up. I only chose 1 because generally any integer greater than 0 will be "true" and I didn't realise that Bash had a "true" keyword.
 
Old 11-11-2010, 08:33 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Notes:
  1. true is a bash builtin replacing the original external command /bin/true. The bash command help true and the true man page reveal a minor difference in that the true external command accepts options --help and --version whereas the builtin ignores any arguments. I wonder how many versions there have been of /bin/true?!
  2. true, :, and :: are functionally identical.
  3. A test (test, [ or [[) with a test expression that evaluates to true and a double-parenthesis arithmetic expression evaluator ( (( ) with an arithmetic expression that evaluates to non-zero are functionally identical to true etc.
  4. The truth of single word test expressions, that is ones without any test operators, is not immediately obvious as discussed here in "Example 7-1. What is truth?". For numbers, zero is false and anything else is true while for strings, empty is false and anything else is true. Numbers are thus used in the opposite sense from return codes ($? as set by the last command executed) where 0 is true and anything else false.
  5. A convenient way to test truth at the command prompt is
    Code:
    <whatever> && echo true || echo false
    for example
    Code:
    [ 1 ] && echo true || echo false
 
Old 11-13-2010, 06:23 AM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
Thanks to you all, guys, and forgive this way of learning bash.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash infinite loop problem haunting stp001 Linux - Newbie 5 03-17-2010 07:43 PM
Bash infinite loop problem? stp001 Linux - Newbie 6 03-15-2010 07:56 AM
Bash infinite loop problems? stp001 Programming 2 03-14-2010 03:41 PM
Problem with char driver write function - infinite loop Cherubim Linux - Kernel 1 10-13-2009 02:08 AM


All times are GMT -5. The time now is 10:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration