免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1227 | 回复: 9
打印 上一主题 下一主题

晕!新注册禁言,憋了我24小时。。。发贴求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-11 11:33 |只看该作者 |倒序浏览
第一次写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

论坛徽章:
0
2 [报告]
发表于 2006-06-11 11:45 |只看该作者
顺便问下关于自增,我用 incr i 或者 let "i++"这种命令在我的环境下都不能运行。
说实话我也不知道环境具体是什么,反正是Sun的Unix的客户端。用Putty软件连上服务器的。

论坛徽章:
0
3 [报告]
发表于 2006-06-11 13:14 |只看该作者

  1. #!/bin/sh                                                                                                                                                   
  2. for f                                                                                                                                                        
  3. do                                                                                                                                                           
  4.       echo $f: $(( 100 * `grep '^#' $f |wc -l` / `wc -l <$f` ))%                                                                                                   
  5. done   
复制代码

论坛徽章:
0
4 [报告]
发表于 2006-06-11 13:30 |只看该作者
原帖由 galilette 于 2006-6-11 13:14 发表
[code]
#!/bin/sh                                                                                                                                                   
for f                          ...


有错误:

syntax error at line 4: `(' unexpected

论坛徽章:
0
5 [报告]
发表于 2006-06-11 13:47 |只看该作者
awk '/^#/{b[FILENAME]++}END{for( i in b )print i,b[FILENAME]}' $*
好象不符和要求
这个没问题了
awk '{a[FILENAME]++;if($1~/^#/)b[FILENAME]++}END{for( j in b )print i,100*b[j]/a[j]"%"}' $*

[ 本帖最后由 shitou9000 于 2006-6-11 13:54 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-06-11 14:11 |只看该作者
谢谢楼上的。现在我还不懂Awk,惭愧。

可以运行了。
输入 percentcomment "file1 file2"
输出   50%
           40%
但和题意还是不符啊。首先应该打印File的名字

Given a list of filenames as arguments 到底是什么意思呢。"file1 file2" 这种算不算一个List? 不知道有没有理解错。。。

论坛徽章:
0
7 [报告]
发表于 2006-06-11 14:46 |只看该作者
自己又写了个。。。

#!/bin/sh                                                                                                                                                   
for filename in $*
do
    grep '^#' $filename >temp
    total=`wc -l <$filename`
    com=`wc -l <temp`
    echo $filename $[100*$com/$total]


done

基本上好像对了,就是输出不对,因为我不会正确表达算术运算。。。

论坛徽章:
0
8 [报告]
发表于 2006-06-11 15:01 |只看该作者
用这样的参数
脚本名 file1 file2 file3 file4 ……

论坛徽章:
0
9 [报告]
发表于 2006-06-11 15:07 |只看该作者
谢谢shitou9000  基本上搞定了

#!/bin/sh                                                                                                                                                   
for filename in $*
do
    grep '^#' $filename >temp
    total=`wc -l <$filename`
    com=`wc -l <temp`
   
    echo $filename  `expr 100 \* $com \/ $total`%


done

论坛徽章:
0
10 [报告]
发表于 2006-06-11 15:10 |只看该作者
不知道为何
total=`wc -l <$filename` 和 total=`wc -l $filename` 的区别是什么??
输出的时候$total分别为 10  和 10 filename
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP