linux shell 入门学习笔记15 shell 条件测试

概念

shell的条件测试目的是得出真和假。

shell 提供的条件测试语法
test 命令
[] 中括号命令

语法*:
条件测试

test条件测试

test命令用来评估一个表达式,他的结果是真,还是假,如果条件为真,那么命令执行状态结果就为0,否则就是不为0,通过$?取值。

test命令参数
============检测给定文件名是否存在
-e 判断该文件是否存在,(普通文件,目录),存在就为真,否则为假
============检测给定文件名的类型
-f 判断该文件名是否为文件
-d 判断该文件名是否为目录
-b 判断该文件名是否为块设备
-c 判断该文件名是否为字符设备
-s 判断该文件名是否为socket
-p 判断该文件名是否为管道FIFO
-L 判断该文件名是否为连接
============检测文件的权限
-r 判断该文件名是否可读
-w 判断该文件名是否可写
-x 判断该文件名是否可运行
-u 判断该文件名是否有SUID属性
-g 判断该文件名是否有SGID属性
-k 判断该文件名是否有Sticky bit属性
-s 判断该文件名是否为空白文件
============文件比较 如test file1 -nt file2
-nt (newer than)判断file1是否比file2新
-ot(older than)判断file1是否比file2旧
-ef 判断file1与file2是否为同一个文件,可用在判断hard link的判定上,主要意义在判定两个文件是否均指向同一个inode
============整数之间判定
-eq 两数值相等
-ne 两数值不等
-gt n1大于n2 (greater than)
-lt n1小于n2(less than)
-ge n1大于等于n2 (greater than or equal)
-le n1小于等于n2(less than or equal)
============判定字符串数据
-z 判断字符串是否为0,若字符串为空,则为true
-n 判断字符串是否为非0,若字符串为非空,则为true
= 判断str1与str2是否相等,相等则为true
!= 判断str1与str2是否不想等,不相等则为true
============多重条件判断
-a (and)两状态同时成立,则返回true
-o (or)两状态任意一个成立,则返回true
! 反向状态

test命令实践

-e演示

xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ echo $?
0
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ echo $?
1
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh && echo 文件已存在
文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 && echo 文件已存在
xiao123@xiao123:~/Downloads/shscripts$ test -e del_data.sh12 || echo 文件不存在
文件不存在
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
xiao123@xiao123:~/Downloads/shscripts$ test -e hello && echo 该文件/目录已存在,不再执行创建动作 || mkdir hello
该文件/目录已存在,不再执行创建动作
xiao123@xiao123:~/Downloads/shscripts$

-f演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -f hello && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$

-d演示

xiao123@xiao123:~/Downloads/shscripts$ ls
calculation.sh           chaochao_5_finished.png  index.html.11  index.html.2   index.html.28  index.html.6
chaochao_1_finished.png  chaochao_5.jpg           index.html.12  index.html.20  index.html.29  index.html.7
chaochao_1.jpg           del_data.sh              index.html.13  index.html.21  index.html.3   index.html.8
chaochao_2_finished.png  expr_test1.sh            index.html.14  index.html.22  index.html.30  index.html.9
chaochao_2.jpg           expr_test.sh             index.html.15  index.html.23  index.html.31  let_test.sh
chaochao_3_finished.png  hello                    index.html.16  index.html.24  index.html.32
chaochao_3.jpg           index.html               index.html.17  index.html.25  index.html.33
chaochao_4_finished.png  index.html.1             index.html.18  index.html.26  index.html.4
chaochao_4.jpg           index.html.10            index.html.19  index.html.27  index.html.5
xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

-z/-n演示

xiao123@xiao123:~/Downloads/shscripts$ test -d hello && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z "" && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ test -z " " && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n "" && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ test -n " " && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$
中括号条件测试

脚本中经常进行条件测试,用的最多的都是中括号。
test和[]的作用是一样的。
注意点:中括号前后都要有空格[ ]

[ -n “${filename}” ]
注意在条件测试中,使用变量必须要添加双引号""

xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file=del_data.sh12
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
双中括号条件测试

双中括号增加了正则表达式的支持,其他与中括号相同。

变量测试

把字符串写入变量中
对变量测试,必须要加双引号

xiao123@xiao123:~/Downloads/shscripts$ file1="鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是个好人
xiao123@xiao123:~/Downloads/shscripts$ touch "鸡你太美.jpg"
xiao123@xiao123:~/Downloads/shscripts$ [[ -f "${file1}" ]] && echo "我是两年半的练习生,鸡你太美" || echo "我是个好人"
我是两年半的练习生,鸡你太美
xiao123@xiao123:~/Downloads/shscripts$
字符串测试

比较两个字符串变量的值,是否相等,不等的情况。

