基于shell的if和else详解

2019-09-23 09:07:03于丽

-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

以上这篇基于shell的if和else详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易采站长站。