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

padding=000<br />

# Use at most 3 zeroes to pad.<br />

soun=$prefix$suffix$padding<br />

MAXLEN=4<br />

soundex=${soun:0:$MAXLEN}<br />

# Pad with zeroes.<br />

# Truncate to maximum of 4 chars.<br />

echo "Soundex = $soundex"<br />

echo<br />

# The soundex code is a method of indexing and classifying names<br />

#+ by grouping together the ones that sound alike.<br />

# The soundex code for a given name is the first letter of the name,<br />

#+ followed by a calculated three−number code.<br />

# Similar sounding names should have almost the same soundex codes.<br />

# Examples:<br />

# Smith and Smythe both have a "S−530" soundex.<br />

# Harrison = H−625<br />

# Hargison = H−622<br />

# Harriman = H−655<br />

# This works out fairly well in practice, but there are numerous anomalies.<br />

#<br />

#<br />

# The U.S. Census and certain other governmental agencies use soundex,<br />

# as do genealogical researchers.<br />

#<br />

# For more information,<br />

#+ see the "National Archives and Records Administration home page",<br />

#+ http://www.nara.gov/genealogy/soundex/soundex.html<br />

# Exercise:<br />

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

# Simplify the "Exception Patch" section of this script.<br />

exit 0<br />

Example A−11. "Game of Life"<br />

#!/bin/bash<br />

# life.sh: "Life in the Slow Lane"<br />

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

# This is the Bash script version of John Conway's "Game of Life". #<br />

# "Life" is a simple implementation of cellular automata. #<br />

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

# On a rectangular grid, let each "cell" be either "living" or "dead". #<br />

# Designate a living cell with a dot, and a dead one with a blank space.#<br />

# Begin with an arbitrarily drawn dot−and−blank grid, #<br />

#+ and let this be the starting generation, "generation 0". #<br />

# Determine each successive generation by the following rules: #<br />

# 1) Each cell has 8 neighbors, the adjoining cells #<br />

#+ left, right, top, bottom, and the 4 diagonals. #<br />

# 123 #<br />

Appendix A. Contributed Scripts 379

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

Saved successfully!

Ooh no, something went wrong!