小编Jam*_*man的帖子

在 bash shell 中添加文件大小

我编写了一个 shell 脚本,它将目录作为参数并打印文件名和大小,我想知道如何将文件大小相加并存储它们,以便我可以在循环后打印它们。我已经尝试了一些东西,但到目前为止还没有取得任何进展,有什么想法吗?

#!/bin/bash

echo "Directory <$1> contains the following files:"
let "x=0"
TEMPFILE=./count.tmp
echo 0 > $TEMPFILE
ls $1 |
while read file
do
        if [ -f $1/$file ]
        then
                echo "file: [$file]"
        stat -c%s $file > $TEMPFILE
        fi
cat $TEMPFILE
done
echo "number of files:"
cat ./count.tmp
Run Code Online (Sandbox Code Playgroud)

帮助将不胜感激。

bash shell loops file add

2
推荐指数
1
解决办法
2216
查看次数

标签 统计

add ×1

bash ×1

file ×1

loops ×1

shell ×1