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. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  2. Spring MVC的Controller接受请求方式以及编写请求处理方法

    Controller接受请求参数的常见方法: 1.通过Bean接受请求参数: 创建POJO实体类 创建pojo包,并在该包中创建实体类UserForm,代码: package pojo; public ...

  3. tinymce富文本编辑器整合到django

    第一步:定义表存图片路径 models.py class AdminIMG(models.Model):     filename = models.CharField(max_length=200, ...

  4. Java——Java中编码问题

    在开发过程中经常会遇到一会乱码问题,不是什么大问题,但是也挺烦人的,今天来将我们开发总结的经验记录下来,希望可以给大家一些帮助. 一些概念: 字符:人们使用的记号,抽象意义上的一个符号.比如:‘1’, ...

  5. Python3中正则的贪婪匹配模式

    什么是贪婪模式 正则在进行匹配时,从开始位置查找最远的结束位置,这种模式称之为贪婪模式. 在进行HTML标签类似内容获取时,贪婪模式会导致整个内容的返回,需要使用非贪婪模式. 固定的书写规则 : .* ...

  6. Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)

    文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...

  7. LeetCode--Array--Container With Most Water (Medium)

    11. Container With Most Water (Medium)# Given n non-negative integers a1, a2, ..., an , where each r ...

  8. 【Scala】isInstanceOf 与 classOf的对比,代码+注解简洁明了

    class Animal { } class Cat extends Animal { } object Cat { def main(args: Array[String]): Unit = { / ...

  9. 简单服务发现协议(SSDP)编程指导

    本文主要讲解如何使用ssdp进行编程,不涉及过多关于ssdp的理论知识 1. 前言   网上对ssdp理论介绍的一大把,缺乏从编程角度的一个指导,这里不会长篇大论ssdp理论,只是根据实际编码经验对用 ...

  10. ArrayList扩容机制实探

    ArrayList初始化 问题:执行以下代码后,这个list的列表大小(size)和容量(capacity)分别是多大? List<String> list = new ArrayList ...