Prev | Current Page 1370 | Next

Christopher Negus

"Linux Bible, 2008 Edition: Boot up to Ubuntu, Fedora, KNOPPIX, Debian, openSUSE, and 11 Other Distributions"


The second rule allows you to create chains of commands, each of which uses the output of the
previous command as its input. A typical use of this behavior is a command pipeline, as in the
following rather contrived example:
$ cat /etc/passwd | cut -f 5 -d: | tr [:lower:] [:upper:] | sort | head -5
The first part of the command pipeline, cat /etc/passwd, writes the contents of the /etc/passwd
file to standard output. The second part, cut -f 5 -d:, cuts out the fifth field of its standard
input (the contents of /etc/passwd), using the colon character (:) as the field delimiter (the fifth
field of /etc/passwd is the GECOS or name field).
The third part, tr [:lower:] [:upper:], translates all lowercase characters in the standard
input to uppercase characters. The next element, sort, performs an alphabetic sort on the first letter
of its input before sending the sorted list to standard output. The final component, head -5,
displays only the first five lines of its standard input to standard out. The output of this pipeline
might resemble the following:
ADM
BIN
DAEMON
GAMES
LP
The following command pipeline should prove more useful: it e-mails the current uptime and load
average to the root user:
$ uptime | mailx -s "System Usage" root
The third rule, keeping programs self-contained, is related to the second.


Pages:
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382