Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Shell Scripts

Although Chapter 25 covers the basic syntax and language of shell programming, look at a few additional examples of scripts that are often useful in day-to-day operation.

Changing Strings in Files with chstr

Users who maintain source code, client lists, and other records often want to launch a find-and-replace operation from the command line. It's useful to have a variant of chstr on UNIX hosts. Listing 26.2 gives one example.

Example 26.2. chstr—A Simple Find-and-Replace Operation

########
#
# See usage() definition, below, for more details.
#
# This implementation doesn't do well with complicated escape
#     sequences. That has been no more than a minor problem in
#     the real world.
#
########
usage() {
     echo "chstr BEFORE AFTER <filenames>

     changes the first instance of BEFORE to AFTER in each line of <filenames>,
     and reports on the differences.
Examples:
     chstr TX Texas */addresses.*
     chstr ii counter2 *.c"
     exit 0
}

case $1 in
     -h|-help)     usage;;
esac

if test $# -lt 3
then
     usage
fi

TMPDIR=/tmp
     # It's OK if more than one instance of chstr is run simultaneously.
     #     The TMPFILE names are specific to each invocation, so there's
     #     no conflict.
TMPFILE=$TMPDIR/chstr.$$

BEFORE=$1
AFTER=$2

     # Toss the BEFORE and AFTER arguments out of the argument list.
shift;shift

for FILE in $*
do
     sed -e "s/$BEFORE/$AFTER/" $FILE >$TMPFILE
     echo "$FILE:"
     diff $FILE $TMPFILE
     echo ""
     mv $TMPFILE $FILE
done

The preceding chstr script will take its first two arguments as the string to look for and the string to put in its place, respectively. In the script they're in the BEFORE and AFTER variables. The two shift commands move those strings aside from the list of arguments and the rest of the arguments are treated as files. Each file is run through sed to replace $BEFORE with $AFTER and placed in a temporary file. The file is then moved back into place.

Most interactive editors permit a form of global search-and-replace, and some even make it easy to operate on more than one file. Perhaps that's a superior automation for your needs. If not, chstr is a minimal command-line alternative that is maximally simple to use.

WWW Retrieval

A question that arises frequently is how to automate retrieval of pages from the World Wide Web. This section shows the simplest of many techniques.

FTP Retrieval

Create a shell script, retrieve_one, with the contents of Listing 26.3 and with execution enabled (that is, command chmod +x retrieve_one).

Example 26.3. retrieve_one—Automating FTP Retrieval

# Usage:  "retrieve_one HOST:FILE" uses anonymous FTP to connect
#     to HOST and retrieve FILE into the local directory.

MY_ACCOUNT=myaccount@myhost.com
HOST=`echo $1 | sed -e "s/:.*//"`
FILE=`echo $1 | sed -e "s/.*://"`
LOCAL_FILE=`basename $FILÈ

     # -v:  report all statistics.
     # -n:  connect without interactive user authentication.
ftp -v -n $HOST << SCRIPT
     user anonymous $MY_ACCOUNT
     get $FILE $LOCAL_FILE
     quit
SCRIPT

retrieve_one is useful for purposes such as ordering a current copy of a FAQ into your local directory; start experimenting with it by making a request with the following:


   # retrieve_one rtfm.mit.edu:/pub/usenet-by-hierarchy/comp/os/linux/answers/ linux/info-sheet

Red Hat Linux comes with other utilities you can use for FTP retrieval. See Chapter 11, "FTP," for information about the ncftp and gftp commands.

HTTP Retrieval

For an HTTP interaction, let the Lynx browser do the bulk of the work. The Lynx browser that accompanies the Red Hat distribution is adequate for all but the most specialized purposes. You can obtain the latest version at http://lynx.browser.org. Although most Lynx users think of Lynx as an interactive browser, it's also handy for dropping a copy of the latest headlines, with live links, in a friend's mailbox with this:


   # lynx -source http://www.cnn.com | mail someone@somewhere.com 
   

To create a primitive news update service, script the following and launch it in the background (using the ampersand, &):

NEW=/tmp/news.new
OLD=/tmp/news.old
URL=http://www.cnn.com
while true
do
     mv $NEW $OLD
     lynx -dump -nolist $URL >$NEW
     diff $NEW $OLD
          # Wait ten minutes before starting the next comparison.
     sleep 600
done

Any changes in the appearance of CNN's home page will appear onscreen every 10 minutes. This simple approach is less practical than you might first expect because CNN periodically shuffles the content without changing the information. It's an instructive example, however, and a starting point from which you can elaborate your own scripts.

Conclusions on Shell Programming

Shells are glue; if there's a way to get an application to perform an action from the command line, there's almost certainly a way to wrap it in a shell script that gives you power over argument validation, iteration, and input-output redirection. These are powerful techniques and well worth the few minutes of study and practice it takes to begin learning them.

Even small automations pay off. My personal rule of thumb is to write tiny disposable one-line shell scripts when I expect to use a sequence even twice during a session. For example, although I have a sophisticated set of reporting commands for analyzing World Wide Web server logs, I also find myself going to the trouble of editing a disposable script such as /tmp/r9


   grep claird `ls -t /usr/cern/log/* | head -1` | grep -v $1 | wc -l

to do quick, ad hoc queries on recent hit patterns. This particular example reports on the number of requests for pages that include the string claird and exclude the first argument to /tmp/r9, in the most recent log.

Share ThisShare This

Informit Network