|
|
When you type a command line and press <Enter>, the entire line is evaluated as a single unit. It is possible to run several programs from one line; either sequentially, or simultaneously, or in a ``pipeline'' where the output from one command is used as input to the second command. You can also store frequently-used commands in a file, and tell the shell to execute the contents of the file as a script.
To send several commands, one after another, separate each of them with a semicolon. For example:
$ ls > list ; sort list > list1This command sequence creates a list of files in a file called list. It then sorts the contents of the file alphabetically and redirects the output into a file called list1. The command after the semicolon is not executed until the command before it has completed; the shell waits for the earlier commands to finish.
A pipeline is a sequence of commands that operate concurrently on a stream of data. All the processes are started simultaneously, but instead of reading or writing to a file or terminal, they read or write to and from a pipe. As the first process begins to produce some output, that output is fed to the second process as input, so that both processes are working at the same time. For example:
$ ls | sort > list1Here, the ls command sends its output straight to sort, which processes it and sends its own output to the file list1. Unlike the similar command line in ``Entering commands on the same line'', no intermediate file called list is created. Writing to a temporary file is a comparatively slow process because it involves transferring data to disk, and the second process must then access the file and read it back into memory. Pipes, in contrast, transfer data directly from one process to another without writing it to the disk.
More than one pipe operation can appear on a single command line, as follows:
$ sort -u file | grep basilisk | wc -l > wordsThis pipe sequence creates a file called words, containing a count of all the nonidentical lines in file that contain the word ``basilisk''.