LDP/LDP/guide/docbook/abs-guide/redir2a.sh

46 lines
1.0 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
# This is an alternate form of the preceding script.
2002-04-01 16:04:17 +00:00
# Suggested by Heiner Steven
#+ as a workaround in those situations when a redirect loop
#+ runs as a subshell, and therefore variables inside the loop
# +do not keep their values upon loop termination.
2001-07-10 14:25:50 +00:00
if [ -z "$1" ]
then
2001-09-04 13:27:31 +00:00
Filename=names.data # Default, if no filename specified.
2001-07-10 14:25:50 +00:00
else
Filename=$1
fi
exec 3<&0 # Save stdin to file descriptor 3.
exec 0<"$Filename" # Redirect standard input.
2001-07-10 14:25:50 +00:00
count=0
echo
while [ "$name" != Smith ]
do
2001-09-04 13:27:31 +00:00
read name # Reads from redirected stdin ($Filename).
2001-07-10 14:25:50 +00:00
echo $name
let "count += 1"
2004-02-16 17:21:02 +00:00
done # Loop reads from file $Filename
#+ because of line 20.
# The original version of this script terminated the "while" loop with
#+ done <"$Filename"
2004-02-16 17:21:02 +00:00
# Exercise:
# Why is this unnecessary?
2001-07-10 14:25:50 +00:00
exec 0<&3 # Restore old stdin.
exec 3<&- # Close temporary fd 3.
2001-07-10 14:25:50 +00:00
echo; echo "$count names read"; echo
exit 0