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.

#23 A Reminder Utility

Windows and Mac users for years have appreciated simple utilities like Stickies and Post-It, streamlined applications that let you have tiny reminder windows stuck on your screen. They're a perfect place to jot down a phone number or other reminder. If you're at the Unix command line, there's nothing analogous, yet the problem is quite easily solved , as shown in this pair of scripts.

The first script, remember , 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 remindme , a companion shell script that either displays the contents of the rememberfile if no arguments are given or, if an argument is given, searches in this file for the specified pattern.

The Code

 #!/bin/sh # remember - An easy command-line-based memory pad. rememberfile="$HOME/.remember" if [ $# -eq 0 ] ; then   echo "Enter note, end with ^D: "   cat - >> $rememberfile else   echo "$@" >> $rememberfile fi exit 0 

Here's the second script, remindme :

 #!/bin/sh # remindme - Searches a data file for matching lines, or shows the entire contents #   of the data file if no argument is specified. rememberfile="$HOME/.remember" if [ $# -eq 0 ] ; then   more $rememberfile else   grep -i "$@" $rememberfile  ${PAGER:-more} fi exit 0 

Running the Scripts

To use the remindme utility, first add notes, phone numbers , or anything else to the rememberfile with the remember script. Then search this freeform database with remindme , specifying as long or short a pattern as you'd like.

The Results

 $  remember  Enter note, end with ^D:  The Boulder Community Network:   ^D  

Then, when I want to remember that note, months later:

 $  remindme boulder  The Boulder Community Network: 

Or if I need any other data that might be in there:

 $  remindme 800  Southwest Airlines: 800-IFLYSWA 

Hacking the Script

While certainly not any sort of shell script programming tour de force, these scripts neatly demonstrate the incredible extensibility of the Unix command line. If you can envision something, the odds are good that there's a simple and straightforward way to accomplish it.

These scripts could be improved in any number of ways. For instance, you could create the concept of records such that each record is time-stamped and multiline input is saved as a single entity that can be searched with a regular expression, which would enable you to store phone numbers for a group of people and retrieve them all by remembering the name of only one person in the group. If you're really into scripting, you might also want to include edit and delete capabilities. Then again, it's pretty easy to edit the ~/.remember file by hand.

Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: