Help with strings in Bourne shell
David Elliott
dce at Solbourne.COM
Wed Apr 26 00:16:06 AEST 1989
In article <1493 at vsedev.VSE.COM> logan at 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 at Solbourne.COM
...!{boulder,nbires,sun}!stan!dce
More information about the Comp.unix.questions
mailing list