PAUSE
ENCORE5
ENCORE
BOX
SOLUTION
GO
Let us write a shell script for counting
the number of .c, .o, and .a files.
This process will consist of the following steps:
1. Initialize the .c, .o and .a counters
2. Count the number of .c files
3. Count the number of .o files
4. Count the number of .a files
5. Print the number of each type of files!
Step 1, initialize the three counters to 0.
c_count=0; o_count=0; a_count=0
Start the iteration for each of three types.
for i in .c .o .a
do
Start iteration for each file type.
for k in *$i*
do
Start counting the number of .c files. Note expr
is a built-in shell function for evaluating mathematical expressions.
if [ "$i" = .c ]
then
c_count=`expr $c_count + 1`
fi
In a similar fashion, count the number of .o and .a files.
if [ "$i" = .o ]
then
o_count=`expr $o_count + 1`
fi
if [ $i = ".a" ]
then
a_count=`expr $a_count + 1`
fi
Finally, end the loops, and print the results!
done
done
echo the number of .c files is $c_count
echo the number of .o files is $o_count
echo the number of .a files is $a_count
<CR> - to continue