Process Substitution

8.7 Process Substitution

A unique but rarely used feature of bash is process substitution . Let's say that you had two versions of a program that produced large quantities of output. You want to see the differences between the output from each version. You could run the two programs, redirecting their output to files, and then use the cmp utility to see what the differences were.

Another way would be to use process substitution. There are two forms of this substitution. One is for input to a process: > ( list ); the other is for output from a process: < ( list ). list is a process that has its input or output connected to something via a named pipe . A named pipe is simply a temporary file that acts like a pipe with a name .

In our case, we could connect the outputs of the two programs to the input of cmp via named pipes:

cmp <(prog1) <(prog2)

prog1 and prog2 are run concurrently and connect their outputs to named pipes. cmp reads from each of the pipes and compares the information, printing any differences as it does so.

This chapter has covered a lot of territory. Here are some exercises that should help you make sure you have a firm grasp on the material. Don't worry if you have trouble with the last one; it's especially difficult.

1.             Write a shell script called pinfo that combines the jobs and ps commands by printing a list of jobs with their job numbers , corresponding process IDs, running times, and full commands.

2.             Take a non-trivial shell script and "bullet-proof" it with signal traps.

3.             Take a non-trivial shell script and parallelize it as much as possible.

4.             Write the code that checks for duplicate arguments to the mcp script. Bear in mind that different pathnames can point to the same file. (Hint: if $i is "1", then eval ' echo \${$i} ' prints the first command-line argument. Make sure you understand why.)

 



Learning the Bash Shell
Learning the bash Shell: Unix Shell Programming (In a Nutshell (OReilly))
ISBN: 0596009658
EAN: 2147483647
Year: 1998
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net