Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

Another way to manipulate stdin and stdout is to have one command take its input directly from the output of another command. To do this, Unix uses the , or pipe , character to connect commands together to form pipelines , with the stdout of each command being piped into the stdin of the next one. The stderr is not affected by the pipeline. The use of pipes enables you to create an almost infinite variety of command lines processing input and output to what are nothing more than miniature custom applications.

To pipe the output of one command into another:

1.

Type the first command that produces output, but don't press until the last step below.

This can be any command (along with options and arguments) that produces output on stdout . For example,

ls -l /bin

lists the contents of /bin , where the executable files for many commands are stored ( Figure 2.34 ). But let's say we want to see only the modification date and the filename. Noticing that this information starts 38 characters into each line, we might pipe the output of the ls command into the cut command, telling cut to show us only from character 38 to the end of each line.

Figure 2.34. Running ls -l /bin lists the contents of /bin , where the executable files for many commands are stored.

user-vc8f9gd:~ vanilla$ ls -l /bin total 8816 -r-xr-xr-x 2 root wheel 18104 Jan 31 01:13 [ -rwxr-xr-x 1 root wheel 581680 Jan 31 00:23 bash -r-xr-xr-x 1 root wheel 14380 Jan 31 01:04 cat -r-xr-xr-x 1 root wheel 23792 Jan 31 00:50 chmod -r-xr-xr-x 1 root wheel 19108 Jan 31 00:51 cp -r-xr-xr-x 2 root wheel 347060 Jan 31 00:52 csh -r-xr-xr-x 1 root wheel 19016 Jan 31 01:12 date -r-xr-xr-x 1 root wheel 27012 Jan 31 00:51 dd -r-xr-sr-x 1 root operator 22984 Jan 31 00:51 df -r-xr-xr-x 1 root wheel 14440 Jan 31 05:03 domainname -r-xr-xr-x 1 root wheel 13712 Jan 31 01:13 echo -r-xr-xr-x 1 root wheel 56480 Jan 31 01:05 ed (...output truncated for brevity...)

2.

Add the character at the end of the command.

The character catches the output from what is on its left and passes it to the stdin of what is on its right.

3.

Add the command that will receive its input from the pipe. Continuing our example, the command line would now look like this:

ls -l /bin cut -c38-

and the result would look like that shown in Figure 2.35 .

Figure 2.35. Running ls -l /bin cut -c38- gives this partial output.

user-vc8f9gd:~ vanilla$ ls -l /bin cut -c38- Jan 31 01:13 [ Jan 31 00:23 bash Jan 31 01:04 cat Jan 31 00:50 chmod Jan 31 00:51 cp Jan 31 00:52 csh Jan 31 01:12 date Jan 31 00:51 dd Jan 31 00:51 df Jan 31 05:03 domainname Jan 31 01:13 echo Jan 31 01:05 ed (...output truncated for brevity...)

Now you can press . Or if you wanted to redirect the output of the pipeline to a file, the final command line would be

ls -l /bin cut -c38- > outfile

Assuming your computer is connected to the Internet, you can pipe the output of any command into an e-mail message for another user on the Internet.

To pipe the output of a command into e-mail:

  • command mail -s " subject " address

    command can be any command line that produces output on stdout . subject is the subject of the e-mail message, and address is a valid e-mail address. For example:

    ls -l /usr/bin mail s myself@xyz.com

Категории