073_扩展篇_Shell编程(五)_条件判断 条件判断 命令 test 基本语法 test condition [condition] (注意 condition 前后要有空格) 注意 条件非空为true,[abc]返回true,[ ]返回false 常用判断条件 两个整数之间比较 -eq 等于 (equal) -lt 小于 (less than) -gt 大于 (greater than) -ne 不等于 (not equal) -le 小于等于 (less equal) -ge 大于等于 (greater equal) 如果是字符串之间的比较,用等号"="判断相等, 用"!="判断不等 按照文件权限进行判断 -r 有读权限 (read) -w 有写权限 (write) -x 有执行权限 (execute) 按照文件类型进行判断 -e 文件存在 (existence) -f 文件存在并且是一个常规的文件 (file) -d 文件存在并且是一个目录 (directory) 多条件判断 && 前一条命令执行成功 才 执行后一条 || 前一条命令执行失败 才 执行后一条 实例 判断a是否等于hello [root@hadoop100 tf]# a=hello [root@hadoop100 tf]# test $a = hello [root@hadoop100 tf]# echo $? 0 [root@hadoop100 tf]# test $a = Hello [root@hadoop100 tf]# echo $? 1 [root@hadoop100 tf]# [ $a = hello ] [root@hadoop100 tf]# echo $? 0 比较整数 [root@hadoop100 tf]# [ 2 -lt 8 ] [root@hadoop100 tf]# echo $? 0 判断文件权限 [root@hadoop100 tf]# [ -r hello.sh ] [root@hadoop100 tf]# echo $? 0 [root@hadoop100 tf]# [ -x test ] [root@hadoop100 tf]# echo $? 1 判断文件类型 [root@hadoop100 tf]# [ -e test ] [root@hadoop100 tf]# echo $? 0 [root@hadoop100 tf]# [ -e /root/tf/test ] [root@hadoop100 tf]# echo $? 0 多条件判断 [root@hadoop100 tf]# [ abc ] && echo OK || echo notOK OK [root@hadoop100 tf]# [ ] && echo OK || echo notOK notOK