= 判断是否相等
!= 判断不相等
! 取结果的反义,颠倒黑白

注意:
对于字符串变量的比较一定要给变量添加双引号
使用等于号判断,左右两边必须有空格

演示

xiao123@xiao123:~/Downloads/shscripts$ [ ! -f "糖果超甜组合.txt" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ [ -f "糖果超甜组合.txt" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
数值比较测试

操作方式
数值计算

  1. 在中括号以及test中数值比较的用法

在中括号中,使用数学比较符号,请添加转义符号

xiao123@xiao123:~/Downloads/shscripts$ [ 2 > 1 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 > 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 1 \> 2 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ [ 2 \= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 2 != 2 ] && echo yes || echo no
no   #不等于可以不用添加转义
xiao123@xiao123:~/Downloads/shscripts$ [ 3 != 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 2 ] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [ 3 \!\= 3 ] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$
  1. 双中括号
    对中括号的补充,双中括号还支持正则处理。
    在双中括号中不要转义字符。
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 > 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 < 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 = 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 != 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -ge 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -le 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -gt 6 ]] && echo yes || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ 5 -lt 6 ]] && echo yes || echo no
yes
xiao123@xiao123:~/Downloads/shscripts$
逻辑操作符号测试

&& -a 与运算, 两边都为真,结果才为真
|| -o 或运算,两边有一个为真,结果为真
逻辑操作

中括号逻辑运算比较

xiao123@xiao123:~/Downloads/shscripts$ file1=/etc/init.d/rsync
xiao123@xiao123:~/Downloads/shscripts$ file2=/etc/hostname
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$ file1=/tmp/sqqqq
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -a -f "${file2}" ] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [ -f "${file1}" -o -f "${file2}" ] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

双中括号运算比较

xiao123@xiao123:~/Downloads/shscripts$ str1=""
xiao123@xiao123:~/Downloads/shscripts$ str2="yuyu"
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" && -n "${str2}" ]] && echo ok || echo no
no
xiao123@xiao123:~/Downloads/shscripts$ [[ -n "${str1}" || -n "${str2}" ]] && echo ok || echo no
ok
xiao123@xiao123:~/Downloads/shscripts$

脚本开发

要求:接收用户输入,判断它是否等于某个数字。

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 3
脚本出错,必须输入1和2
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 1
1
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_input.sh
Please input a char: 2
2
xiao123@xiao123:~/Downloads/shscripts$ cat ./test_input.sh
#! /bin/bash

read -p "Please input a char: " var1

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

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

[ "${var1}" != 2 -a "${var1}" != 1 ] && {
        echo "脚本出错,必须输入1和2"
        exit 1
}
xiao123@xiao123:~/Downloads/shscripts$

要求:安装lnmp/lamp脚本开发。

xiao123@xiao123:~/Downloads/shscripts$ mkdir test
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LAMP is installed" > ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ echo "echo LNMP is installed" > ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lamp.sh
xiao123@xiao123:~/Downloads/shscripts$ chmod +x ./test/lnmp.sh
xiao123@xiao123:~/Downloads/shscripts$

源码

xiao123@xiao123:~/Downloads/shscripts$ cat ./test_install.sh
#! /bin/bash

path=./test

[ ! -d  "${path}" ] && mkdir ${path} -p

cat << END
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
END

read num

expr ${num} + 1 &>/dev/null

[ $? -ne 0 ] && {
        echo "input number must be {1|2|3}"
        exit 1
}

[ ${num} -eq 1 ] && {

        echo "Strating installing lamp.....waiting..."
        sleep 2

        [ -x ${path}/lamp.sh ] || {
                echo "The file does not exist or can't be execut."
                exit 1
        }

        ${path}/lamp.sh
        exit $?
}

[ ${num} -eq 2 ] && {

        echo "Strating installing lnmp.....waiting..."
        sleep 2

        [ -x ${path}/lamp.sh ] || {
                echo "The file does not exist or can't be execut."
                exit 1
        }

        ${path}/lnmp.sh
        exit $?
}

[ ${num} -eq 3 ] && {
        echo "byebye"
        exit 3
}

[[ ! ${num} =~ [1-3] ]] && {
        echo "The number input must be {1|2|3}"
        exit 4
}
xiao123@xiao123:~/Downloads/shscripts$

运行结果

xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
e
input number must be {1|2|3}
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
3
byebye
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
1
Strating installing lamp.....waiting...
LAMP is installed
xiao123@xiao123:~/Downloads/shscripts$ bash ./test_install.sh
        1. [install lamp]
        2. [install lnmp]
        3. [exit]
        please input the num you want:
2
Strating installing lnmp.....waiting...
LNMP is installed
xiao123@xiao123:~/Downloads/shscripts$
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值