shell-条件判断

1. shell条件测试表达式

1.1 条件表达式种类

1. test <测试表达式>
[root@shell scripts]# test 2 = 3 && echo ok || echo no 
no
root@shell scripts]# test 2=3  && echo ok || echo no
ok

2. [ <测试表达式> ]
[root@shell scripts]# [ 2 = 3 ] && echo ok || echo no
no
[root@shell scripts]# [2 = 3] && echo ok || echo no
-bash: [2: 未找到命令
no

3. [[ <测试表达式> ]] #现代的shell语法
[root@shell scripts]# [[ 5 = 3 ]] && echo ok || echo no
no
[root@shell scripts]# [[5>5]] && echo ok || echo no
-bash: [[5: 未找到命令
no

4. ((<测试表达式>))  #不需要空格缩进
[root@shell scripts]# (( 5 == 3 )) && echo ok || echo no
no
[root@shell scripts]# ((5>5)) && echo ok || echo no
no
1.1.1 条件表达式的表达方法

[root@shell scripts]# [ 5 = 3 ] && echo ok || echo no
no

&&并且    ||或者
前面成功,执行echo ok ,否则执行echo no

1. 2 文件测试

[ -e dir|file ] 存在则为真
[ -d dir ] 目录是否存在
[ -f file ] 文件是否存在 
[ -r file ] 文件是否有读权限
[ -x file ] 文件是否有执行权限
[ -w file ] 文件是否有写权限

常用操作

[root@shell scripts]# [ -f /etc/hosts ] && echo '文件存在' || echo '文件不存在'
文件存在
[root@shell scripts]# [ -d /etc ] && echo '目录存在' || echo '目录不存在'
目录存在

1.3 整数测试

数值比较 [ 整数 1 操作符 整数 2 ]
[ 1 -gt 10 ] 大于
[ 1 -lt 10 ] 小于
[ 1 -eq 10 ] 等于
[ 1 -ne 10 ] 不等于
[ 1 -ge 10 ] 大于等于
[ 1 -le 10 ] 小于等于
注:建议
在(()) 中使用
==或=
!=
>
>=
<
<=

[]
[root@shell scripts]# [ 1 -gt 2 ] && echo yes || echo no
no
[root@shell scripts]# [ 1 -ge 2 ] && echo yes || echo no
no
[root@shell scripts]# [ 1 -eq 2 ] && echo yes || echo no
no
[root@shell scripts]# [ 1 -lt 2 ] && echo yes || echo no
yes
[root@shell scripts]# [ 1 > 2 ] && echo yes || echo no
yes
[root@shell scripts]# [ 1 > 1 ] && echo yes || echo no
yes
[root@shell scripts]# [ 1 \< 1 ] && echo yes || echo no
no
[root@shell scripts]# [ 1 \> 1 ] && echo yes || echo no
no
[[]]
[root@shell scripts]# [[ 1 > 1 ]] && echo yes || echo no
no
[root@shell scripts]# [[ 1 < 1 ]] && echo yes || echo no
no
[root@shell scripts]# [[ 1 -eq 1 ]] && echo yes || echo no
yes
[root@shell scripts]# [[ 1 -lt 1 ]] && echo yes || echo no
no
[root@shell scripts]# [[ 1 -ne 1 ]] && echo yes || echo no
no
[root@shell scripts]# [[ 1 <= 1 ]] && echo yes || echo no
-bash: 条件表达式中有语法错误
-bash: `1' 附近有语法错误
[root@shell scripts]# [[ 1 >= 1 ]] && echo yes || echo no
-bash: 条件表达式中有语法错误
-bash: `1' 附近有语法错误
(())
[root@shell scripts]# (( 1 >= 2 )) && echo yes || echo no
no
[root@shell scripts]# (( 3 >= 2 )) && echo yes || echo no
yes

小结:
[]可以-eq,<等符号,但要转义,不能用>=和<=。
[[]]可以直接用>和-eq等的符号不能用>=和<=。
(()) 都可以使用,但是不能用-eq等符号

1.4 多整数测试

[]符号
-a 并且
-o 或者
[[]]
&& 并且
|| 或者

[root@shell scripts]# [ 1 -lt 2 -a 5 -gt 10 ] && echo ok ||  echo no
no
[root@shell scripts]# [ 1 -lt 2 -o 5 -gt 10 ] && echo ok ||  echo no
ok
[root@shell scripts]# [[ 1 -lt 2 || 5 -gt 10 ]] && echo ok ||  echo no
ok
[root@shell scripts]# [[ 1 -lt 2 && 5 -gt 10 ]] && echo ok ||  echo no
no

1.5 字符测试

= 检测两个字符串是否相等,相等返回 true。
!= 检测两个字符串是否不相等,不相等返回 true。
-z 检测字符串长度是否为0,为0返回 true。
-n 检测字符串长度是否不为0,不为0返回 true。

[root@shell scripts]# [ "$a" = "111" ] && echo ok || echo no
no
[root@shell scripts]# [ "$a" != "111" ] && echo ok || echo no
ok
[root@shell scripts]# [ -z "$a" ] && echo ok || echo no
no
[root@shell scripts]# [ -n "$a" ] && echo ok || echo no
ok
[root@shell scripts]# a=0
[root@shell scripts]# [ -n "$a" ] && echo ok || echo no
ok

注意:
1)字符串必须加双引号
2)=、!=两端必须要有空格。

1.6 正则匹配

正则需要使用条件符号 [[]]
=~表示正则匹配

[root@shell scripts]# a="haoge"
[root@shell scripts]# [ "$a" =~ ^r ];echo $?
-bash: [: =~: 期待二元表达式
2
[root@shell scripts]# [[ "$a" =~ ^r ]];echo $?
1
[root@shell scripts]# [[ "$a" =~ ^[0-9]+$ ]];echo $?
0
[root@shell scripts]# a=123abc
[root@shell scripts]# [[ "$a" =~ [abc] ]];echo $?
0

2. if判断语句

2.1. 单分支结构

if [ 如果你有房 ];then 
    我就嫁给你
fi

2.2. 双分支结构

if [ 如果你有房 ];then 
        我就嫁给你
    else
        再见
fi

2.3. 多分支结构

if [ 如果你有房 ];then 
        我就嫁给你
elif [ 你有车 ];then
        我就嫁给你
elif [ 你有钱 ];then
        我就嫁给你
    else
        再见
fi

小结:

    单分支:一个条件一个结果
  双分支:一个条件两个结果
  多分支:多个条件多个结果

你可能感兴趣的:(shell-条件判断)