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.

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

Example 10−22. Continuing at a higher loop level<br />

#!/bin/bash<br />

# The "continue N" command, continuing at the Nth level loop.<br />

for outer in I II III IV V<br />

do<br />

echo; echo −n "Group $outer: "<br />

# outer loop<br />

for inner in 1 2 3 4 5 6 7 8 9 10 # inner loop<br />

do<br />

if [ "$inner" −eq 7 ]<br />

then<br />

continue 2 # Continue at loop on 2nd level, that is "outer loop".<br />

# Replace above line with a simple "continue"<br />

# to see normal loop behavior.<br />

fi<br />

echo −n "$inner " # 8 9 10 will never echo.<br />

done<br />

done<br />

echo; echo<br />

# Exercise:<br />

# Come up with a meaningful use for "continue N" in a script.<br />

exit 0<br />

Example 10−23. Using "continue N" in an actual task<br />

# Albert Reiner gives an example of how to use "continue N":<br />

# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

# Suppose I have a large number of jobs that need to be run, with<br />

#+ any data that is to be treated in files of a given name pattern in a<br />

#+ directory. There are several machines that access this directory, and<br />

#+ I want to distribute the work over these different boxen. Then I<br />

#+ usually nohup something like the following on every box:<br />

while true<br />

do<br />

for n in .iso.*<br />

do<br />

[ "$n" = ".iso.opts" ] && continue<br />

beta=${n#.iso.}<br />

[ −r .Iso.$beta ] && continue<br />

[ −r .lock.$beta ] && sleep 10 && continue<br />

lockfile −r0 .lock.$beta || continue<br />

echo −n "$beta: " `date`<br />

run−isotherm $beta<br />

date<br />

ls −alF .Iso.$beta<br />

[ −r .Iso.$beta ] && rm −f .lock.$beta<br />

continue 2<br />

done<br />

break<br />

Chapter 10. Loops and Branches 116

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

Saved successfully!

Ooh no, something went wrong!