shell脚本学习(3)文件判断

  shell常用的文件判断运算符如下:

-e 文件是否存在

-f  文件是否是普通文件(不是目录、设备文件、链接文件)

-s  表示文件大小不为0

-d 表示文件是否是目录

-b 表示是块设备(光驱、软盘等)

-c  表示是字符设备(键盘、声卡等)

-p 表示是管道

-h 表示是符号链接

-S 表示是否是socket

-r、-w、-x表示文件是否有可读、可写、可执行权限(指运行这个测试命令的用户)

f1 -nt f2      f1是否比f2新(new than)

f1 -ot f2      f1是否比f2旧(old than)

f1 -ef f2      f1和f2是否是相同文件的硬链接

       使用!时表示上述结果取反,由于内容较多,这里不一一列举了。下面一个例子可以作为编程参考

myfile="aa.txt"
if [ ! -f $myfile ]; then
    echo $myfile" is not exist"
    touch $myfile
else
    echo $myfile" is exist"
fi

if [ ! -s $myfile ]; then
    echo "hello, my master" > $myfile
else
    echo $myfile" is not null"
fi
原文地址:https://www.cnblogs.com/me80/p/7990007.html