home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5346 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  1.9 KB

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!ogicse!pnl-oracle!aggie!d3a061
  2. From: d3a061@aggie.oname (ZT Taylor)
  3. Newsgroups: comp.unix.shell
  4. Subject: SUMMARY: Bourne shell functions & 'exit'
  5. Message-ID: <1993Jan12.160725.19922@oracle.pnl.gov>
  6. Date: 12 Jan 93 16:07:25 GMT
  7. Article-I.D.: oracle.1993Jan12.160725.19922
  8. Sender: news@oracle.pnl.gov
  9. Reply-To: d3a061@aggie.oname
  10. Organization: Pacific Northwest Laboratory
  11. Lines: 49
  12.  
  13.  
  14. My original question:
  15.  
  16. > Bourne shell functions seem to be inconsistent in the way they handle an 'exit'
  17. > statement.  For example, the following:
  18. >     func() {
  19. >         .
  20. >         .
  21. >         exit 1
  22. >     }
  23. >     func
  24. > causes the whole shell script to exit, while this:
  25. >     func() {
  26. >         .
  27. >         .
  28. >         exit 1
  29. >     }
  30. >     junk=`func`
  31. >     echo $?
  32. > doesn't.  At first I thought this meant func was executed in a subshell, but
  33. > echoing $$ both places gives the same PID.  Is this context-sensitive treatment
  34. > of 'exit' a feature?  Can it be counted on in portable scripts?
  35.  
  36. Thanks to Don Peterson <donp@hpbs1639.boi.hp.com> and Philip Guenther <guenther@stolaf.edu> for responding.  Philip solved the mystery for me.  It seems
  37. that Bourne shell functions executed in backticks ARE run in a subshell, but per
  38. POSIX standards, $$ in a subshell expands to the PID of the parent.  To quote
  39. the POSIX standard (1003.2 section 3.5.2)...
  40.  
  41.  3.5.2  Special Parameters
  42.  
  43.  Listed below are the special parameters and the values to which they
  44.  shall expand.  Only the values of the special parameters are listed; see
  45.  3.6 for a detailed summary of all the stages involved in expanding words.
  46. [...]
  47.     $     Expands to the decimal process ID of the invoked shell.  In a
  48.           subshell (see 3.12), $ shall expand to the same value as that of
  49.           the current shell.
  50.  
  51. So...the behavior IS reliable and portable.
  52.  
  53. Todd Taylor
  54. Pacific Northwest Laboratory
  55. zt_taylor@pnl.gov
  56.