home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / shell / 5071 < prev    next >
Encoding:
Text File  |  1992-12-15  |  1.7 KB  |  48 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!spool.mu.edu!umn.edu!uum1!digiboard!digibd!rick
  3. From: rick@digibd.digibd.com (Rick Richardson)
  4. Subject: Re: Reading a line from a file in Bourne
  5. Message-ID: <rick.724414097@digibd>
  6. Sender: news@digiboard.digibd.com (USENET News)
  7. Nntp-Posting-Host: digibd.digibd.com
  8. Organization: DigiBoard, Incorporated, Eden Prairie,MN
  9. References: <SHUTTON.92Dec11112920@fokker.union.indiana.edu> <SHUTTON.92Dec11223107@fokker.union.indiana.edu> <1992Dec14.233439.4054@highlite.uucp>
  10. Distribution: usa
  11. Date: Tue, 15 Dec 1992 10:08:17 GMT
  12. Lines: 34
  13.  
  14. dlreed@highlite.uucp (David L. Reed) writes:
  15.  
  16. >The alternative that has worked best for me is to let while take
  17. >stdin directly from the file:
  18.  
  19. >#!/bin/sh
  20. >while read LINE
  21. >do
  22. > echo a full line: $LINE
  23. >done < myfile
  24.  
  25. My favorite way is to use shell I/O redirection.  This method allows
  26. one to set and keep the values of shell variables.  It works in all
  27. versions of Bourne shell from XENIX to SVR3.2 to SVR4.0 to SVR4.2,
  28. except that you have to have the latest SVR4.0 shell.  Early SVR4.0
  29. Bourne shells introduced a bug where this code would hang the 2nd time it
  30. is used.  Without the 'exec' malarky, a lot of Bourne shells will spawn
  31. a subprocess to do the "done < myfile" part shown above.
  32.  
  33. # Example showing technique
  34. exec 3<&0 0<myfile
  35. count=0
  36. while read LINE
  37. do
  38.     count=`expr count + 1`
  39. done
  40. exec 0<&3 3<&-
  41. echo "$count lines"
  42.  
  43. --
  44. Rick Richardson        Senior Staff Engineer    Investment advice for 1993-1996:
  45. DigiBoard, Inc.        Email: rick@digibd.com    Go long on Arkansas chicken
  46. 6400 Flying Cloud Dr.    Fax:   (612) 943-0803    Go short on everything else
  47. Eden Prarie, MN 55344    Tel:   (612) 943-5383    <standard disclaimer>
  48.