27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

for b in 1 2 3 4 5<br />

do<br />

echo "Pass $inner in inner loop."<br />

let "inner+=1" # Increment inner loop counter.<br />

done<br />

# End of inner loop.<br />

let "outer+=1" # Increment outer loop counter.<br />

echo<br />

# Space between output in pass of outer loop.<br />

done<br />

# End of outer loop.<br />

exit 0<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

See Example 26−6 for an illustration of nested "while" loops, and Example 26−7 to see a "while" loop nested<br />

inside an "until" loop.<br />

10.3. Loop Control<br />

Commands Affecting Loop Behavior<br />

break, continue<br />

The break and continue loop control commands [23] correspond exactly to their counterparts in other<br />

programming languages. The break command terminates the loop (breaks out of it), while continue<br />

causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular<br />

loop cycle.<br />

Example 10−20. Effects of break and continue in a loop<br />

#!/bin/bash<br />

LIMIT=19 # Upper limit<br />

echo<br />

echo "Printing Numbers 1 through 20 (but not 3 and 11)."<br />

a=0<br />

while [ $a −le "$LIMIT" ]<br />

do<br />

a=$(($a+1))<br />

if [ "$a" −eq 3 ] || [ "$a" −eq 11 ] # Excludes 3 and 11<br />

then<br />

continue # Skip rest of this particular loop iteration.<br />

fi<br />

echo −n "$a "<br />

done<br />

# Exercise:<br />

# Why does loop print up to 20?<br />

echo; echo<br />

echo Printing Numbers 1 through 20, but something happens after 2.<br />

Chapter 10. Loops and Branches 114

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!