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 http://www.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 # How many commands: a simple script to count how many executable # commands are in your current PATH. myPATH="$(echo $PATH sed -e 's/ /~~/g' -e 's/:/ /g')" count=0 ; nonex=0 for dirname in $myPATH ; do directory="$(echo $dirname sed 's/~~/ /g')" if [ -d "$directory" ] ; then for command in $(ls "$directory") ; do if [ -x "$directory/$command" ] ; then count="$(($count + 1))" else nonex="$(($nonex + 1))" fi done fi done echo "$count commands, and $nonex entries that weren't executable" exit 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.