| 1 | #!/bin/bash |
| 2 | |
| 3 | if [ -z "$1" ] |
| 4 | then |
| 5 | Filename=names.data # Default, if no filename specified. |
| 6 | else |
| 7 | Filename=$1 |
| 8 | fi |
| 9 | |
| 10 | line_count=`wc $Filename | awk '{ print $1 }'` |
| 11 | # Number of lines in target file. |
| 12 | # |
| 13 | # Very contrived and kludgy, nevertheless shows that |
| 14 | #+ it's possible to redirect stdin within a "for" loop... |
| 15 | #+ if you're clever enough. |
| 16 | # |
| 17 | # More concise is line_count=$(wc -l < "$Filename") |
| 18 | |
| 19 | |
| 20 | for name in `seq $line_count` # Recall that "seq" prints sequence of numbers. |
| 21 | # while [ "$name" != Smith ] -- more complicated than a "while" loop -- |
| 22 | do |
| 23 | read name # Reads from $Filename, rather than stdin. |
| 24 | echo $name |
| 25 | if [ "$name" = Smith ] # Need all this extra baggage here. |
| 26 | then |
| 27 | break |
| 28 | fi |
| 29 | done <"$Filename" # Redirects stdin to file $Filename. |
| 30 | # ^^^^^^^^^^^^ |
| 31 | |
| 32 | exit 0 |