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

esac<br />

◊ Quoting the variables is not mandatory, since word splitting does not take<br />

place.<br />

◊ Each test line ends with a right paren ).<br />

◊ Each condition block ends with a double semicolon ;;.<br />

◊ The entire case block terminates with an esac (case spelled backwards).<br />

Example 10−24. Using case<br />

#!/bin/bash<br />

echo; echo "Hit a key, then hit return."<br />

read Keypress<br />

case "$Keypress" in<br />

[a−z] ) echo "Lowercase letter";;<br />

[A−Z] ) echo "Uppercase letter";;<br />

[0−9] ) echo "Digit";;<br />

* ) echo "Punctuation, whitespace, or other";;<br />

esac # Allows ranges of characters in [square brackets].<br />

# Exercise:<br />

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

# As the script stands, # it accepts a single keystroke, then terminates.<br />

# Change the script so it accepts continuous input,<br />

# reports on each keystroke, and terminates only when "X" is hit.<br />

# Hint: enclose everything in a "while" loop.<br />

exit 0<br />

Example 10−25. Creating menus using case<br />

#!/bin/bash<br />

# Crude address database<br />

clear # Clear the screen.<br />

echo "<br />

Contact List"<br />

echo "<br />

−−−−−−− −−−−"<br />

echo "Choose one of the following persons:"<br />

echo<br />

echo "[E]vans, Roland"<br />

echo "[J]ones, Mildred"<br />

echo "[S]mith, Julie"<br />

echo "[Z]ane, Morris"<br />

echo<br />

read person<br />

case "$person" in<br />

# Note variable is quoted.<br />

"E" | "e" )<br />

Chapter 10. Loops and Branches 118

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

Saved successfully!

Ooh no, something went wrong!