shell_条件判断条件测试参数以及test用法

发布时间 2023-10-10 10:47:03作者: WeChat2834
条件判断

补充个read命令

shell变量除了直接赋值,或者脚本传参,还有就是read命令读取。

read 也是内置命令

#  -p  设置提示信息
#  -t	等待用户输入超市,timeout
#   read  -p "请输入:"  vars
[root@localhost ~]# read -t 10  -p "只有10秒的时间录入姓名和年龄:" uname uage
只有10秒的时间录入姓名和年龄:mrxu 38
[root@localhost ~]# echo $uname $uage
mrxu 38
[root@localhost ~]# 

条件测试

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

shell对于真假判断的逻辑,提供了&& 与运算,并且的两端是递进的关系;||

​ A条件 && B条件, 当A条件城里,并且执行B条件

​ A条件 || B条件, 当A条件不成立,才会执行B条件

常用语法

条件测试语法 说明
语法1:test <测试表达式> 这是利用test命令进行条件测试表达式的方法。test命令和"<测试表达式>"之间至少有一个空格。
语法2:[ <测试表达式> ] 这是通过[]单中括号进行条件测试表达式的方法,和test命令的用法相同,这是推荐的方法,[]的边界和内容之间至少有一个空格
语法3:[[ <测试表达式>]] 这是通过[[]]双中括号进行条件测试表达式的方法,是比test和[]更新的语法格式。 [[]]的边界和内容之间至少有一个空格
语法4:((<测试表达式>)) 这是通过(())双小括号进行条件测试表达式的方法,一般用于if语句理。(())双小括号两端不需要有空格
test条件测试

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

  • 参数
#test命令的参数
#1.关于某个文件名的类型侦测(存在与否),如test -e filename
-e 判断文件是否存在,存在为真,否则为假   (常用)
-f 该文件名是否为文件(file)            (常用)
-d 该文件名是否为目录(diretory)        (常用)
-b 该文件名是否为一个block device 装置
-c 该文件名是否为一个character device 装置
-s 该文件名是否为一个Socket文件
-P 该文件名是否为一个FIFO(pipe)文件
-L 该文件名是否为一个连接档
#2. 关于文件的权限侦测,如test -r filename
-r 侦测该文件名是否具有可读属性
-w 侦测该文件名是否具有可写属性
-x 侦测该文件名是否具有可执行属性
-u 侦测该文件是否具有SUID属性
-g 侦测该文件是否具有SGID的属性
-k 侦测该文件名是否具有Sticky bit属性
-s 侦测该文件名是否为非空白文件
#3.两个文件之间的属性,如test file1 -nt file2
-nt (newer than ) 判断 file 是否比file2 新
-ot (older than ) 判断file1 是否比fiel2旧
-ef 判断file1与file2是否为同一文件,可用在判断hard link的判定上,注意意义在判定,两个文件是否均指向同一个inode 
#4.关于两个整数间的判定 test n1 -eq n2
#针对变量数值的大小比较判定
-eq 两数值相等 (equal)
-ne 两数值不等 (not equal)
-gt n1 大于 n2 (greater than)
-lt n1 小于 n2 (less than)
-ge n1 大与等于n2 (greater than or equal)
-le n1 小于等于n2(less than or equal)
#5. 判定字符串的数据
test -z string 判定字符串是否为0 ? 若string为空字符串,则为true   (常用)
test -n string 判定字符串是否非 0 ? 若string为空字符串,则为false 注意-n 亦可省略
test str1 = str2  判定str1是否等于str2,若相等,则回传true
test str1 != str2 判定str2是否不等于str2,若相等,则回传false
#6. 多重条件判定,例如:test -r filename -a -x filename
-a  (and)两种状态同时成立! 例如 test -r file -a -x file  当file 同时具有r 与x权限时,才回传true
-o  (or)两状况任何一个成立! 例如test -r file -o -x file   当feile 同时具有r或x权限时,旧可回传true
!    反相状态,如test ! -x file   ,当file 不具有x时,回传true.

[root@localhost tmp]# ll
总用量 80
-rwxr-xr-x. 1 root root    78 9月  15 16:29 test01.sh
-rw-r--r--. 1 root root   114 9月  21 15:51 testbc.sh
-rw-r--r--. 1 root root   130 9月  21 15:02 test_expr.sh
-rw-r--r--. 1 root root   151 9月  21 11:21 test_houzui2.sh
-rw-r--r--. 1 root root   153 9月  21 10:38 test_houzui.sh
[root@localhost tmp]# test -e  test01.sh  #结果为真$?返回0
[root@localhost tmp]# echo $?
0
[root@localhost tmp]# test -e  test02.sh  #结果为假$?返回非0
[root@localhost tmp]# echo $?
1
[root@localhost tmp]# 
  • test命令实践,

    test 命令

    test 测试参数 要测试的对象 对结果进行判断执行的逻辑动作

###test  -e  参数
[root@localhost tmp]# cat demotest.sh
#!/bin/bash

echo "脚本执行,创建文件夹"

 test -e hello && echo "文件已存在" || mkdir hello

echo "以正确执行完毕"
[root@localhost tmp]# sh demotest.sh 
脚本执行,创建文件夹
以正确执行完毕
[root@localhost tmp]# sh demotest.sh 
脚本执行,创建文件夹
文件已存在
以正确执行完毕
#####test  -f  参数 举例
 drwxr-xr-x. 2 root root     6 9月  25 14:23 hello
-rwxr--r--. 1 root root   111 8月  30 15:52 myshell.sh
-rw-r--r--. 1 root root   400 9月   1 16:39 MyVar.sh
-rw-------. 1 root root 31552 9月   5 15:38 nohup.out
[root@localhost tmp]# test -f test01.sh && echo "ok" || echo "no"
ok
[root@localhost tmp]# test -f hello  && echo "ok" || echo "no"
no
[root@localhost tmp]# 
#####test  -z  参数 举例
[root@localhost ~]# test  -z  "" && echo ok ||echo no 
ok
[root@localhost ~]# test  -z  " " && echo ok ||echo no  ###空格也算是有字符的
no
[root@localhost ~]# expr length ""
0
[root@localhost ~]# expr length " "           ###空格也是有长度的
1
[root@localhost ~]#