Introducing UNIX and Linux |
Introduction to shellsOverview |
Grouping commandsOccasionally it will be necessary to group commands together so
that they appear to the shell as a single command. For instance,
suppose you wish to print a text file ( The first involves creating a temporary file to store the
output, and then using
This is inelegant and to be discouraged - proliferation of temporary files causes confusion and wastes storage space. The second involves creating a script to perform the task, so:
This method also uses a temporary file, but we can substitute
the occurrence of
By using the latter method we have overcome the need for a
temporary file by taking the commands for By enclosing a list of commands in parentheses, a new invocation of the shell is formed to execute that list of commands, just as if you had placed those commands in a file and run that file as a separate shell script. The solution to the above problem then becomes:
If a sequence of commands (which can be separated either by newlines or by semicolons) is enclosed in parentheses, then they will be executed in sequence, and their output streams will be concatenated together to form a single stream, which can itself then be redirected. Worked exampleWithout creating any temporary files, and using a single shell
command, instruct your shell to display the names of files in the
current directory, preceded by an explanatory message, and
paged (in case you have a large number of them).
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck