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

33 lines
858 B
Bash

#!/bin/bash
if [ -z "$1" ]
then
Filename=names.data # Default, if no filename specified.
else
Filename=$1
fi
line_count=`wc $Filename | awk '{ print $1 }'`
# Number of lines in target file.
#
# Very contrived and kludgy, nevertheless shows that
#+ it's possible to redirect stdin within a "for" loop...
#+ if you're clever enough.
#
# More concise is line_count=$(wc -l < "$Filename")
for name in `seq $line_count` # Recall that "seq" prints sequence of numbers.
# while [ "$name" != Smith ] -- more complicated than a "while" loop --
do
read name # Reads from $Filename, rather than stdin.
echo $name
if [ "$name" = Smith ] # Need all this extra baggage here.
then
break
fi
done <"$Filename" # Redirects stdin to file $Filename.
# ^^^^^^^^^^^^
exit 0