Shell 中判断条件的使用(本文只列出并常用的)

1.文件的检测

test -option file

-e file是否存在

-f file是否是文件

-d file是否是目录

-s file是否为空白文件

-r 是否可读

-w 是否可写

-x 是否可执行

 

2.两个文件之间的比较

test file1 -nt file2       file1 is newer than file2 

test file1 -ot file2       file1 is older than file2

 

3.两个整数之间的比较

test n1 -option n2

-eq    n1 equal n2

-ne    n1 not equal n2

-gt     n1 is greater than n2

-lt      n1 is less than n2

-ge    n1 is greater than or equal n2

-le     n1 is less than or equal n2

 

4.字符串之间的比较

test    -z   string     判断string是否为0,若为空串,返回true

test    -n  string     判断string是否不为0,若为空串,返回false

 

5.多重条件判断

test   -r  file    -a     -w file

-a  and

-o  or

!  not

 

 

eg1:test -e file && echo "文件file存在!"

eg2:  test file1 -nt file2 && echo "file1 is newer than file2 !"    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值