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

fi<br />

( shift; "$PROGNAME" $* ) | grep $1<br />

# Script recursively calls itself.<br />

exit 0<br />

# Script exits here.<br />

# It's o.k. to put non−hashmarked comments<br />

#+ and data after this point.<br />

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

# Sample "phonebook" datafile:<br />

John Doe 1555 Main St., Baltimore, MD 21228 (410) 222−3333<br />

Mary Moe 9899 Jones Blvd., Warren, NH 03787 (603) 898−3232<br />

Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333−4567<br />

Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444−5678<br />

Zoe Zenobia 4481 N. Baker St., San Franciso, SF 94338 (415) 501−1631<br />

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

$bash pb.sh Roe<br />

Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333−4567<br />

Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444−5678<br />

$bash pb.sh Roe Sam<br />

Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444−5678<br />

# When more than one argument passed to script,<br />

#+ prints *only* the line(s) containing all the arguments.<br />

Too many levels of recursion can exhaust the script's stack space, causing a segfault.<br />

34.5. "Colorizing" Scripts<br />

The ANSI [64] escape sequences set screen attributes, such as bold text, and color of foreground and<br />

background. DOS batch files commonly used ANSI escape codes for color output, and so can Bash scripts.<br />

Example 34−8. A "colorized" address database<br />

#!/bin/bash<br />

# ex30a.sh: "Colorized" version of ex30.sh.<br />

# Crude address database<br />

clear<br />

# Clear the screen.<br />

echo −n " "<br />

echo −e '\E[37;44m'"\033[1mContact List\033[0m"<br />

# White on blue background<br />

echo; echo<br />

echo −e "\033[1mChoose one of the following persons:\033[0m"<br />

# Bold<br />

tput sgr0<br />

echo "(Enter only the first letter of name.)"<br />

echo<br />

echo −en '\E[47;34m'"\033[1mE\033[0m" # Blue<br />

tput sgr0<br />

# Reset colors to "normal."<br />

echo "vans, Roland"<br />

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

echo −en '\E[47;35m'"\033[1mJ\033[0m" # Magenta<br />

Chapter 34. Miscellany 342

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

Saved successfully!

Ooh no, something went wrong!