M.1. Analyzing Scripts
Examine the following script. Run it, then explain what it
does. Annotate the script and rewrite it in a more compact and
for((nr=1; nr<$MAX; nr++))
let "t1 = nr % 5"
if [ "$t1" -ne 3 ]
let "t2 = nr % 7"
if [ "$t2" -ne 4 ]
let "t3 = nr % 9"
if [ "$t3" -ne 5 ]
break # What happens when you comment out this line? Why?
echo "Number = $nr"
Explain what the following script does. It is really just
a parameterized command-line pipe.
file "$DIRNAME"/* | fgrep "$FILETYPE" | tee $LOGFILE | wc -l
A reader sent in the following code snippet.
while read LINE
done < `tail -f /var/log/messages`
He wished to write a script tracking changes to the system log
the above code block hangs and does nothing
useful. Why? Fix this so it does work. (Hint:
rather than redirecting the
stdin of the loop
, try a pipe
Analyze Example A-10, and reorganize it in a
simplified and more logical style. See how many of the variables
can be eliminated, and try to optimize the script to speed up
its execution time.
Alter the script so that it accepts any ordinary ASCII
text file as input for its initial "generation". The
script will read the first $ROW*$COL
characters, and set the occurrences of vowels as
"living" cells. Hint: be sure to translate the
spaces in the input file to underscore characters.