求指点:怎样用Shell编写一个以若干文件名作为运行参数的脚本,脚本功能是对这些文件的大小求和。
该回答引用ChatGPT
编写一个shell脚本来计算文件名参数所指定文件的大小总和:
shell
#!/bin/bash
sum=0
for file in "$@"
do
if [ -f "$file" ]
then
size=$(stat -c%s "$file")
sum=$((sum+size))
echo "$file size: $size"
else
echo "$file is not a file!"
fi
done
echo "Total size: $sum"
然后给予该脚本可执行权限:
shell
chmod +x script.sh
然后可以这样运行该脚本:
shell
./script.sh file1.txt file2.mp4 file3.jpg
该脚本会: