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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Example 12−13. Checking words in a list for validity<br />

#!/bin/bash<br />

# lookup: Does a dictionary lookup on each word in a data file.<br />

file=words.data # Data file from which to read words to test.<br />

echo<br />

while [ "$word" != end ] # Last word in data file.<br />

do<br />

read word # From data file, because of redirection at end of loop.<br />

look $word > /dev/null # Don't want to display lines in dictionary file.<br />

lookup=$? # Exit status of 'look' command.<br />

if [ "$lookup" −eq 0 ]<br />

then<br />

echo "\"$word\" is valid."<br />

else<br />

echo "\"$word\" is invalid."<br />

fi<br />

done /dev/null<br />

then echo "\"$word\" is valid."<br />

else echo "\"$word\" is invalid."<br />

fi<br />

done

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

Saved successfully!

Ooh no, something went wrong!