Infinite Loops

team bbl


Loops can be infinite loops. That is, loops that continue repeating forever. Infinite loops are seldom written deliberately. They usually result from a scripting error. No one is immune.

The following program results in an infinite loop:

 #!/bin/bash while [ $n -lt 10 ] do     n=0     echo $n     let n=$n+1 done 

The output from this program is:

 0 0 0 0 ... 

continuing forever because $n is set to 0 at the beginning of each loop. Therefore, $n will never become equal to 10. So, the loop will never stop.

Another common scripting mistake is to leave out the statement that increments the value being tested. For instance, if you left out the let command in the above script, you would create an infinite loop.

You can stop an infinite loop with <Ctrl-c>. Perhaps more than one <Ctrl-c>. It should break out of the script. Also, a <Ctrl-z> works. It stops the script. Sometimes the output will continue to display for a time, but it will stop shortly.

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net