免费注册 查看新帖 |

Chinaunix

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

为什么这个Shell结果不是我想要的结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-22 15:52 |只看该作者 |倒序浏览
#!/bin/bash
directory="."
sum=0
ls "$directory" | more |
while read file
do
    file="$directory"/"$file"
    if [ -f "$file" ]
    then
        set -- `ls -l "$file"`
        sum=`expr $sum + $4`
    fi
done
echo "the size of all ordinary files in $directory is $sum bytes"
为什么最终结果sum总是0,但是在进行调试模式下#!/bin/bash -x,sum每次都在变化

论坛徽章:
0
2 [报告]
发表于 2007-10-22 17:10 |只看该作者
不知道你想要的结果是点样,但是我觉得 这两步是有点问题,
      set -- `ls -l "$file"`  这是什么意思,不是很明白,set --
        sum=`expr $sum + $4`

$4 未定义, 是否来处系统变量设定在,
sum=`expr $sum + $4` 是不是应该改为 sum=$[$sum+4] ;echo $sum
因为不知道你想要的结果,只能给你这些提示了,
在调试模下,你应看到expr 0+ 的错误,

论坛徽章:
0
3 [报告]
发表于 2007-10-22 18:25 |只看该作者
lz 的脚本有不少的问题

论坛徽章:
0
4 [报告]
发表于 2007-10-23 18:30 |只看该作者
set -- `ls -l "$file"`
--不解析ls -l file产生结果最前面的-,因为ls -l对于普通文件会产生-rwx....,这样set解析就不会正确

$4的字段表示文件的大小
我的程序目的是取得当前目录下所有文件的大小。没错有现成的命令可用,但是我想知道这么脚本在哪个地方错误,这是我修改后的结果,我感觉在shell里面是不是也有作用域问题。还有我的系统是unix-center的fedora系统,shell bash3.1

程序是
#!/bin/bash
directory="."
sum=0
ls "$directory" | more |
while read file
do
        file="$directory"/"$file"
        if [ -f "$file" ]
        then
                set -- `ls -l "$file"`
                sum=`expr $sum + $4`
                echo "until $file,the size is $sum"
        fi
done
echo "the size of all ordinary files in $directory is $sum bytes"


结果是
until ./addall,the size is 13739
until ./all_user,the size is 27478
until ./ar_test.a,the size is 41217
until ./ar_test.c,the size is 54956
until ./ar_test.o,the size is 68695
until ./a.txt,the size is 82434
until ./caller,the size is 96173
until ./caller.c,the size is 109912
until ./case_demo,the size is 123651
until ./catargs_demo,the size is 137390
until ./dext,the size is 151129
until ./diff2,the size is 164868
until ./display_name,the size is 178607
until ./error.log,the size is 192346
until ./file1,the size is 206085
until ./file3,the size is 219824
until ./file4,the size is 233563
until ./for_demo1,the size is 247302
until ./fs,the size is 261041
until ./fs_demo,the size is 274780
until ./if_demo,the size is 288519
until ./read_demo,the size is 302258
until ./sample,the size is 315997
until ./shift_demo,the size is 329736
until ./student_address,the size is 343475
until ./student_address_bak,the size is 357214
until ./student_address_bak2,the size is 370953
until ./swap_macro,the size is 384692
until ./swap_macro.c,the size is 398431
until ./swap_macro.c~,the size is 412170
until ./trap_demo,the size is 425909
until ./until_demo,the size is 439648
until ./user_info,the size is 453387
until ./while_demo,the size is 467126
until ./words_from_celebrity_in_unix.txt,the size is 480865
the size of all ordinary files in . is 0 bytes

论坛徽章:
0
5 [报告]
发表于 2007-10-23 20:14 |只看该作者

  1. sum=`expr $sum + $4`
复制代码


把 + 改为 '+' 看看

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2007-10-24 00:04 |只看该作者

  1. for file in *;do [[ -f $file ]] && ((sum+=$(stat -c %s $file)));done ;echo $sum
复制代码

论坛徽章:
0
7 [报告]
发表于 2007-10-25 23:47 |只看该作者

因为管道的问题?这里面有什么机制呢,不能修改里面修改的变量

通过管道产生的结果会产生错误
#!/bin/bash
sum=0
ls . | more |
while read a
do
        [[ -f $a ]] && ((sum+=$(stat -c %s $a)))
done
echo $sum

可以工作的程序
#!/bin/sh
directory="."
sum=0
for file in "$directory"/*
do
        if [ -f $file ]
        then
                set -- `ls -l $file`
                sum=`expr $sum + $4`
                echo "until $file,the size is $sum"
        fi
done
echo $sum
echo "the size of all ordinary files in $directory is $sum bytes"

但是为什么这样会错呢??

[ 本帖最后由 dirtysalt 于 2007-10-25 23:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP