一bash的条件测试

判断某需求是否满足,需要由测试机制来实现。专用的测试表达式需要由测试命令辅助完成测试过程。

评估布尔声明,以便用在条件性执行中。若真,则返回0;若假,则返回1。

测试命令:
• test EXPRESSION
• [ EXPRESSION ]
• [[ EXPRESSION ]]和上面两个不同的在于支持正则表达式
注意:EXPRESSION前后必须有空白字符

(一)使用test命令

[root@centos7 ~]# type test
test is a shell builtin
[root@centos7 ~]# type [
[ is a shell builtin 内部命令
[root@centos7 ~]# type [[
[[ is a shell keyword

比较两个字符串是否相同

[root@centos7 ~]# srtr1=abc
[root@centos7 ~]# srtr2=abc
[root@centos7 ~]# test str1=str2
[root@centos7 ~]# echo $?
0
[root@centos7 ~]# test str1 = str2
[root@centos7 ~]# echo $?
1
[root@centos7 ~]# srtr1=abc
[root@centos7 ~]# srtr2=def
[root@centos7 ~]# test str1 = str2
[root@centos7 ~]# echo $?
1
[root@centos7 ~]# test str1 != str2
[root@centos7 ~]# echo $?
0
[root@centos7 ~]# test str1 ! = str2
#注意!=一定要连在一起的
-bash: test: too many argumentsy

(二)使用中括号[ ]

系统里面的脚本也是使用中括号。

使用中括号会更好,因为test是字符串,容易和其他的字符串混在一起,而中括号一目了然。

注意使用中括号EXPRESSION前后必须有空白字符

[root@centos7 ~]# srtr1=abc
[root@centos7 ~]# srtr2=abc
[root@centos7 ~]# [test1 = test2]
-bash: [test1: command not found
#使用中括号EXPRESSION前后必须有空白字符
[root@centos7 ~]# [ test1 = test2 ] [root@centos7 ~]# echo $?
0

系统里面的脚本也是使用中括号

[root@centos73 shell_scripts]# cat  /etc/init.d/functions
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
# TEXTDOMAIN=initscripts # Make sure umask is sane
umask 022 # Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
[ -d /run/systemd/system ] ; then
case "$0" in
/etc/init.d/*|/etc/rc.d/init.d/*)
_use_systemctl=1
;;
esac
fi

在中括号里面,只要中括号里面有东西就是为真,没有就是为假

[root@centos7 ~]# [ ] && echo true ||  echo false
false
[root@centos7 ~]# [ 123 ] && echo true || echo false
true
[root@centos7 ~]# [ 1243] && echo true || echo false
-bash: [: missing `]'
false
[root@centos7 ~]# [1243] && echo true || echo false
-bash: [1243]: command not found
false
[root@centos7 ~]# [1243 ] && echo true || echo false
-bash: [1243: command not found
false

?如果中括号里面是引号,就要空格

[root@centos7 ~]# [ "" ] && echo true ||  echo false
false
[root@centos7 ~]# [ " " ] && echo true || echo false
true

如果变量是有值,但是是空字符串,那么直接写就假,要使用引号引起来

[root@centos7 ~]# a=36
[root@centos7 ~]# [ $a ] && echo true || echo false
true
[root@centos7 ~]# unset a
[root@centos7 ~]# a=" "
[root@centos7 ~]# [ $a ] && echo true || echo false
false
[root@centos7 ~]# [ "$a" ] && echo true || echo false
true

可以使用中括号判断某个变量是否赋值了

[root@centos7 ~]# [ "x"$a = "x" ] && echo true ||  echo false 
#这个x纯粹就是为了组合的,x和字符串组合,如果a没有定义,那么x就等于x。这里的x是一个字母代表,使用其他的字母也可以。
true
[root@centos7 ~]# a=245
[root@centos7 ~]# [ "x"$a = "x" ] && echo true || echo false
false

在6里面的vim /etc/rc.sysinit中使用的就是这种方法。

645 # Start up swapping.
646 update_boot_stage RCswap
647 action $"Enabling /etc/fstab swaps: " swapon -a -e
648 if [ "$AUTOSWAP" = "yes" ]; then
649 curswap=$(awk '/^\/dev/ { print $1 }' /proc/swaps | while read x; do get_numeric_dev dec $x ; echo -n " "; done)
650 swappartitions=$(blkid -t TYPE=swap -o device)
651 if [ x"$swappartitions" != x ]; then
652 for partition in $swappartitions ; do
653 [ ! -e $partition ] && continue
654 majmin=$(get_numeric_dev dec $partition)
655 echo $curswap | grep -qw "$majmin" || action $"Enabling local swap partitions: " swapon $partition

二bash的字符串测试

字符串测试操作符的作用包括:比较两个字符串是否相同、测试字符串的长度是否为零、字符串是否为NULL等。

(一)常用字符串测试操作符及其说明:

== 是否等于

> ascii码是否大于ascii码

< 是否小于

!= 是否不等于

=~ 左侧字符串是否能够被右侧的PATTERN所匹配,注意: 此表达式一般用于[[ ]]中;扩展的正则表达式

-z "STRING“ 字符串是否为空,空为真,不空为假。若字符串的长度为0,则为真,即测试表达式成立, z可以理解为zero的缩写"

-n "STRING“ 字符串是否不空,不空为真,空为假。若字符串的长度不为0,则为真,即测试表达式成立, n可以理解为no zero

注意:用于字符串比较时的用到的操作数都应该使用引号

"串1"="串2"   若字符串1等于字符串2,则为真,即测试表达式成立,可使用"==”代替

"串1"!="串2" 若字符串1不等于字符串2,则为真,即测试表达式成立,但不能用"!==”代替"!

(二)字符串测试操作符的注意事项:

对于字符串的测试,一定要将字符串加双引号之后再进行比较。

如[-n  "$var"] ,特别是使用中括号[ ]的场景。

比较符号(例如=和!-)的两端一定要有空格。
 
"!=”和“=”可用于比较两个字符串是否相同。

注意一般相等或者不等写上一个中括号就可以

[root@centos7 ~]# var=abc;[ "$var" != "abc" ]  &&  echo true ||  echo false
false
[root@centos7 ~]# var=abc;[ "$var" = "abc" ] && echo true || echo false
true

两个中括号

[root@centos7 ~]# var=abc;[[ "$var" = "abc" ]]  &&  echo true ||  echo false
true
[root@centos7 ~]# var=abc;[[ "$var" != "abc" ]] && echo true || echo false
false

两个中括号的优点就是支持正则表达式

=~ 左侧字符串是否能够被右侧的PATTERN所匹配
注意: 此表达式一般用于[[ ]]中;扩展的正则表达式

[root@centos7 ~]# var=abcd;[[ "$var" =~ cd ]]   &&  echo true || echo false
true
#只要变量包含了字符串,返回的值就是真 [root@centos7 ~]# var=abc;[[ "$var" =~ cd ]] && echo true || echo false
false
[root@centos7 ~]# var=abc;[[ "$var" =~ ab?  ]]   &&  echo true || echo false
true
[root@centos7 ~]# var=b;[[ "$var" =~ a? ]] && echo true || echo false
true
[root@centos7 ~]# var=b;[[ "$var" =~ b+ ]] && echo true || echo false
true
[root@centos7 ~]# var=a;[[ "$var" =~ . ]] && echo true || echo false
true
[root@centos7 ~]# var=a;[[ "$var" =~ .. ]] && echo true || echo false
false
[root@centos7 ~]# var=a;[[ "$var" =~ . ]] && echo true || echo false
#点表示的是包含任意1个字符
true [root@centos7 ~]# var=a;[[ "$var" =~ .. ]] && echo true || echo false
#点表示的是包含任意1个字符,而2个点表示的是包含任意2个字符,而变量的值只有1个字符,所以返回的是假。
false
[root@centos7 ~]# var=abc;[[ "$var" =~ .. ]] && echo true || echo false
#点表示的是包含任意1个字符,而2个点表示的是包含任意2个字符,而变量的值是3个字符,所以返回的是真
true
[root@centos7 ~]# var=abc;[[ "$var" =~ ^..$ ]] && echo true || echo false
#已经锚定了任意2个字符,而变量的值是3个字符,所以返回的是假
false

判断文件名的后缀是否是sh的

正则表达式很绕,要多看

[root@centos7 ~]# filename=f1.sh;  [[ "$filename"  =~  .*\.sh$ ]]   && echo true  ||  echo  false
true
[root@centos7 ~]# filename=f1.shshsh; [[ "$filename" =~ .*\.sh$ ]] && echo true || echo false
false
[root@centos7 ~]# filename=f1.shshshs; [[ "$filename" =~ .*\.sh ]] && echo true || echo false
true
[root@centos7 ~]# filename=f1.shshshs; [[ "$filename" =~ .*\.sh$ ]] && echo true || echo false
false
[root@centos7 ~]# filename=f1.shshshs;  [[ "$filename"  =~  \.sh$ ]]   && echo true  ||  echo  false
false
[root@centos7 ~]# filename=f1.shshsh; [[ "$filename" =~ \.sh$ ]] && echo true || echo false
false
[root@centos7 ~]# filename=f1.sh; [[ "$filename" =~ \.sh$ ]] && echo true || echo false
true
[root@centos7 ~]# filename=f1.sh; [[ "$filename" =~ \.sh$ ]] && echo true || echo false
#不管后缀前面的是什么,这种写法也可以
true
[root@centos7 ~]# filename=f1.sh; [[ "$filename" =~ "\.sh$" ]] && echo true || echo false
#在正则表达式是不能加引号的
false
[root@centos7 ~]# filename=f1.sh; [ "$filename" =~ "\.sh$" ] && echo true || echo false
#在正则表达式是不能加引号的
-bash: [: =~: binary operator expected
false
[root@centos7 ~]# filename=f1.sh; [ "$filename" =~ "\.sh$" ] && echo true || echo false
#在正则表达式是不能加引号的,单中括号是不支持=~和正则表达式的,
-bash: [: =~: binary operator expected
false
[root@centos7 ~]# var="abc*"; [[ "$var" == "abc*" ]] && echo true || echo false
true
[root@centos7 ~]# var="abc*"; [[ "$var" == abc* ]] && echo true || echo false
true
[root@centos7 ~]# #==不支持正则表达式,而是精确匹配,双引号起到了引用的作用,不是引号本身,加不加引号效果一样。
[root@centos7 ~]# var="abcdefgh"; [[ "$var" == abc* ]] && echo true || echo false
true
[root@centos7 ~]# #这里的*表示的通配符,表示正则表达式。注意如果是精确匹配==就不要用双中括号了,这样更不容易搞混。

判断IP地址是否是正确的,匹配IP地址

注意要锚定行首行尾

[root@centos7 ~]# ip="192.168.36.1";[[  $ip =~    \<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>    ]]    &&   echo  $ip  is  correct   ||   echo  $ip  is incorrect
192.168.36.1 is incorrect
[root@centos7 ~]# ip="192.168.36.1";[[ $ip =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] && echo $ip is correct || echo $ip is incorrect
192.168.36.1 is correct

变量值加不加引号没啥区别

[root@centos7 ~]# ip=192.168.36.1;[[  $ip =~    ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$    ]]   &&   echo  $ip  is  correct   ||   echo  $ip  is incorrect
192.168.36.1 is correct

判断一个变量是否是纯数字

涉及到正则表达式。*表示包括0次,+表示至少一次

[root@centos7 ~]# unset var;  [[ "$var" =~  ^[0-9]*$ ]]   &&  echo  true  ||  echo false
true
[root@centos7 ~]# unset var; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
false
[root@centos7 ~]# var=123456; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
true
[root@centos7 ~]# var=a23456; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
false
[root@centos7 ~]# var=023456; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
true
[root@centos7 ~]# var=023450; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
true
[root@centos7 ~]# var=0; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
true
[root@centos7 ~]# var=0a; [[ "$var" =~ ^[0-9]+$ ]] && echo true || echo false
false

判断是正整数,不括号0

[root@centos7 ~]#  var=0;  [[ "$var" =~  ^[1-9][0-9]*$ ]]   &&  echo  true  ||  echo false  
#*表示前面的字符重复0次或者任意次
false
[root@centos7 ~]# var=1; [[ "$var" =~ ^[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true
[root@centos7 ~]# var=123455670; [[ "$var" =~ ^[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true
[root@centos7 ~]# var=10000002345567; [[ "$var" =~ ^[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true
[root@centos7 ~]# var=010000002345567; [[ "$var" =~ ^[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
false
[root@centos7 ~]# var=010000002345567; [[ "$var" =~ ^0*[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true
[root@centos7 ~]# #0*表示的0可以没有,也可以出现好几个。[1-9]表示中间必须是1到9,最后的数是重复0到9任意的数0次及以上。
[root@centos7 ~]# var=000000010000002345567; [[ "$var" =~ ^0*[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true
[root@centos7 ~]# var=0000007; [[ "$var" =~ ^0*[1-9][0-9]*$ ]] && echo true || echo false
#*表示前面的字符重复0次或者任意次
true

示例——匹配手机号

注意手机号第二位现在没有1,2,{9}表示重复前面的数字9次

[root@centos7 ~]# mobile=12345670000 ;[[ "$mobile" =~ ^1[3456789][0-9]{9}$  ]] && echo true || echo false
false
[root@centos7 ~]# mobile=13345670000 ;[[ "$mobile" =~ ^1[3456789][0-9]{9}$ ]] && echo true || echo false
true

shell脚本编程测试类型上的更多相关文章

  1. shell脚本编程测试类型下

    一bash的数值测试 -v VAR变量VAR是否设置 数值测试:-gt 是否大于greater-ge 是否大于等于-eq 是否等于-ne 是否不等于  not equal-lt 是否小于-le 是否小 ...

  2. shell脚本编程及bash特性

    bash特性及bash脚本编程初步 终端,附着在终端的接口程序; GUI: KDE,GNome,Xfce CLI: /etc/shells bash的特性: 命令行展开: ~,{} 命令别名: ali ...

  3. SHELL脚本编程的条件测试

    SHELL脚本编程的条件测试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.条件测试概述 判断某需求是否满足,需要由测试机制来实现 专用的测试表达式需要由测试命令辅助完成测试过 ...

  4. 亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]

    亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]http://automationqa.com/forum.php?mod=viewthread&tid=2453& ...

  5. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  6. 【Linux】Shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  7. Shell脚本编程总结及速查手册

    Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...

  8. 08 bash特性--shell脚本编程入门

    shell脚本编程入门 编程语言介绍 编程语言分为:机械语言.汇编语言和高级语言: 计算机能识别的语言为机械语言,而人类能学习的并且能够方便掌握的为高级语言,所以,我们所编写的程序就要通过编译来转换成 ...

  9. SHELL脚本编程的常识和VI常用技巧

    来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...

随机推荐

  1. POJ 3525 Most Distant Point from the Sea (半平面交)

    Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...

  2. LG1440 求 m 区间内的最小值

    题目描述 一个含有 \(n\) 项的数列 (\(n≤ 2000000\)),求出每一项前的 \(m\) 个数到它这个区间内的最小值.若前面的数不足 \(m\) 项则从第 \(1\) 个数开始,若前面没 ...

  3. 六. jenkins部署springboot项目(3)--windows环境--远程windows server服务器

    前提:jenkins服务器和windows server服务器不在一台机器上 对于jenkins服务器上编译好的jar或war包如何推送到windows server服务器上. 参照网上的,在wind ...

  4. (62)C# 动态绑定

    动态绑定不能绕过成员可访问性的规则

  5. 测开之路二十九:Flask基础之jinja2模板

    中文文档:http://docs.jinkan.org/docs/jinja2/ 与静态资源一样,Flask默认的模板目录名为templates,如果有需要的话和static一样,要在初始化的时候声明 ...

  6. cvAddWeighted 进行图片融合

     http://blog.csdn.net/longzaitianya1989/article/details/8103822 cvAddWeighted 进行图片融合 2012-10-23 18:2 ...

  7. Centos7 安装配置Apache+Mysql5.7+PHP7.0+phpmyadmin

    Centos7 下安装配置Apache+Mysql5.7+PHP7.0+phpmyadmin 搭建LAMP =========================================Apach ...

  8. USACO 5.5 章节

    Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...

  9. python 装饰器 第四步:基本装饰器的实现

    #第四步:基本装饰器的实现 #用于扩展基本函数的函数 def kuozhan(func): #内部函数(扩展之后的eat函数) def neweat(): #以下三步就是扩展之后的功能,于是我们把这三 ...

  10. this关键字与super关键字区别

        this super 1 访问属性 访问本类中属性,如果本类中没有此属性,就从父类继承过来的属性中查找 (遵循就近原则) 访问父类中的属性 2 调用方法 访问本类中方法 直接访问父类中方法 3 ...