LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash: how do I write an infinite loop? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-how-do-i-write-an-infinite-loop-843604/)

stf92 11-11-2010 01:53 AM

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.

Nylex 11-11-2010 01:57 AM

while [ 1 ]
do
...
done

You might want to look at the Advanced Bash-Scripting Guide.

druuna 11-11-2010 02:00 AM

Hi,

Something like this?
Code:

#!/bin/bash

while true
do
:
done

Hope this helps.

catkin 11-11-2010 02:03 AM

Any of the bash looping facilities described here (except the first form of for) can be used to construct an infinite loop.

goldenbarb 11-11-2010 03:35 AM

Yet another way:
Code:

while (:) ; do
...
done;


stf92 11-11-2010 03:53 AM

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.

stf92 11-11-2010 04:00 AM

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.

Nylex 11-11-2010 06:40 AM

Quote:

Originally Posted by stf92 (Post 4155585)
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.

catkin 11-11-2010 07:33 AM

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

stf92 11-13-2010 05:23 AM

Thanks to you all, guys, and forgive this way of learning bash.


All times are GMT -5. The time now is 11:47 PM.