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

grep −v '^#' | # Delete lines beginning with a hashmark.<br />

grep −v '^$'<br />

# Delete blank lines.<br />

exit 0<br />

Example A−10. Soundex conversion<br />

#!/bin/bash<br />

# soundex.sh: Calculate "soundex" code for names<br />

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

# Soundex script<br />

# by<br />

# Mendel Cooper<br />

# thegrendel@theriver.com<br />

# 23 January, 2002<br />

#<br />

# Placed in the Public Domain.<br />

#<br />

# A slightly different version of this script appeared in<br />

#+ Ed Schaefer's July, 2002 "Shell Corner" column<br />

#+ in "Unix Review" on−line,<br />

#+ http://www.unixreview.com/documents/uni1026336632258/<br />

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

ARGCOUNT=1<br />

E_WRONGARGS=70<br />

# Need name as argument.<br />

if [ $# −ne "$ARGCOUNT" ]<br />

then<br />

echo "Usage: `basename $0` name"<br />

exit $E_WRONGARGS<br />

fi<br />

assign_value ()<br />

# Assigns numerical value<br />

{ #+ to letters of name.<br />

val1=bfpv # 'b,f,p,v' = 1<br />

val2=cgjkqsxz # 'c,g,j,k,q,s,x,z' = 2<br />

val3=dt<br />

# etc.<br />

val4=l<br />

val5=mn<br />

val6=r<br />

# Exceptionally clever use of 'tr' follows.<br />

# Try to figure out what is going on here.<br />

value=$( echo "$1" \<br />

| tr −d wh \<br />

| tr $val1 1 | tr $val2 2 | tr $val3 3 \<br />

| tr $val4 4 | tr $val5 5 | tr $val6 6 \<br />

| tr −s 123456 \<br />

| tr −d aeiouy )<br />

# Assign letter values.<br />

# Remove duplicate numbers, except when separated by vowels.<br />

# Ignore vowels, except as separators, so delete them last.<br />

# Ignore 'w' and 'h', even as separators, so delete them first.<br />

Appendix A. Contributed Scripts 377

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

Saved successfully!

Ooh no, something went wrong!