- 论坛徽章:
- 0
|
第一次写SH,狂郁闷。。题目如下
Write a shell script called percentcomment. Given a list of filenames as
arguments, percentcomment should print out, for each file, what percentage of the
lines in that file are comments. You should consider a line to be a comment if it
starts with a # character. The output should be in this form:
file1 22%
file2 30%
file3 10%
(Hint: the wc program counts lines as well as words.)
这是我写的,错误百出。。。
#!/bin/sh
set -x
count=0
for file in $1
do
exec<$file
count=`expr $count + 1`
com=0
result=0
total=`wc -l $file`
while read LINE
do
if [ `echo $LINE | grep "^#" ` ]
then
com=`expr $com + 1`
fi
result=`expr (100)*($com)/($total)`
echo "file $count $result %"
done
don |
|