Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!boulder!stan!dce From: dce@Solbourne.COM (David Elliott) Newsgroups: comp.unix.questions Subject: Re: Help with strings in Bourne shell Message-ID: <870@marvin.Solbourne.COM> Date: 25 Apr 89 14:16:06 GMT References: <10166@orstcs.CS.ORST.EDU> <1493@vsedev.VSE.COM> Reply-To: dce@Solbourne.com (David Elliott) Distribution: usa Organization: Solbourne Computer Inc., Longmont, Colorado Lines: 38 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; Watch out for this one. It's very important to note that in the "standard" shells (those that come with most commercial UNIXes), the redirection causes a subshell to be spawned to run the loop. The result is that any variables set inside the loop will not have the same values when the loop is exited. So, if the input data are used to set variables to be used later in the program, this won't work. 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. -- David Elliott dce@Solbourne.COM ...!{boulder,nbires,sun}!stan!dce