博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell编程之条件测试与if语句用法
阅读量:2047 次
发布时间:2019-04-28

本文共 3559 字,大约阅读时间需要 11 分钟。

条件测试

teat命令

- 格式1: test 条件表达式- 格式2: [ 条件表达式 ]

判断文件类

在这里插入图片描述

举例说明:

111
test -e file					只要文件存在条件为真[ -d /shell01/dir1 ]		 	判断目录是否存在,存在条件为真[ ! -d /shell01/dir1 ]		判断目录是否存在,不存在条件为真[[ -f /shell01/1.sh ]]		判断文件是否存在,并且是一个普通的文件

判断文件权限

在这里插入图片描述

判断文件新旧

在这里插入图片描述

判断整数

在这里插入图片描述

判断字符串

在这里插入图片描述

多重条件判断

在这里插入图片描述

[ 33 <> 55 -a 33 -gt $[300/10] ]; echo $?[ 33 -le 44 ] && [ 33 -gt $[999/100] ] ; echo $?

特别说明:

&& 前面的表达式为真,才会执行后面的代码

|| 前面的表达式为假,才会执行后面的代码

; 只用于分割命令或表达式

举例说明

  • 数值比较
[root@server ~]# [ $(id -u) -eq 0 ] && echo "the user is admin"[root@server ~]$ [ $(id -u) -ne 0 ] && echo "the user is not admin"[root@server ~]$ [ $(id -u) -eq 0 ] && echo "the user is admin" || echo "the user is not admin"[root@server ~]# uid=`id -u`[root@server ~]# test $uid -eq 0 && echo this is adminthis is admin[root@server ~]# [ $(id -u) -ne 0 ]  || echo this is adminthis is admin[root@server ~]# [ $(id -u) -eq 0 ]  && echo this is admin || echo this is not adminthis is admin[root@server ~]# su - stu1[stu1@server ~]$ [ $(id -u) -eq 0 ]  && echo this is admin || echo this is not adminthis is not admin [ $(id -u) -eq 0 ] && echo 'root用户' || echo '普通用户'
  • 类c风格的数值比较
注意:在(( ))中,=表示赋值;==表示判断[root@server ~]# ((1==2));echo $?[root@server ~]# ((1<2));echo $?[root@server ~]# ((2>=1));echo $?[root@server ~]# ((2!=1));echo $?[root@server ~]# ((`id -u`==0));echo $? [root@server ~]# ((a=123));echo $a[root@server ~]# unset a[root@server ~]# ((a==123));echo $?
  • 字符串比较
注意:双引号引起来,看作一个整体;= 和 == 在 [ 字符串 ] 比较中都表示判断[root@server ~]# a='hello world';b=world[root@server ~]# [ $a = $b ];echo $?[root@server ~]# [ "$a" = "$b" ];echo $?[root@server ~]# [ "$a" != "$b" ];echo $?[root@server ~]# [ "$a" !== "$b" ];echo $?        错误[root@server ~]# [ "$a" == "$b" ];echo $?[root@server ~]# test "$a" != "$b";echo $?字符串比较test  表达式[ 表达式 ][[ 表达式 ]]思考:[ ] 和 [[ ]] 有什么区别?单个 [  ] 使用字符串对字符串必须加双引号两个 [[  ]] 不用对字符串变量加双引号两个 [[  ]] 里面可以使用 &&,||, 而单个不行两个 [[  ]] 支持c风格如:	a=1; b=2	[[ $a > $b ]] && echo $a || echo $b[root@server ~]# a=[root@server ~]# test -z $a;echo $?[root@server ~]# a=hello[root@server ~]# test -z $a;echo $?[root@server ~]# test -n $a;echo $?[root@server ~]# test -n "$a";echo $?# [ '' = $a ];echo $?-bash: [: : unary operator expected2# [[ '' = $a ]];echo $?0[root@server ~]# [ 1 -eq 0 -a 1 -ne 0 ];echo $?[root@server ~]# [ 1 -eq 0 && 1 -ne 0 ];echo $?[root@server ~]# [[ 1 -eq 0 && 1 -ne 0 ]];echo $?

逻辑运算符总结

  1. test exp1

  2. [ exp1 ]

  3. [[ exp1 ]]

  4. (( 整数表达式exp1 ))

  5. -s 判断文件不为空

  6. 符号;和&&和||都可以用来分割命令或者表达式

  7. 分号(;)完全不考虑前面的语句是否正确执行,都会执行;号后面的内容

  8. &&符号,需要考虑&&前面的语句的正确性,前面语句正确执行才会执行&&后的内容;反之亦然

  9. ||符号,需要考虑||前面的语句的非正确性,前面语句执行错误才会执行||后内容;反之亦然

  10. 如果&&和||一起出现,从左往右依次看,按照以上原则

if语句的结构

单分支结构

F:表示false,为假

T:表示true,为真

if [ condition ];then		command		commandfiif test 条件;then	命令fiif [[ 条件 ]];then	命令fi[ 条件 ] && command

在这里插入图片描述

双分支结构

if [ condition ];then		command1	else		command2fi[ 条件 ] && command1 || command2

在这里插入图片描述

多分支结构

if [ condition1 ];then		command1  	结束	elif [ condition2 ];then		command2   	结束	else		command3fi注释:如果条件1满足,执行命令1后结束;如果条件1不满足,再看条件2,如果条件2满足执行命令2后结束;如果条件1和条件2都不满足执行命令3结束.

在这里插入图片描述

嵌套

if [ condition1 ];then		command1				if [ condition2 ];then			command2		fi else		if [ condition3 ];then			command3		elif [ condition4 ];then			command4		else			command5		fifi注释:如果条件1满足,执行命令1;如果条件2也满足执行命令2,如果不满足就只执行命令1结束;如果条件1不满足,不看条件2;直接看条件3,如果条件3满足执行命令3;如果不满足则看条件4,如果条件4满足执行命令4;否则执行命令5

case多分支结构

说明:pattern表示需要匹配的模式case var in             定义变量;var代表是变量名pattern 1)              模式1;用 | 分割多个模式,相当于or    command1            需要执行的语句    ;;                  两个分号代表命令结束pattern 2)    command2    ;;pattern 3)    command3    ;;		  *)              default,不满足以上模式,默认执行*)下面的语句    command4    ;;esac							esac表示case语句结束

转载地址:http://sfqof.baihongyu.com/

你可能感兴趣的文章
国内外helm源记录
查看>>
牛客网题目1:最大数
查看>>
散落人间知识点记录one
查看>>
Leetcode C++ 随手刷 547.朋友圈
查看>>
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
Leetcode C++ 剑指 Offer 09. 用两个栈实现队列
查看>>
Leetcode C++《每日一题》20200707 112. 路径总和
查看>>
云原生 第十一章 应用健康
查看>>
Leetcode C++ 《第202场周赛》
查看>>