1.test命令  也可以用[  ]来表示

返回值为0时为true,返回值为1时为false。

例1:str1=aaa,str2=bbb

1)判断字符串是否为空(省略了-n选项,-n选项是不为空,-z选项为空)

[root@xiaoxiao ~]# str1=aaa
[root@xiaoxiao ~]# str2=bbb
[root@xiaoxiao ~]# [ $str1 ]
[root@xiaoxiao ~]# echo $? [root@xiaoxiao ~]# [ -z $str1 ]
[root@xiaoxiao ~]# echo $? [root@xiaoxiao ~]# [ -n $str1 ]
[root@xiaoxiao ~]# echo $?

2)判断两个字符串是否相等

[root@xiaoxiao ~]# [ $str1 = $str2 ]
[root@xiaoxiao ~]# echo $?

3)判断两个数字时候相等

[root@xiaoxiao bin]# str1=
[root@xiaoxiao bin]# str2=
[root@xiaoxiao bin]# [ $str1 -eq $str2 ] && echo equal || echo noequal
noequal
[root@xiaoxiao bin]# str1=
[root@xiaoxiao bin]# [ $str1 -eq $str2 ] && echo equal || echo noequal
equal

2.逻辑运算符

# help let

&与

|或

!非

&&逻辑与 (cmd1 && cmd2,当cmd为ture时执行cmd2,为false时不继续执行cmd2)

||逻辑或(cmd1 && cmd2,当cmd1为fasle时执行cmd2,为true时不继续执行cmd2)

例:str1=aaa;str2=bbb

[root@xiaoxiao ~]# echo $str1 $str2
aaa bbb
[root@xiaoxiao ~]# [ $str1 = $str2 ] && echo truestrs || echo falsestrs
falsestrs
[root@xiaoxiao bin]# str1=aaa;str2=aaa
[root@xiaoxiao bin]# [ $str1 = $str2 ] && echo truestrs || echo falsestrs
truestrs

判断str1与str2两个字符串是否相等,cmd1 && cmd2 || cmd3 如果cmd1为真则执行cmd2,如果cmd1 && cmd2 命令cmd1为假则不行cmd2;此时将cmd1 && cmd2 的运算返回值 || cmd3逻辑或时,cmd1 && cmd2 的返回值是false,则执行cmd3。

^异或(可以实现两个值得互换,在let运算中)

[root@xiaoxiao bin]# str1=;str2=
[root@xiaoxiao bin]# str1=$[str1^str2];str2=$[str1^str2];str1=$[str1^str2]
[root@xiaoxiao bin]# echo $str1 $str2

判断是否是数字

[[ "$n" =~ ^[[:digit:]]+$  ]] && echo digit || echo "no digit";[[ "$n" =~ ^[0-9]+$  ]] && echo digit || echo "no digit"

"[]"中括号中的变量最好用引号,避免造成语法的错误

判断后缀

# .表示一个字符 .*表示任意字符 ..*表示至少一个字符

[[ $filename =~ ..*\.sh$ ]] && echo sh ||echo "not sh"

shell判断语句的更多相关文章

  1. 【转】 shell 判断语句

    转自:http://see.sl088.com/wiki/Shell_%E4%B8%AD%E6%8B%AC%E5%8F%B7 test 和 [] test -z string 判定字串是否為 0 ?若 ...

  2. shell 判断语句

    1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真str1 != str2 当串str1和str2不等时为真-n str1 当串的长度大于0时为真(串非空)-z str1 当串的长 ...

  3. 6.shell判断语句

    [ condition ](注意condition前后要有空格),可以使用$?验证(0为true,>1为false) 两个整数的比较:=:字符串比较-lt:小于-gt:大于-le:小于等于-ge ...

  4. linux shell中的条件判断语句

    http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...

  5. shell判断文件,目录是否存在或者具有权限的代码

    核心代码 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的 ...

  6. Unix shell判断和比较

    1.  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of c ...

  7. Linux Shell系列教程之(九)Shell判断 if else 用法

    本文是Linux Shell系列教程的第(九)篇,更多shell教程请看:Linux Shell系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.今天就给大家介绍下Shel ...

  8. shell判断文件,目录是否存在或者具有权限 (转载)

    转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...

  9. shell判断和比较

    http://blog.chinaunix.net/uid-7553302-id-183648.html 1  shell 的$! ,$?, $$,$@ $n        $1 the first ...

随机推荐

  1. POJ 1176 Party Lamps&& USACO 2.2 派对灯(搜索)

    题目地址 http://poj.org/problem?id=1176 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都 ...

  2. 动态SQL各个标签作用以及注意事项详解

    创建com.mybatis包,包含:UserMapper.xml和mybatis-config.xml UserMapper.xml代码: <?xml version="1.0&quo ...

  3. JMeter Nmon Tool V2.0 插件

    很早之前宝路已将nmon监控功能集成到了JMeter中,自己在使用旧版本时,也有诸多不满意的地方.趁着五一假期(基本都是晚上,白天要陪孩子),对插件底层代码进行了重构,自己还要反复测试调整,最晚的一次 ...

  4. Jmeter-接口测试参数化后循环断言不同内容的方法

    前言 各位小伙伴在做接口自动化有没遇到过这样的问题,CSV文件参数化测试数据后,只能通过人工的的方法去查看结果,不懂写代码去循环断言返回的结果.今天我们来学习一下,不用写代码,就用响应断言,怎么实现循 ...

  5. python学习之组成字符串的两种方式

    第一种就是加法,比如 a ='张三' b = '李四' 那么print c =a+b 例如之前提到的 或者数值转换成字符串的 num  = 100 str(num) 其他参照表格中的转换即可 2.组成 ...

  6. String、StringBuffer和StringBuilder比较

    从执行速度效率来说: String为字符串常量,一旦创建就不能修改,第一次创建一个String对象,实例化后有初始值:当第二次创建String对象后,再次赋值,表面上看是改变了第一个String对象的 ...

  7. Spring Batch 读 10 万条记录,写到 MongoDB

    实践内容 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB . 具体实现 1.新建 Spring Boot 应用,依赖如下: <!-- Web 应用 --> & ...

  8. MOS管、PCB、H桥、步进电机驱动电路、51单片机的IO口驱动能力、灌电流、拉电流、上拉电阻的选择

    嵌入式开发PCB设计几点体会(转载):http://bbs.ednchina.com/BLOG_ARTICLE_3021801.HTMCollector-Emitter Saturation Volt ...

  9. 【5min+】美化API,包装AspNetCore的返回结果

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  10. 缓冲 buffer 和缓存 cache 的区别

    缓存(cache)是在读取硬盘中的数据时,把最常用的数据保存在内存的缓存区中,再次读取该数据时,就不去硬盘中读取了,而在缓存中读取. 缓冲(buffer)是在向硬盘写入数据时,先把数据放入缓冲区,然后 ...