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 />

exit 0<br />

See also Example 26−9, Example 26−10, and Example A−7.<br />

−−−<br />

Now, a for−loop used in a "real−life" context.<br />

Example 10−13. Using efax in batch mode<br />

#!/bin/bash<br />

EXPECTED_ARGS=2<br />

E_BADARGS=65<br />

if [ $# −ne $EXPECTED_ARGS ]<br />

# Check for proper no. of command line args.<br />

then<br />

echo "Usage: `basename $0` phone# text−file"<br />

exit $E_BADARGS<br />

fi<br />

if [ ! −f "$2" ]<br />

then<br />

echo "File $2 is not a text file"<br />

exit $E_BADARGS<br />

fi<br />

fax make $2<br />

# Create fax formatted files from text files.<br />

for file in $(ls $2.0*) # Concatenate the converted files.<br />

# Uses wild card in variable list.<br />

do<br />

fil="$fil $file"<br />

done<br />

efax −d /dev/ttyS3 −o1 −t "T$1" $fil<br />

# Do the work.<br />

# As S.C. points out, the for−loop can be eliminated with<br />

# efax −d /dev/ttyS3 −o1 −t "T$1" $2.0*<br />

# but it's not quite as instructive [grin].<br />

while<br />

exit 0<br />

This construct tests for a condition at the top of a loop, and keeps looping as long as that condition is<br />

true (returns a 0 exit status). In contrast to a for loop, a while loop finds use in situations where the<br />

number of loop repetitions is not known beforehand.<br />

while [condition]<br />

do<br />

command...<br />

done<br />

Chapter 10. Loops and Branches 110

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

Saved successfully!

Ooh no, something went wrong!