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.

# Accept upper or lowercase input.<br />

echo<br />

echo "Roland Evans"<br />

echo "4321 Floppy Dr."<br />

echo "Hardscrabble, CO 80753"<br />

echo "(303) 734−9874"<br />

echo "(303) 734−9892 fax"<br />

echo "revans@zzy.net"<br />

echo "Business partner & old friend"<br />

;;<br />

# Note double semicolon to terminate each option.<br />

"J" | "j" )<br />

echo<br />

echo "Mildred Jones"<br />

echo "249 E. 7th St., Apt. 19"<br />

echo "New York, NY 10009"<br />

echo "(212) 533−2814"<br />

echo "(212) 533−9972 fax"<br />

echo "milliej@loisaida.com"<br />

echo "Girlfriend"<br />

echo "Birthday: Feb. 11"<br />

;;<br />

# Add info for Smith & Zane later.<br />

* )<br />

# Default option.<br />

# Empty input (hitting RETURN) fits here, too.<br />

echo<br />

echo "Not yet in database."<br />

;;<br />

esac<br />

echo<br />

# Exercise:<br />

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

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

#+ instead of terminating after displaying just one address.<br />

exit 0<br />

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

An exceptionally clever use of case involves testing for command−line parameters.<br />

#! /bin/bash<br />

case "$1" in<br />

"") echo "Usage: ${0##*/} "; exit 65;; # No command−line parameters,<br />

# or first parameter empty.<br />

# Note that ${0##*/} is ${var##pattern} param substitution. Net result is $0.<br />

−*) FILENAME=./$1;;<br />

# If filename passed as argument ($1) starts with a dash,<br />

# replace it with ./$1<br />

# so further commands don't interpret it as an option.<br />

* ) FILENAME=$1;; # Otherwise, $1.<br />

esac<br />

Chapter 10. Loops and Branches 119

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

Saved successfully!

Ooh no, something went wrong!