Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ames!haven!vrdxhq!vsedev!logan From: logan@vsedev.VSE.COM (James Logan III) Newsgroups: comp.unix.questions Subject: Re: Help with strings in Bourne shell Message-ID: <1497@vsedev.VSE.COM> Date: 28 Apr 89 02:55:20 GMT References: <10166@orstcs.CS.ORST.EDU> <1493@vsedev.VSE.COM> <870@marvin.Solbourne.COM> Reply-To: logan@vsedev.VSE.COM (James Logan III) Distribution: usa Organization: VSE Software Development Lab Lines: 57 In article <870@marvin.Solbourne.COM> dce@Solbourne.com (David Elliott) writes: # In article <1493@vsedev.VSE.COM> logan@vsedev.VSE.COM (James Logan III) writes: # >BTW, you can also read from a specific file by redirecting the # >input to the read command like this: # > # > INPUTFILE="some_file"; # > # > while read DEFINITION <$INPUTFILE; do # > echo "$DEFINITION"; # > . # > . # > . # > done; This construct has a BIG problem. I corrected it in a previous posting. # # A typical trick is # # exec 3<&0 0<"$INPUTFILE" # while read DEFINITION # do # echo "$DEFINITION" # done # exec 0<&3 # # The first exec makes fd 3 a duplicate of fd 0 (stdin), and # redirects stdin. The second exec changes fd 0 back to what # it was. This has the side effect of redirecting stdin for every command invoked inside the while loop. (Take a look at my corrected posting.) Besides, it has the same effect as the simpler construct: while read DEFINITION; do echo "$DEFINITION"; done <$INPUTFILE; Under System V (on 3B2, Altos, and XENIX at least) the variable "DEFINITION" is not set in a sub-shell. To do this you would have to do something silly like: while `read DEFINITION`; do echo "$DEFINITION"; done; Perhaps this isn't the case in BSD UNIX. -Jim -- Jim Logan logan@vsedev.vse.com VSE Software Development Lab uucp: ..!uunet!vsedev!logan (703) 329-4654 inet: logan%vsedev.vse.com@uunet.uu.net