#!/bin/sh -u # $0 (no args) # A script that loops appending some text to a file until the number of # lines in the file is larger than a desired length. # # Some of the comments in this script are "teacher" comments that should # never appear in student programs (or real-world programs). # # -IAN! idallen@ncf.ca February 2002 export PATH=/bin:/usr/bin umask 022 # The line to put into the output file. # - note how the string is quoted to include protected blanks and newlines! # The desired length of the output file, in lines. # The name of the output file. # string='Hello World! It is a Beautiful Day!' numlines=12 file='myout.txt' # Use command substitution to get the current length of the file. # (Warning: Use wc -l <"$file" and not wc -l "$file" !) # The output of "wc" (the single number) is substituted into the line. # The "test" command (named "[") tests the number against the length. # The return code of "test" determines whether the "while" keeps going. # >"$file" # start with an empty output file while [ $(wc -l <"$file") -lt $numlines ]; do echo "The current number of lines in '$file' is $(wc -l <"$file")" echo "$string" >>"$file" echo " The new number of lines in '$file' is $(wc -l <"$file")" done echo "The number of lines in the output file is: $(wc -l <"$file")" echo "The first four lines are" head -4 "$file" exit 0