Linux学习---shell编程(11-条件判断式)

来源:互联网 发布:js修改div背景颜色 编辑:程序博客网 时间:2024/04/30 16:18

1. 按照文件类型进行判断

-b

判断文件是否存在,并且是否为块设备文件

-c

判断文件是否存在,并且是否为字符串设备文件

-d

判断文件是否存在,并且是否为目录文件

-e

判断文件是否存在

-f

判断文件是否存在,并且是否为普通文件

-L

判断文件是否存在,并且是否为符号链接文件

-p

判断文件是否存在,并且是否为管道文件

-s

判断文件是否存在,并且是否非空

-S

判断文件是否存在,并且是否为套接字文件

 

yesimingdeMac-mini:testShell yesiming$ ll

total 40

drwxr-xr-x   7 yesiming  staff   238B  8 30 14:12.

drwxr-xr-x@ 53 yesiming  staff   1.8K  8 30 14:30..

-rwxr-xr-x   1 yesiming  staff    81B  8 29 16:24pid.sh

-rwxr-xr-x   1 yesiming  staff   251B  8 29 16:38read.sh

-rwxr-xr-x   1 yesiming  staff    86B  8 29 16:04test.sh

-rwxr-xr-x   1 yesiming  staff   113B  8 29 16:09test2.sh

-rw-r--r--   1 yesiming  staff    59B  8 30 14:12 testCut.txt

yesimingdeMac-mini:testShell yesiming$test -e pid.sh

yesimingdeMac-mini:testShell yesiming$echo $?

0

yesimingdeMac-mini:testShell yesiming$[ -e pid.sh.bak ]

yesimingdeMac-mini:testShell yesiming$echo $?

1

yesimingdeMac-mini:testShell yesiming$

 

yesimingdeMac-mini:testShell yesiming$ [ -e pid.sh ] && echo yes || echo no

yes

yesimingdeMac-mini:testShell yesiming$ [ -e pid.sh1 ] && echo yes || echo no

no

yesimingdeMac-mini:testShell yesiming$

 

2. 按照文件权限进行判断

-r

判断文件是否存在,并且是否有读权限

-w

是否存在,是否有写权限

-x

是否存在,是否有执行权限

-u

是否存在,是否有SUID权限

-g

是否存在,是否有GUID权限

-k

是否存在,是否有SBit权限

 

3. 两个文件之间进行比较

文件1 –nt 文件2

判断文件1的修改时间是否比文件2的新

文件1 –ot 文件2

判断文件1的修改时间是否比文件2的旧

文件1 –ef 文件2

判断文件1是否喝文件2的inode一致。用于判断硬链接

yesimingdeMac-mini:testShell yesiming$ [ pid.sh -nt read.sh ] && echo yes || echo no

no

 

4. 两个整数之间比较

a –eq b

a与b相等

a –ne b

a与b不相等

a –gt b

a > b

a –lt b

a < b

a –ge b

a >= b

a –le b

a <= b

yesimingdeMac-mini:testShell yesiming$ [ 1 -gt 2 ] && echo yes || echo no

no

 

5. 字符串

-z 字符串

判断字符串是否为空(为空返回真)

-n 字符串

判断字符串是否非空(非空返回真)

str1 == str2

 

str1 != str2

 

 

6. 多重判断

判断1 –a 判断2

逻辑与

判断1 –o 判断2

逻辑或

yesimingdeMac-mini:testShell yesiming$ ls

pid.sh     read.sh    test.sh    test2.sh    testCut.txt

yesimingdeMac-mini:testShell yesiming$ [ -e pid.sh  -a  -e test.sh ] && echo "had" || echo "no"

had

yesimingdeMac-mini:testShell yesiming$ [ -e pid.sh  -a  -e test1.sh ] && echo "had" || echo "no"

no

yesimingdeMac-mini:testShell yesiming$ [ -e pid.sh  -o  -e test1.sh ] && echo "had" || echo "no"

had

yesimingdeMac-mini:testShell yesiming$

 

0 0
原创粉丝点击