我编写了一个 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)
帮助将不胜感激。