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