shell脚本如何判断文件大小

1 、ls -l

ls -l $filename | awk '{print $5}'

执行结果:
[root@localhost opt]# ls -l test.txt
-rw-r--r--. 1 root root 4 Jun 21 11:40 test.txt
[root@localhost opt]# ls -l test.txt | awk '{print $5}'
4

2、shell -s $filename

文件大小非0时为真

if [ ! -s $filename ]
then
    echo "$filename 文件大小为0!"
    exit 1
fi

3、shell脚本判断

[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a$b是否相等
-r file     用户可读为真
-w file     用户可写为真
-x file     用户可执行为真
-f file     文件为正规文件为真
-d file     文件为目录为真
-c file     文件为字符特殊文件为真
-b file     文件为块特殊文件为真
-s file     文件大小非0时为真
-t file     当文件描述符(默认为1)指定的设备为终端时为真

猜你喜欢

转载自blog.csdn.net/lovegengxin/article/details/80762329
今日推荐