1.逻辑运算符:与&&     或||    非!

 &&:双目操作符:与运算中:如果第一个数为假,结果一定为假   ==> 短路操作符

||:双目操作符:或运算中:如果第一个数为真,结果一定为真    ==> 短路操作符

!:单目操作符:  对数取反.

例子:

 [root@lbg test]# echo 2 && echo 3
2
3
[root@lbg test]# echo 2 ||  echo 3 
2
[root@lbg test]# 

2.测试表达式

 [ 表达式 ]  ---比较数字大小 ,表达式与中括号两端必须有空格
 [[ 表达式 ]]  --比较字符大小,表达式与中括号两端必须有空格。
 
3.算术比较运算符:
  num1 -eq num2         //测试num1是否等于num2  (eq:equal)

-ne                //测试num1是否不等于num2

-gt             //大于 great than

-lt             //小于 less than

-ge             //大于等于 great  equal

-le             //小于等于 less equal

例子:

[root@lbg test]# [ 1 -lt 2 ]  && echo yes || echo no
yes
[root@lbg test]# [ 1 -gt 2 ]  && echo yes || echo no
no
4.字符串测试
 'string1' == 'string2'                    //做等值比较

!=,<>              //不等值比较

-n "$a"           //跟变量名,a变量的值的长度非0为真

-z "$a"         //测试是否为空的,空为真,非空为假,即值得长度为0为真

例子:

[root@lbg test]# [[ a == b ]] && echo yes || echo no
no
[root@lbg test]# [[ a != b ]] && echo yes || echo no
yes
[root@lbg test]# echo $a
123
[root@lbg test]# [[ -n $a ]] && echo yes || echo no
yes
[root@lbg test]# declare b      ---$b为空
[root@lbg test]# [[ -n $b ]] && echo yes || echo no
no
[root@lbg test]# [[ -z $b ]] && echo yes || echo no
yes
5.文件测试
 -e        /path/to/somewhere    是否存在,存在为真    --exist

-f   /path/to/somewhere  是否是文件,是则为真      --file

-d          //测试是否是目录,是为真          --directory

-l          //测试是否是链接,是为真         --link

-r          //是否可读               --read

-w          //是否可写              --write

-x          //是否可执行             --execute

说明:文件测试用单个[]或者[[]]都行。

例子:

[root@lbg test]# ll                    ----只有普通文件a
-rw-r--r-- 1 root root 37 Oct  5 19:13 a
[root@lbg test]# [[ -e /test/a ]] && echo yes || echo no
yes
[root@lbg test]# [[ -e /test/b ]] && echo yes || echo no
no
[root@lbg test]# [[ -d /test/a ]] && echo yes || echo no
no
[root@lbg test]# [ -x /test/a ] && echo yes || echo no  
no

6.组合条件测试

 !                     //取反,写在表达式之前,且用空格隔开

-a      //与条件,写在[]里面,表示and

-o      //或条件,写在[]里面,表示or

例子:

[root@lbg test]# [  d == f  ] && echo yes || echo no
no 
[root@lbg test]# [ ! d == f  ] && echo yes || echo no 
yes
[root@lbg test]# [ ! d == f  ] && [ a == b ]&& echo yes || echo no
no
[root@lbg test]# [ ! d == f -a c == b ]  && echo yes || echo no
no
[root@lbg test]# [ ! d == f -o c == b ]  && echo yes || echo no
yes

shell中数字、字符串、文件比较测试的更多相关文章

  1. Linux Shell 04 数字/字符串/文件测试

    一. 数字测试 格式:n1  -op  n2 测试操作op: eq/ne/le/ge/lt/gt    -->    等于/不等于/小于等于/大于等于/小于/大于 1. 数字比较可以使用特殊的( ...

  2. (转)Shell中获取字符串长度的七种方法

    Shell中获取字符串长度的七种方法 原文:http://blog.csdn.net/jerry_1126/article/details/51835119 求字符串操作在shell脚本中很常用,下面 ...

  3. shell中取字符串子串的几种方式 截取substr

    shell中取字符串子串的几种方式 echo "123456789" | awk '{print substr($0,5,2)}' 截取 1)awk中函数substrsubstr( ...

  4. shell中截取字符串的方法总结

    shell中截取字符串的方法有很多种, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  5. shell中的字符串操作和数学运算

    字符串操作   变量赋值: 说明:变量值可以用单引号.双引号.或者不加任何引号来赋值给变量 变量名="变量值" 变量名='变量值' 变量名=变量值 例如:str="hel ...

  6. 在Shell中使用函数文件

    需要编写一个较庞大的脚本时,可能会涉及许多函数.变量.这是通常建议将众多的函数.变量放入一个单独的脚本内.这样做的好处很明显,不用担心某个函数.变量是否已经被定义和使用,也不用频繁地定义.清除函数和变 ...

  7. LinuxShell脚本编程基础5--数值,字符串,文件状态测试,((..))和[[..]]的使用

    1.数值比较 ! /bin/bash echo "enter a score:" read num1 ] then echo "Very Good" elif ...

  8. 【前端_js】js中数字字符串之间的比较

    js中字符串间的比较是按照位次优先,比较各字符的ASCII大小,包括数字字符串之间的比较. 1.console.log("1"<"3");//true 2 ...

  9. Shell中判断字符串是否为数字的6种方法分享

    #!/bin/bash ## 方法1 a=1234;echo "$a"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] &&a ...

随机推荐

  1. 微信小程序 - 重置checkbox样式

    /* 未选中的 背景样式 */ checkbox .wx-checkbox-input { border-radius: 50%;/* 圆角 */ width: 30rpx; /* 背景的宽 */ h ...

  2. spring boot:redis+lua实现生产环境中可用的秒杀功能(spring boot 2.2.0)

    一,秒杀需要具备的功能: 秒杀通常是电商中用到的吸引流量的促销活动方式 搭建秒杀系统,需要具备以下几点: 1,限制每个用户购买的商品数量,(秒杀价格为吸引流量一般会订的很低,不能让一个用户全部抢购到手 ...

  3. nginx安全:用limit_req_zone/limit_req限制连接速率(流量控制/限流)

    一,limit_req的用途: 1,官方文档地址: http://nginx.org/en/docs/http/ngx_http_limit_req_module.html 2,用途: 限制用户在给定 ...

  4. python matplotlib配置

    import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib.font_manager import FontPro ...

  5. 往with as中写入数据的方法

    方法1:直接写入,使用union all,简单直观,但程序运行效率低,几百条就很慢了 with dw_wms_outbound_info_v100 as( select '10700001' as o ...

  6. HTML <big> 标签

    HTML <big> 标签 什么是<big> 标签? <big> 标签呈现大号字体效果. 使用 <big> 标签可以很容易地放大字体.这简直不能再简单了 ...

  7. Django折腾日记(django2.0)

    新建项目 django-admin startproject mysite 运行 python manage.py runserver 创建一个应用 python manage.py startapp ...

  8. buuctf-pwn:jarvisoj_level6_x64

    jarvisoj_level6_x64 只能申请unsorted bin大小下的unlink IDA看一下,可以发现edit里面有任意堆溢出的情况(realloc造成堆溢出) 然后free里面有UAF ...

  9. 需要加强NB-IoT网络优化和终端监管

    NB-IoT解决方案在行业规模商用应用中得到了持续的验证,承受住考验,现在芯片和网络问题和障碍已经得到解决. 从统计结果来看,目前的绝大多数问题都集中在终端侧,因为行业的定制化需求以及合伙伙伴的能力差 ...

  10. [NOIP 2016D2T2/Luogu P1600] 天天爱跑步 (LCA+差分)

    待填坑 Code //Luogu P1600 天天爱跑步 //Apr,4th,2018 //树上差分+LCA #include<iostream> #include<cstdio&g ...