跳转至

2. 条件测试

补充:read命令

read -t 5 -p "please input your name and age in 5 seconds!!:" name age

2.1 分类

1. test
2. []
3. [[]]
4. (())

2.1.1 test命令

#判断是否存在
test -e file
echo $?

1.文件类型检测

参数 说明 备注
-e 是否存在 常用
-f 是否是file类型 常用
-d 是否是directory类型 常用
-b 是否是block device
-c 是否是character device
-S 是否是Socket文件
-p 是否是FIFO(pipe)文件
-L 是否是连结档

2.文件权限检测

参数 说明 备注
-r 是否可读
-w 是否可写
-x 是否可执行
-u 是否有【SUID】属性
-g 是否有【SGID】属性
-k 是否有【sticky bit】属性
-s 是否是非空白文件

3.两文件间比较

参数 用法 说明
-nt test f1 -nt f2 判断f1是否比f2新
-ot test f1 -ot f2 判断f1是否比f2旧
-ef test f1 -ef f2 判断f1、f2是否同一个文件

4.两整数之间的判定

#参数 -eq -ne -gt -lt -ge -le
test n1 -eq n2

5.判断字符串

#判断string是否为空字符串,是则返回0
test -z string
echo $?
#这个貌似是与上面相反
test -n string
echo $?
#判断字符串是否相等,相等则返回0
test str1=str2 
echo $?
test str1!=str2
echo $?

字符串运算符

运算符 说明 举例(\(a=abc,\)b=efg)
= 检测2个字符串是否相等,相等返回true [ \(a=\)b ] 返回false
!= 检测2个字符串是否相等,不相等返回true [ \(a!=\)b ] 返回true
-z 检测字符串长度是否为0,为0返回true [ -z $a ] 返回false
-n 检测字符串长度是否不为0,不为0返回true [ -n $a ] 返回true
$ 检测字符串是否为空,不为空返回true [ $a ] 返回true

6.多重条件判断

#参数 
1.-a and,两条件同时成立则返回0;
2.-o or,两条件之一成立则返回0;
3.!取反
test -r file -a -x file
echo $?

7.案例

test -e file1 && echo "yes,file1 exists"||echo "no,file1 does not exists"
test -e file1 && echo "yes,file1 exists"||touch file1
test -f f.txt && echo ok ||echo no
test -d home && echo ok ||echo no
test -z "" && echo ok ||echo no
test -z " " && echo ok || echo no
test -n "" && echo ok || echo no

2.1.2 中括号[]

#注意:
1.中括号必须加空格!!!
2.条件测试中使用变量,必须加双引号!!!
[  -n "$filename"  ]
file1="happy.txt"
[  -f "$file1"  ] && echo ok || echo no
dir_name="/home"
[ -d dir_name ] && echo ok ||echo no
[  -r "test.txt"  ] && cat test.txt || echo "you can't read me!"
[ -w "t2.txt" ] && (echo "hahahahahha">> t2.txt)||echo "you can't write"

2.1.3 双中括号

1.双中括号不需要转义字符

2.双中括号支持正则

[[ 5 > 6 ]] && echo yes || echo no
[[ 5 -lt 6 ]] && echo yes || echo no

2.1.4 逻辑判断

1.分类

test [] [[]] (()) 说明
-a && and
-o || or
! ! not

2.举例

a=""
b="chupeng"
[ -n "$a" -a "$a" = "$b" ] && echo yes|| echo no
[[ -n "$a" && "$a" = "$b" ]] && echo yes || echo no

3.只能输入1or2的脚本

vim and_or.sh
#!/bin/bash

read -p "pls input a char:" var1

[  "$var1" -eq "1"  ] && {
    echo $var1
    exit 0
}

[  "$var1" = "2"  ] && {
    echo $var1
    exit 0
}

[  "$var1" != "2" -a "$var1" != "1"  ] && {
    echo "you must input 1 or 2!"
    exit 1
}

4.安装lnmp/lamp脚本

#lamp.sh  lnmp.sh
echo "echo lamp is installed" >./lamp.sh
echo "echo lnmp is installed" > ./lnmp.sh
chmod +x ./*
vim lamp_lnmp.sh
#!/bin/bash
#判断脚本文件路径是否存在
path=/myscripts
[  ! -d "$path"  ] && mkdir $path -p
#提示信息
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the number you want:
END
#读取输入信息并判断
read num 
# 这里执行完后,为 $? 啥输出不是0???
expr $num +1 &> /dev/null

[  $? -ne 0  ]  && {
    echo "the num you input must be {1|2|3}"
    exit 1
}
#选择1的情况
[  "$num" -eq "1" ] && {
    echo "start installing lamp..."
    sleep 2
    [ -x "$path/lamp.sh" ] || {
        echo "the lamp.sh does not exist or can't be exec."
        exit 1
    }
    $path/lamp.sh
    exit $?
}
#选择2的情况
[  "$num" -eq "2"  ] && {
    echo "start installing lnmp..."
    sleep 2
    [  -x "$path/lnmp.sh"  ] || {
        echo "lnmp.sh is error..."
        exit 1
    }
    $path/lnmp.sh
    exit $?
}
#选择3
[ "$num" -eq 3 ] && {
    echo "bye..."
    exit 3
}
#限制用户必须输入1|2|3
[[ ! "$num" = ~ [1-3]  ]] && {
    echo "you must input num in {1|2|3}"
    exit 4
}

2.1.5 字符串比较

name1="chupeng"
[ "${name1}" = "chupeng" ] && echo ok || echo no

2.1.6 数值比较

#注意:中括号中使用数学比较符号,必须加转义字符!!!
[  2 \> 1  ] && echo yes || echo no
[  2 \= 1  ] && echo yes ||echo no

test和[ ] 支持数学比较符号和-eq类型


最后更新: 2022-02-19 13:59:07