|Text-only||Table of Contents (frame/ no frame)|
|(25) Wizard I/O|
This short test script can be used to generate suitable output.
ex13: display, text
echo "This goes to stdout" echo "This goes to stdout and has foo in the line" echo "This goes to stderr" >&2 exit 99
Pass stderr of a command into a pipeline for further processing
Example: ex14 display, text
./ex13.sh 2>&1 1>&3 3>&- | sed 's/stderr/STDERR/' 1>&2
We duplicate stdout to another file descriptor (3), then run the first command with stderr redirected to stdout and stdout redirected to the saved descriptor (3). The result is piped into other commands as needed. The output of the pipeline is redirected back to stderr, so that stdout and stderr of the script as a whole are what we expect.
ex13stat=`((./ex13.sh; echo $? >&4) | grep 'foo' 1>&3) 4>&1`
This script uses nested subshells captured in backtics. Again we first duplicate stdout to another file descriptor (3). The inner subshell runs the first command, then writes the exit status to fd 4. The outer subshell redirects 4 to stdout so that it is captured by the backtics. Standard output from the first command (inner subshell) is passed into the pipeline as normal, but the final output of the pipeline is redirected to 3 so that it appears on the original stdout and is not captured by the backtics.
If any of the commands really care about inheriting open file descriptors that they don't need then a more correct command line closes the descriptors before running the commands.
Combine the above two techniques:
Example: ex16 display, text
ex13stat=`((./ex13.sh 2>&1 1>&3 3>&- 4>&- ; echo $? >&4) | \
sed s/err/ERR/ 1>&2 3>&- 4>&- ) 4>&1`
A practical application of this would be running a utility such as
ddwhere the exit status is important to capture, but the error output is overly chatty and may need to be filtered before delivering to other parts of a script.
|wizardio.src last modified Mar 11, 2005||Introduction||Table of Contents
|© Dartmouth College|