shell判断语句
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判断语句的更多相关文章
- 【转】 shell 判断语句
转自:http://see.sl088.com/wiki/Shell_%E4%B8%AD%E6%8B%AC%E5%8F%B7 test 和 [] test -z string 判定字串是否為 0 ?若 ...
- shell 判断语句
1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真str1 != str2 当串str1和str2不等时为真-n str1 当串的长度大于0时为真(串非空)-z str1 当串的长 ...
- 6.shell判断语句
[ condition ](注意condition前后要有空格),可以使用$?验证(0为true,>1为false) 两个整数的比较:=:字符串比较-lt:小于-gt:大于-le:小于等于-ge ...
- linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...
- shell判断文件,目录是否存在或者具有权限的代码
核心代码 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的 ...
- Unix shell判断和比较
1. shell 的$! ,$?, $$,$@ $n $1 the first parameter,$2 the second... $# The number of c ...
- Linux Shell系列教程之(九)Shell判断 if else 用法
本文是Linux Shell系列教程的第(九)篇,更多shell教程请看:Linux Shell系列教程 判断语句是每个语言都必不可少的关键语法,Shell命令当然也不例外.今天就给大家介绍下Shel ...
- shell判断文件,目录是否存在或者具有权限 (转载)
转自:http://cqfish.blog.51cto.com/622299/187188 文章来源:http://hi.baidu.com/haigang/blog/item/e5f582262d6 ...
- shell判断和比较
http://blog.chinaunix.net/uid-7553302-id-183648.html 1 shell 的$! ,$?, $$,$@ $n $1 the first ...
随机推荐
- C++条件分支结构
一.对于近期学习知识点的摘要: 1. 从第一个.cpp文件谈起, #include<iostream> //头文件 using namespace std; //使用命名空间,namesp ...
- ACM成长之路(干货) 我爱ACM,与君共勉
前几天在网上看到,转过来时刻督促一下自己. ACM队不是为了一场比赛而存在的,为的是队员的整体提高. 大学期间,ACM队队员必须要学好的课程有: l C/C++两种语言 l 高等数学 l 线性代数 l ...
- lintcode 826电脑维修
826,一个n * m矩阵代表一个电脑的阵列,给你一个list< Point >代表坏掉的电脑坐标.现在我们从(0,0)出发修电脑,要求: 1.必须修完当前行所有坏掉的电脑才能走向下一 ...
- Python基础02 变量
Python中的变量有两个特点: 1. 无需声明 a = 1 2. 不与类型绑定 a = 1 a = 'hello world' 变量名只是内存中具体对象的一个引用(reference). 对于 a ...
- Programming Languages_04 Deferred Substitution
Deferred Substitution 在执行出现with时,利用"substitution",每次with的出现,它都绕着整个body置换.这一方式是由F1WAE到env再到 ...
- springboot之异常处理
我在使用springboot的时候,运行主类结果报错 : 异常错误:java.sql.SQLException: The server time zone value '?й???????' is u ...
- P3983 赛斯石(双背包)
这题不算难的,但是脑子真的特别乱.....传送门 \(Ⅰ.物品可以拆开来但船不能拆开来,所以1-10载重船的最大收益完全可以用背包求出来.\) \(Ⅱ.最后一定是选一些船走,而船的收益已经固定.所以用 ...
- 能量项链(区间DP入门)
题面:能量项链https://www.luogu.com.cn/problem/P1063 乍一看和石子合并差不多,可是多了头值和尾值,看起来十分麻烦 我们画一张图,紫色表示头值,蓝色表示尾值.规定西 ...
- fork...join的用法
如果希望在仿真的某一时刻同时启动多个任务,可以使用fork....join语句.例如,在仿真开始的 100 ns 后,希望同时启动发送和接收任务,而不是发送完毕后再进行接收,如下所示: initial ...
- 1.1UML图分类
用例图 表现方式 是谁用软件 软件的功能 类图 描述类内部关系和类之间关系, 关系的强弱顺序泛化=实现>组合>聚合>关联>依赖 泛化:继承关系,指定了子类如何继承父类所有特征和 ...