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.

Appendix A. Contributed Scripts<br />

These scripts, while not fitting into the text of this document, do illustrate some interesting shell programming<br />

techniques. They are useful, too. Have fun analyzing and running them.<br />

Example A−1. manview: Viewing formatted manpages<br />

#!/bin/bash<br />

# manview.sh: Formats the source of a man page for viewing.<br />

# This is useful when writing man page source and you want to<br />

#+ look at the intermediate results on the fly while working on it.<br />

E_WRONGARGS=65<br />

if [ −z "$1" ]<br />

then<br />

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

exit $E_WRONGARGS<br />

fi<br />

groff −Tascii −man $1 | less<br />

# From the man page for groff.<br />

# If the man page includes tables and/or equations,<br />

# then the above code will barf.<br />

# The following line can handle such cases.<br />

#<br />

# gtbl < "$1" | geqn −Tlatin1 | groff −Tlatin1 −mtty−char −man<br />

#<br />

# Thanks, S.C.<br />

exit 0<br />

Example A−2. mailformat: Formatting an e−mail message<br />

#!/bin/bash<br />

# mail−format.sh: Format e−mail messages.<br />

# Gets rid of carets, tabs, also fold excessively long lines.<br />

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

# Standard Check for Script Argument(s)<br />

ARGS=1<br />

E_BADARGS=65<br />

E_NOFILE=66<br />

if [ $# −ne $ARGS ] # Correct number of arguments passed to script?<br />

then<br />

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

exit $E_BADARGS<br />

fi<br />

if [ −f "$1" ]<br />

then<br />

file_name=$1<br />

# Check if file exists.<br />

Appendix A. Contributed Scripts 368

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

Saved successfully!

Ooh no, something went wrong!