Linux shell编程自动化运维 判断,条件测试和 if 流程控制 详解

shell条件测试

表达格式

格式一: tesr 条件表达式
格式二: [ 条件表达式 ] //注意空格
格式三: [[ 条件表达式 ]] //注意空格

文件测试 [操作符,文件或目录]

-f filename         : 当filename 存在时返回真
-b filename        : 当filename 存在并且是块文件时返回真(返回0)
-d pathname      : 当pathname 存在并且是一个目录时返回真
-h filename        : 当filename 存在并且是符号链接文件时返回真 (或 -L filename)
-u pathname      : 当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真
-c filename        : 当filename 存在并且是字符文件时返回真
-e pathname     : 当由pathname 指定的文件或目录存在时返回真
-g pathname     : 当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真
-k pathname     : 当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真
-p filename       : 当filename 存在并且是命名管道时返回真
-r pathname     : 当由pathname 指定的文件或目录存在并且可读时返回真
-s filename       : 当filename 存在并且文件大小大于0 时返回真
-S filename       : 当filename 存在并且是socket 时返回真
-t fd                  : 当fd 是与终端设备相关联的文件描述符时返回真
-w pathname      : 当由pathname 指定的文件或目录存在并且可写时返回真
-x pathname       : 当由pathname 指定的文件或目录存在并且可执行时返回真
-O pathname      : 当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写)
-G pathname      : 当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真
file1 -nt file2       : file1 比file2 新时返回真
file1 -ot file2      : file1 比file2 旧时返回真
特殊符
! 取反,判断不成功才执行

示例

-d  :  这是一个目录
  test -d /home
  echo $?                   
0                           //返回值0,表示成功
-----------------------------------------------------
  test -d  /jgsadhgl;sjgs
  echo $?
1                          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值