Example script: ex15.sh


   1: #!/bin/sh
   2: # Example 15
   3: 
   4: # Uses ex13.sh to generate some output and give us an 
   5: # exit status to capture.
   6: 
   7: # Get the exit status of ex13 into $ex13stat.  
   8: # stdout of ex13 is processed normally
   9: 
  10: # Save a copy of stdout
  11: exec 3>&1
  12: # Run a subshell, with 4 duplicated to 1 so we get it in stdout.  
  13: # Capture the output in ``
  14: # ex13stat=`( ...  ) 4>&1`
  15: # Inside the subshell, run another subshell to execute ex13, 
  16: # and echo the status code to 4
  17: # (./ex13.sh; echo $? >&4)
  18: # stdout from the inner subshell is processed normally, but the 
  19: # subsequent output must be directed to 3 so it goes to the 
  20: # original stdout and not be captured by the ``
  21: ex13stat=`((./ex13.sh; echo $? >&4) | grep 'foo' 1>&3) 4>&1`
  22: 
  23: echo Last command status=$?
  24: echo ex13stat=$ex13stat
  25: 
  26: # If any of the commands really care about inheriting open file 
  27: # descriptors that they don't need then a more correct command line 
  28: # closes the descriptors before running the commands
  29: exec 3>&1
  30: ex13stat=`((./ex13.sh 3>&- 4>&- ; echo $? >&4) | \
  31:    grep 'foo'  1>&3 3>&- 4>&- ) 4>&1`
  32: echo Last command status=$?
  33: echo ex13stat=$ex13stat



  last modified 22/03/2012 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College