March 21, 2005

CLI Magic: One-liners

Newcomers to Linux often feel overwhelmed by the number of commands at their disposal. Frustration sets in when they know what they wish to accomplish, but they do not have any idea what command(s) they can use to get the job done. This week's CLI Magic -- written by Jim Westbrook -- helps clear away some of that excess one-liner at a time.Fortunately, Linux provides a couple of utilitites which use a database of the man pages to point them to an appropriate command or provide a hint of what a command does. These are apropos and whatis respectively. An example may be the best illustration of their use. First one for apropos:

~/> apropos music
SDL::Music (3pm)     - a perl extension
tse3play (1)         - play/convert music files (MIDI or TSE3MDL) using
the TSE3 sequencer engine library

The output results are not particularly useful if what the user desires is to learn how to play a music CD on his system. The next logical step is to call apropos again, this time looking for the string "CD" instead of music. Alas, the result of this request returns 76 lines which is almost as overwhelming as the full list
of commands in /bin. What the user needs is a way to limit the results to only those items that include the word "play" in the description. Thankfully, Linux provides just such a tool --
grep.

Let's take a quick side trip to an example of the whatis
command:

~/> whatis grep
grep (1)             - print lines matching a pattern

At first glance this doesn't tell the newbie much. He needs to understand the meaning of the word pattern in the result. It is significant to note that grep can match either a
literal string or a regular expression as its passed parameter. For now, let's use the literal string form as it is much easier for most folks to understand. In this instance, the pattern is the sequence of characters passed to grep as a parameter.

However, for grep to have any data to examine, the user needs to get the results of apropos CD to be used as the input to grep. This is accomplished by the use of a pipe, which is enabled by using the pipe symbol (|) to link the two commands into one sequence. Such as the following:

~/> apropos CD |grep play
cdplay (1)           - an interactive text-mode program for controlling
and playing audio CD Roms under Linux.
cdp (1)              - an interactive text-mode program for controlling
and playing audio CD Roms under Linux.
qlcdnumber (3qt)     - Displays a number with LCD-like digits
cddb-id (1)          - Display the CDDB id of CD in [device].
workbone (1)         - an interactive text-mode program for controlling
and playing audio CD Roms under Linux.
QLCDNumber (3qt)     - Displays a number with LCD-like digits
cda (1)              - Compact disc digital audio player utility
xmcd (1)             - CD digital audio player utility for X11/Motif

Doing that limits the output of apropos to only those items which contain the word "play" in the result. This finally illustrates one of the greatest powers of Linux which I call "one-liners" as it is activated by a single line of commands in the order they are to be performed. This is also the basis of creating shell scripts. Shell scripts can be very complex sets of instructions, but they can also be one-line command strings.

My personal use of one-liner scripts is most often a repetitive set of tasks to be applied to some or all of the files in a single directory. This requires expanding our commands to include the for/do/done loop. I will refer you to "man bash" for these and other constructs rather than attempt to improve on that information. I will, however, provide an example that I use often
to play sets of video clips. Note that in bash, the elements of the loop are separated by semi-colons.

~/>for z in *mpg; do /usr/bin/mplayer -vo x11 -nofs $z; done

This one-liner examines the current directory for files ending with "mpg" making each one found, in turn, the current value for $z. This value is then passed to mplayer as the last parameter (the others are video info) so that it is displayed. When there are no more files meeting the criteria, the done command closes the script and returns the user to a command prompt.

The uses to which one-liners can be applied are limited only by the user's need, desires, and ingenuity. They are a major advantage of using Linux and its CLI. The more you make use of one-liners, the more things you will find for them to do for you.

Click Here!