Shell test 命令
1. Shell test 命令概述
在我们之前章节学习了 Shell 的运算符,其中大多数运算都可以结合 test
命令来完成测试与比较操作,test
命令配合各种操作符号,不仅可以完成测试逻辑表达式,还可以进行,数字,字符串的比较。本文是一些常见的归纳总结。
在 Shell 中每个命令都有其应用场景,test 命令也不例外,其应用场景几乎遍布我们整个 Shell 脚本的生命周期,基本上 Shell 脚本里面都会有其身影。
当我们对数值进行算术运算,当我们需要对进行判断,当我们需要对逻辑或字符串进行判断的时候,这些情况都需要使用 test 命令来配合运算符进行操作。
2. Shell test 命令简介
在我们学习 test 命令之前,我们需要知道在 Linux 中执行命令返回 0 为正常,返回非 0 表示异常,可以利用 $?
来返回值。在下面的 test 操作中我们就会运用此特性来进行测试比较。
test 命令
test 为 Shell 内置命令,其通常需要与 if 语句一块使用。
语法格式
语法格式为 test expression
, 当 test 判断 expression 为成立时,返回状态为 0,否则为非 0 值。
test 命令还可以简写为 [ ]
, 需要在两边中括号与 expression 是有空格,这个空格是必须的,否则会导致语法。[]
的写法更加简洁,比 test 使用频率更高。
我们可以简单来看示例:
[root@master shell_test]# cat test1.sh #!/bin/bash// 使用中括号if test -ne ;thenecho "true"elseecho "false"fi[root@master shell_test]# bash test1.sh true[root@master shell_test]# cat test2.sh #!/bin/bash// 使用testif [ -eq ];thenecho "true"elseecho "false"fi[root@master shell_test]# bash test2.sh true
3. test 命令操作
test 命令操作主要配合比较运算符进行,可大体分为判断 / 数组比较 / 字符串判断 / 逻辑运算等。
数组比较其实就是 test 命令配合关系运算符进行数字比较。
关系运算顾名思义就是比较数字的大小,注意关系运算符作用的对象为数字。
#!/bin/bashnum1=num2=echo "num1 为:${num1}"echo "num2 为:${num2}"if [ $num1 -eq $num2 ]then echo "$num1 -eq $num2 : num1 等于 num2"else echo "$num1 -eq $num2: num1 不等于 num2"fiif [ $num1 -ne $num2 ]then echo "$num1 -ne $num2: num1 不等于 num2"else echo "$num1 -ne $num2 : num1 等于 num2"fiif [ $num1 -gt $num2 ]then echo "$num1 -gt $num2: num1 大于 num2"else echo "$num1 -gt $num2: num1 不大于 num2"fiif [ $num1 -lt $num2 ]then echo "$num1 -lt $num2: num1 小于 num2"else echo "$num1 -lt $num2: num1 不小于 num2"fiif [ $num1 -ge $num2 ]then echo "$num1 -ge $num2: num1 大于或等于 num2"else echo "$num1 -ge $num2: num1 小于 num2"fiif [ $num1 -le $num2 ]then echo "$num1 -le $num2: num1 小于或等于 num2"else echo "$num1 -le $num2: num1 大于 num2"fi
运行结果为:
num1 为: num2 为: -eq : num1 不等于 num2 -ne : num1 不等于 num2 -gt : num1 不大于 num2 -lt : num1 小于 num2 -ge : num1 小于 num2 -le : num1 小于或等于 num
在本章节由于 test 命令与运算符配合使用,与之前运算符章节有重复,我们在此温故知新,着手 test 命令来学习判断。
测试在我们编写 Shell 中与操作非常常用,熟练掌握操作可以在后续的 Shell 编写中得心应手,例如 file 变量为:
例如:
#!/bin/bashTEST_FILE="/etc/fstab"echo "检测的为:${TEST_FILE}"echo "信息为:$(ls -l ${TEST_FILE})"if [ -r $TEST_FILE ]then echo "可读"else echo "不可读"fiif [ -w $TEST_FILE ]then echo "可写"else echo "不可写"fiif [ -x $TEST_FILE ]then echo "可执行"else echo "不可执行"fiif [ - $TEST_FILE ]then echo "为普通"else echo "为特殊"fiif [ - $TEST_FILE ]then echo "是个目录"else echo "不是个目录"fiif [ -s $TEST_FILE ]then echo "不为空"else echo "为空"fiif [ - $TEST_FILE ]then echo "存在"else echo "不存在"fi
返回为:
检测的为:/etc/fstab 信息为:-rw-r--r--. 1 root root 500 Jan 17 14:23 /etc/fstab可读 可写 不可执行 为普通 不是个目录 不为空 存在
对于字符串判断与数字对比,其作用的对象不同需要格外注意,利用 test 命令对于字符串进行一些判断操作,假定变量 a 为 “linux”,变量 b 为 “shell”:
例如:
#!/bin/bashstr1="linux"str2="shell"echo "str1 为:${str1}"echo "str2 为:${str2}"if [ $str1 = $str2 ];then echo "$str1 = $str2 : str1 等于 str2"else echo "$str1 = $str2: str1 不等于 str2"fiif [ $str1 != $str2 ];then echo "$str1 != $str2 : str1 不等于 str2"else echo "$str1 != $str2: str1 等于 str2"fiif [ -z $str1 ];then echo "-z $str1 : 字符串长度为 0"else echo "-z $str1 : 字符串长度不为 0"fiif [ -n "$str1" ];then echo "-n $str1 : 字符串长度不为 0"else echo "-n $str1 : 字符串长度为 0"fiif [ ${str1} ];then echo "str1 : 字符串不为空"else echo "str1 : 字符串为空"fi
返回为:
str1 为:linux str2 为:shell linux = shell: str1 不等于 str2 linux != shell : str1 不等于 str2-z linux : 字符串长度不为 -n linux : 字符串长度不为 str1 : 字符串不为空
利用 test 命令可以进行逻辑判断,以下介绍 Shell 的逻辑运算符,假定变量 A 为 1,变量 b 为 2:
如果需要在 test 中执行多个判断,需要使用 [[]]
, test 是 Shell 内置关键字,不是命令,免除传递过程,因此更加建议使用 [[]]
。双中括号有以下注意事项:
不需要把变量名用双引号 ""
包围起来,即使变量是空值,也不会出错;
不需要、也不能对 >、< 进行转义,转义后会出错;
其正则表达式,组合逻辑可以不使用 test 的 - a,-o 而使用 && ||。
与逻辑运算符号对应的 &&
可以使用 -a
,||
可以使用 -o
来替换。
例如:
#!/bin/bashnum1=num2=echo "num1 为:${num1}"echo "num2 为:${num2}"if [[ $num1 -lt && $num2 -gt ]];then echo "返回 true"else echo "返回 false"fiif [[ $num1 -lt || $num2 -gt ]];then echo "返回 true"else echo "返回 false"fiif [ $num1 -lt ] && [ $num2 -gt ];then echo "返回 true"else echo "返回 false"fiif [ $num1 -lt ] || [ $num2 -gt ];then echo "返回 true"else echo "返回 false"fiif [ $num1 -lt - $num2 -gt ];then echo "返回 true"else echo "返回 false"fiif [ $num1 -lt -o $num2 -gt ];then echo "返回 true"else echo "返回 false"fi
返回:
num1 为: num2 为: 返回 false返回 true返回 false返回 true返回 false返回 true
4. 实例
编写脚本,可以传入任意多个数值,要求输入的数字大于等于 0,脚本执行最小的和最大的数字。
可以利用测试运算符来判断的目录是否正确,之后利用算术运算符配合数组对或目录进行。
#!/bin/bash# Description: count file scripts# Auth: kaliarch# Email: kaliarch@163.com# function: count file# Date: 2020-03-21 14:00# Version: 1.0# 判断输入至少参数[ $# -le 1 ] && echo "输入参数,至少输入数字" && exit 1for num in $*;do# 判断输入的都为数字且大于等于0[ "${num}" -ge ] >/dev/null[ $? -ne ] && echo "输入的${num}不符合参数规范" && exit done# 对目录下的进行MIN_NUM=$ MAX_NUM=$for num in $*;doif [ ${num} -gt ${MAX_NUM} ];thenMAX_NUM=${num} elif [ ${num} -lt ${MIN_NUM} ];thenMIN_NUM=${num}elseecho - ""fi done echo "输入的参数总共为:$#"echo "输入的数字为:$*"echo "其中最大的数字为:${MAX_NUM}"echo "其中最小的数字为:${MIN_NUM}"
当我们不输入任何参数,或输入单个参数的时候,输入参数,至少输入数字;
如果输入有包含字符串,则参数不规范;
当输入一些列数字的时候,可以得到正确的结果。
[root@master shell_test]# bash num_check.sh 输入参数,至少输入数字[root@master shell_test]# bash num_check.sh 1输入参数,至少输入数字[root@master shell_test]# bash num_check.sh 1 "hell"输入的hell不符合参数规范[root@master shell_test]# bash num_check.sh 12 13 32 435 0输入的参数总共为: 输入的数字为: 其中最大的数字为: 其中最小的数字为:
4. 注意事项
对于关系运算,test 命令比较的对象为数字,不是字符串,一定要牢记这个注意点;
对于字符串判断,test 命令作用的为字符串,如果想比较数字,可以利用双引号引起来通过字符串判断。
5. 小结
在本章节由于 test 命令与运算符配合使用,与之前运算符章节有重复,在此温故知新,通过示例来更加熟悉 test 命令及相关运算符,其是后期 Shell 脚本编写的流程框架,熟练掌握 test 命令的数值,,字符串与逻辑运算,使得我们编写 Shell 脚本更加得心应手。