咨询热线:4006-75-4006

售前:9:00-23:30    备案:9:00-18:00    技术:7*24h

Shell基本运算符之文件测试运算符

2018-03-23 01:08:47 10037次

Shell基本运算符之文件测试运算符

文件测试运算符用于检测 Unix 文件的各种属性。

属性检测描述如下:


image.png

实例

变量 file 表示文件"/home/zgy.sy",它的大小为18字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:

image.png

#!/bin/bash
file="/var/www/runoob/test.sh"
if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi
if [ -w $file ]
then
   echo "文件可写"
else
   echo "文件不可写"
fi
if [ -x $file ]
then
   echo "文件可执行"
else
   echo "文件不可执行"
fi
if [ -f $file ]
then
   echo "文件为普通文件"
else
   echo "文件为特殊文件"
fi
if [ -d $file ]
then
   echo "文件是个目录"
else
   echo "文件不是个目录"
fi
if [ -s $file ]
then
   echo "文件不为空"
else
   echo "文件为空"
fi
if [ -e $file ]
then
   echo "文件存在"
else
   echo "文件不存在"
fi


执行脚本,输出结果如下

image.png


首页
最新活动
个人中心