home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3623 < prev    next >
Encoding:
Text File  |  1992-08-25  |  1.3 KB  |  45 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: Korn shell and file descriptors
  5. Message-ID: <mcook.714794558@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <1548@tetra.co.uk>
  10. Date: Wed, 26 Aug 1992 02:02:38 GMT
  11. Lines: 32
  12.  
  13. dave@tetra.co.uk (David Sussman) writes:
  14.  
  15. >I have an interesting Korn Shell problem.  I have a script containing:
  16.  
  17. >    echo stream 1 >&1
  18. >    echo stream 2 >&2
  19. >    echo stream 3 >&3
  20. >    echo stream 4 >&4
  21.  
  22. >Under the Bourne Shell this works ok, but the Korn Shell produces:
  23.  
  24. >    stream 1
  25. >    stream 2
  26. >    ./ksh[3]: 3: bad file unit number
  27. >    ./ksh[4]: 4: bad file unit number
  28.  
  29. >Why?
  30.  
  31. When you do
  32.  
  33.     echo stream 3 >&3
  34.  
  35. the shell does a dup2(3,1) for you, which means close file descriptor 1, and
  36. then make it point at whatever file descriptor 3 is pointing at.  If file
  37. descriptor 3 isn't currently open, dup2 fails.  The Bourne shell was probably
  38. ignoring the failure; ksh tells you.
  39.  
  40. You can test to see whether file descriptor 3 is open like this:
  41.  
  42.     (exec>&3) 2>/dev/null || echo "3 is not open"
  43.  
  44. Michael.
  45.