- 论坛徽章:
- 0
|
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 |
|