六、shell脚本的条件测试与比较

(一)条件表达式的常见语法

1、条件表达式6种写法(if,while)

语法1:test<测试表达式>

语法2:[ <测试表达式>] #中括号两端必须要有空格

语法3:[[<测试表达式>]] #两端必须要有空格

语法4:((测试表达式)) #两端必不需要空格

语法5:(命令表达式)

语法6:命令表达式

实际应用展示

①[]条件表达式
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc/host1 ] && echo 0 || echo 1
1
②test条件表达式:test和[]功能相同
[root@centos6-kvm3 scripts]# test -e /etc/host1 && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# test -e /etc/hosts && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# # [] == test
③[[]]双中括号条件表达式,两边需要空格
[root@centos6-kvm3 scripts]# [[ -e /etc/host1 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [[ -e /etc/hosts ]] && echo 0 || echo 1
0
④双括号条件表达式,两边不需要空格
[root@centos6-kvm3 scripts]# (( -e /etc/hosts )) && echo 0 || echo 1
-bash: ((: -e /etc/hosts : division by 0 (error token is "/hosts ")
1
⑤双括号一般用于计算:
[root@centos6-kvm3 scripts]# ((3>5)) && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# ((3<5)) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# #(())用于计算
⑥expr 表达式:使用括号
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# (expr 1 + a &>/dev/null) && echo 0 || echo 1
1
⑦expr 表达式:使用反引号
[root@centos6-kvm3 scripts]# `expr 1 + a &>/dev/null` && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# `expr 1 + 2 &>/dev/null` && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]#

(二)条件表达式的编辑语法:

1、[<测试表达式>] && 命令1 ||命令2

如果前面表达式成功,那么执行命令1,否则执行命令2

if [ <测试表达式>]
then
命令1
else
命令2
fi

2、多命令情况

当命令很多的时候,我们可以使用大括号把所有命令括起来。如下:

[ <测试表达式> ] && {

命令1

命令2

}||{

命令3

命令4

}

3、只保留执行成功的

[ <测试表达式>] &&{

命令1

命令2

命令3

}

4、只保留执行失败的

[<测试表达式>] || {

命令1

命令2

命令3

}

(三)文件测试表达式

man test

-d:文件为目录且存在
[root@centos6-kvm3 scripts]# [ -d /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# [ -d /etc ] && echo 0 || echo 1
0
-f:判断为文件且存在
[root@centos6-kvm3 scripts]# [ -f /etc/hosts ] && echo 0 || echo 1
0
-e:判断存在,为目录或者文件
[root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# [ -e /etc ] && echo 0 || echo 1
0
-r:判断文件为可读:
[root@centos6-kvm3 scripts]# [ -r /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# ll /etc/hosts
-rw-r--r--. 2 root root 352 Nov 19 2018 /etc/hosts
-x:判断文件为可执行:
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
1
[root@centos6-kvm3 scripts]# chmod +x /etc/hosts
[root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1
0
-s:判断文件大小不为0:
[root@centos6-kvm3 scripts]# [ -s /etc/hosts ] && echo 0 || echo 1
0
[root@centos6-kvm3 scripts]# touch oldboy.log
[root@centos6-kvm3 scripts]# [ -s oldboy.log ] && echo 0 || echo 1
1
应用示例:crond
[root@centos6-kvm3 scripts]# cat /etc/init.d/crond

(四)字符串测试表达式的常见功能说明

n:not zero ,[-n "字符串" ] 字符串长度不为0,表达式为真。
z:zero,[-z "字符串" ] 字符串长度为0,表达式为真。
["字符串1"==“字符串2”] 两个字符串相同为真。
[“字符串1”!=“字符串2”] 两个字符串不相同为真。
注意:
1、字符串就用双引号。
2、等号可以用一个或者两个。
3、等号两端必须要有空格。 [root@centos6-kvm3 scripts]# [ -n "oldboy" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ -z "oldboy" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# char="oldboy"
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# unset char
[root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd"=="ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" == "ff" ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ "dd" == "dd" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "ff" ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ "dd" != "dd" ] && echo 1 || echo 0
0
实例应用:
cat /etc/init.d/crond
cat /etc/init.d/network

实例:

[root@centos6-kvm3 scripts]# cat select1.sh
#!/bin/bash
cat << EOF
1.install lamp
2.install lnmp
3.exit
EOF
read -p "请输入一个序号:" num
[ -z "$num" ] && exit 1 #判断内容是否为空
expr 2 + $num &>/dev/null
if [ $? -ne 0 ]
then
echo "usage:$0{1|2|3}"
exit 1
fi if [ $num -eq 1 ]
then
echo "install lamp..."
elif [ $num -eq 2 ]
then
echo "install lnmp ..."
elif [ $num -eq 3 ]
then
echo "bye..."
exit
else
echo "usage:$0{1|2|3}"
exit 1
fi

(五)整数测试表达式

在[]及test中使用的比较表达式 在(())和[[]]中使用的比较符号 说明
-eq ==或者= 等于equal
-ne != 不等于not equal
-gt > 大于greater then
-ge >= 大于等于greater equal
-lt < 小于 less then
-le <= 小于等于 less equal

实例

[root@centos6-kvm3 ~]# [ 2 -eq 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -gt 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [ 2 -lt 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 > 3 ] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# [ 2 \> 3 ] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 > 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -gt 3 ]] && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# [[ 2 -lt 3 ]] && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# (( 2 -lt 3 )) && echo 0 || echo 1
-bash: ((: 2 -lt 3 : syntax error in expression (error token is "3 ")
1
[root@centos6-kvm3 ~]# (( 2 > 3 )) && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# (( 2 < 3 )) && echo 0 || echo 1
0
[root@centos6-kvm3 ~]# test 2 -gt 3 && echo 0 || echo 1
1
[root@centos6-kvm3 ~]# test 2 -lt 3 && echo 0 || echo 1
0
总结:
1、双中括号中使用 字母表达式。
2、双括号中不适合字母表达式,只适合符号表达式。
3、test表达式只适合符号表达式。

(六)测试题:使用read的交互方式,来比较两个整数的大小。

[root@centos6-kvm3 scripts]# cat test3.sh
#!/bin/bash
read -p "请输入两个整数:" a b
[ -z "$b" ] && {
echo "请输入两个整数。"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "请输入两个整数。"
exit 2
}
[ $a -lt $b ] && {
echo "$a小于$b."
exit 0
}
[ $a -gt $b ] && {
echo "$a大于$b."
exit 0
} [ $a -eq $b ] && {
echo "$a等于$b."
exit 0
} ================
使用if语句:
[root@centos6-kvm3 scripts]# cat test4.sh
#!/bin/bash
read -p "请输入两个整数:" a b
[ -z "$b" ] && {
echo "请输入两个整数。"
exit 1
}
expr $a + $b + 1 &>/dev/null
[ $? -ne 0 ] && {
echo "请输入两个整数。"
exit 2
}
if [ $a -lt $b ]
then
echo "$a小于$b."
elif [ $a -gt $b ]
then
echo "$a大于$b."
else
echo "$a等于$b."
fi
[root@centos6-kvm3 scripts]#

(七)逻辑测试表达式

在[]和test中使用操作符 在[[]]和(())中使用操作符 说明
-a && and 与
-o || or 或
! ! not 非

实例:

[root@centos6-kvm3 scripts]# [ 1 -eq 1 -a -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -a -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 -o -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] -o [ -f /etc/hosts ] && echo 1 || echo 0
-bash: [: too many arguments
0
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] || [ -f /etc/hosts ] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [ 1 -eq 2 ] && [ -f /etc/hosts ] && echo 1 || echo 0
0
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 || -f /etc/hosts ]] && echo 1 || echo 0
1
[root@centos6-kvm3 scripts]# [[ 1 -eq 2 && -f /etc/hosts ]] && echo 1 || echo 0
0

shell脚本之六:shell脚本的条件测试与比较的更多相关文章

  1. 【转】shell学习笔记(四)——条件测试

    1 test 条件检测 当我要检测系统上面某些文件或者是相关的属性时,利用 test 这个命令来工作真是好用得不得了, 举例来说,我要检查 /home/oracle/zy是否存在时,使用: test ...

  2. Shell基础(二):Shell中的数值运算、条件测试操作、使用if选择结构

    一.Shell中的数值运算 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 使用expr.$[ ].let等整数运算工具:定义变量X=1234,然后计算X与78 ...

  3. Shell学习笔记——算数运算与条件测试

    算数运算 1. 使用let命令 #!/sbin/bash var1=2 var2=3 let sum=var1+var2 echo $sum 使用let命令式,变量前不需要加$号 只用于整数运算,不适 ...

  4. shell 字符串比较 算数比较 文件条件测试

    set-group-id即set-gid -->授予了程序其所在组的访问权限 set-user-id即set-uid -->授予了程序其拥有者的访问权限而不是其使用者的访问权限 set-g ...

  5. shell脚本进阶之条件测试与条件语句

       接着上篇博客,今天整理一下关于条件测试和条件语句方面的知识. shell脚本进阶之条件测试    在编写shell脚本时,经常需要对一些条件进行判断,可以使用测试命令test辅助完成测试过程.t ...

  6. shell脚本学习—条件测试和循环语句

    条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...

  7. Shell脚本的条件测试与比较

    Shell脚本的条件测试与比较 一.shell脚本的条件测试 通常,在bash的各种条件结构和流程控制结构中都要进行各种测试,然后根据测试结构执行不同的操作,有时也会与if等条件语句相结合,来完成测试 ...

  8. Shell脚本下条件测试(eq.ne.....)(转载)

    转载:http://cxj632840815.blog.51cto.com/3511863/1168709 Shell编程中的条件测试 在Linux编程中经常会用到判断数值的大小,字符串是否为空这样或 ...

  9. SHELL脚本编程的条件测试

    SHELL脚本编程的条件测试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.条件测试概述 判断某需求是否满足,需要由测试机制来实现 专用的测试表达式需要由测试命令辅助完成测试过 ...

随机推荐

  1. .Net Web Api返回Json数据中原对象变量名大小写问题

    这两天在工作中使用SignalR的WebSocket做数据实时传递的功能开发,在后端主动向前端广播数据以Json传递时,前端获取的Json中对应类的变量名首字母默认传递的是大写.而前端一直获取到的后台 ...

  2. VS2015 远程调试:Remote Debugger

    一.关于Remote Debugger 使用VS远程调试器Remote Debugger,我们可以调试部署在不同机器上的应用程序,如桌面应用程序和Asp.Net应用程序. 二.Remote Debug ...

  3. Ubuntu系统在Anaconda中安装Python3.6的虚拟环境

    原因:Anaconda的python版本是3.7的,TensorFlow尚不支持此版本,于是我们创建一个Python的虚拟环境以支持TensorFlow 创建tf环境 conda create --n ...

  4. Bootstrap 的基本实现

    bootstrap:   UI插件  YUI,  ElementUI Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 响应式布局 ...

  5. Oracle给权限和同义词

    在同一个DB下,用户A创建了一个Table(student),用户B无法访问.如果B想要访问,就需要A赋予B权限. 登录用户A执行下面语句: GRANT SELECT, INSERT, UPDATE, ...

  6. 《Three.js 入门指南》3.1.2 - 一份整齐的代码结构以及使用ORBIT CONTROLS插件(轨道控制)实现模型控制

    3.1.2 正式代码结构 & ORBIT CONTROLS插件(轨道控制) 说明 本节内容属于插入节,<Three.js入门指南>这本书中,只是简单的介绍了一些概念,是一本基础的入 ...

  7. CentOS8中安装maven

    下载maven,具体目录可根据实际情况而定 # wget http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-mave ...

  8. JVM类加载过程详细分析

    双亲委派加载模型 为什么需要双亲委派加载模型 主要是为了安全,避免用户恶意加载破坏JVM正常运行的字节码文件,比如说加载一个自己写的java.util.HashMap.class.这样就有可能造成包冲 ...

  9. 在Android Studio中导入jar包

    #1 下载jar包文件, #2 拷贝到libs目录下 #3 打开你的build.gradle,在dependencies加入如下代码 dependencies {compile files('libs ...

  10. 用最新的版本,蹦最野的迪~~~IDE写大数据程序避坑指南

    文章更新于:2020-04-05 注:本次实验使用的操作系统及各个程序版本号 类别 版本号 说明 操作系统 Ubuntu 16.04.6 LTS 代号 xenial jdk java version ...