shell中if语句

1 篇文章 0 订阅

一、if语句
1、单分支语句结构
if <条件表达式>
then
指令
fi
例子:
if [ -f file ]
then
echo “是一个普通文件”
else
echo “不是一个普通文件”
exit 1
fi
######说明:如果file是一个普通文件,那么就执行echo “是一个普通文件” 否则 输出一条"不是一个普通文件" 最后退出脚本
2、多分支if语句结构
if <条件表达式1>
then
指令1
elif<条件表达式2>
then
指令2
else
指令3
fi
二、条件表达式:&&与 ||或
1、数值测试:
[ arg1 OP arg2 ] OP是-eq(等于), -ne(不等于), -lt(小于), -le(小于等于), -gt(大于), -ge(大于等于) 的其中之一,数值可正可负。注意[]要加空格号

2、字符串测试:
-z string 如果string长度为0则为真
-n string 如果string长度不为0则为真
string1 == string2 如果string1和string2相等则为真,=只应由test使用
string1 = string2 如果string1和string2相等则为真,=只应由test使用
string1 != string2 如果字符串不相等则为真
string1 < string2 如果按字典序string1在string2之前则为真
string1 > string2 如果按字典序string1在string2之后则为真

3、文件属性测试:
-a file 如果file存在则为真
-b file 如果file存在且为块文件则为真
-c file 如果file存在且为字符文件则为真
-d file 如果file存在且是目录则为真
-e file 如果file存在则为真
-f file 如果file存在且为普通文件则为真
-g file 如果file存在且置位设置-组ID则为真,见参考【1】第4.4、12.3节
-h file 如果file存在且为符号连接则为真
-k file 如果file存在且其粘性位置位则为真,参考man chmod
-p file 如果file存在且为命令管道(FIFO)则为真
-r file 如果file存在且可读则为真
-s file 如果file存在且文件长度大于0则为真
-t fd 如果文件描述符fd打开且指向为终端则为真
-u file 如果file存在且设置-用户-ID置位则为真,见参考【1】第4.4节
-w file 如果file存在且可写则为真
-x file 如果file存在且可执行
-G file 如果file存在且由有效组ID拥有则为真,见参考【1】第4.4节
-L file 如果file存在且为符号连接则为真
-N file 如果file存在且在上次读后有修改(modified)则为真
-O file 如果file存在且由有效用户ID拥有则为真,见参考【1】第4.4节
-S file 如果file存在且是一个套接字则为真
file1 -ef file2 如果file1和file2指向同一个设备的inode则为真
file1 -nt file2 如果file1比file新(modified),或者file1存在file2不存在在为真
file1 -ot file2 如果file1比file旧(modified),或者file1存在file2不存在在为真

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值