在任何一门语言中,判断语句总是少不了,今天来学习一下Shell中的if语句。

基本语法

单分支情况

  • 第一种语法
if <条件表达式>
then
语句
fi
  • 第二种语法
if <条件表达式>;then
语句
fi

其中条件表达式部分可以是test、[]、[[]]和(())等条件表达式。以上两种格式,可根据自己实际情况选择一种即可。

双分支情况

if <条件表达式>
then
语句
else
语句
fi

多分支情况

if <条件表达式>
then
语句
elif <条件表达式>
then
语句
elif <条件表达式>
then
语句
else
语句
fi

在多分支的if中,每个elif中后均需要带有then

分支嵌套情况

if <条件表达式>
then
if <条件表达式>
then
语句
fi
fi

在以上的写法注意缩进,方便阅读

建议在一个if嵌套不要超过三层

if与条件表达式语法

    在前面讲过各个条件测试表达式,如test、[]、[[]]和(())等条件表达式,如下所示:

  • 1、test表达式
if test <表达式>
then
语句
fi
  • 2、[]表达式
if [ <表达式> ]
then
语句
fi
  • 3、[ [ ] ]表达式
if [[ <表达式> ]]
then
语句
fi
  • 4、(( ))表达式
if (( <表达式> ))
then
语句
fi
  • 5、命令表达式
if 命令
then
语句
fi

if示例

1、if示例:判断文件是否且为普通文件

[root@localhost ~]# [ -f /etc/passwd ] && echo true || echo false
true
[root@localhost ~]# test -f /etc/passwd && echo true || echo false
true

与以下写法等效

[root@localhost Test]# cat if.sh
#!/bin/bash
if [ -f "$1" ]
then
echo true
else
echo false
fi if test -f "$2"
then
echo true
else
echo false
fi
[root@localhost Test]# bash if.sh /etc/passwd /etc/hostssss
true
false

2、if示例:比较输入数字的大小

[root@localhost Test]# cat compareNum.sh
#!/bin/bash
a=$1
b=$2
echo "Inputed number is:" ${a} ${b} if [ $# -ne 2 ]
then
echo "input number must be 2 number."
exit 2
fi expr $a + 2 &> /dev/null # 检查是否为整数
resulta=$?
expr $b + 2 &> /dev/null # 检查是否为整数
resultb=$? if [ $resulta -eq 0 -a $resultb -eq 0 ] # 判断检查结果
then
if [ $a -gt $b ]
then
echo "$a > $b"
elif [ $a -lt $b ]
then
echo "$a < $b"
elif [ $a -eq $b ]
then
echo "$a = $b"
else
echo "error"
fi
else
echo "please check your input"
fi [root@localhost Test]# bash compareNum.sh 1 # 输入一个数字
Inputed number is: 1
input number must be 2 number. [root@localhost Test]# bash compareNum.sh a b # 输入字母
Inputed number is: a b
please check your input [root@localhost Test]# bash compareNum.sh 900 89 # 输入两个数字
Inputed number is: 900 89
900 > 89 [root@localhost Test]# bash compareNum.sh 89 900
Inputed number is: 89 900
89 < 900 [root@localhost Test]# bash compareNum.sh 900 900
Inputed number is: 900 900
900 = 900

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

Shell编程-06-Shell中的if语句的更多相关文章

  1. shell编程系列7--shell中常用的工具find、locate、which、whereis

    shell编程系列7--shell中常用的工具find.locate.which.whereis .文件查找之find命令 语法格式:find [路径] [选项] [操作] 选项 -name 根据文件 ...

  2. shell编程系列6--shell中的函数

    shell编程系列6--shell中的函数 .函数介绍 linux shell中的函数和大多数编程语言中的函数一样 将相似的任务或者代码封装到函数中,供其他地方调用 语法格式 第一种格式 name() ...

  3. 【Shell编程】Shell程序设计

    1.Shell简介   作为Linux灵感来源的Unix系统最初是没有图形化界面的,所有的任务都是通过命令行来实现的.因此,Unix的命令行系统得到了很大的发展,逐步成为一个功能强大的系统.   Sh ...

  4. shell编程01—shell基础

    01.学习shell编程需要的知识储备 1.vi.vim编辑器的命令,vimrc设置 2.命令基础,100多个命令 3.基础.高端的网络服务,nfs,rsync,inotify,lanmp,sersy ...

  5. Linux shell编程02 shell程序的执行 及文件权限

    第一个shell脚本 1.       shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2.       ...

  6. 【Shell编程】Shell基本语法

    Shell 语法   Shell程序设计作为一种脚本语言,在Linux系统中有广泛的应用,本文记录了关于Shell程序设计的基础语法知识和常用命令,方便查询,熟练使用shell也需要经常实践,这对于完 ...

  7. Shell编程(二)——shell的基础知识及常用命令

    shell的基础知识 一.bash有以下特点: 1.记录命令历史 2.指令和文件名补全 3.别名 alias rm='rm -i' 4.通配符 * 0个或多个字符 ?​匹配一个字符 5 输入输出重定向 ...

  8. Shell编程中括号判断中赋值语句和判断语句

    #!/bin/bash declare var="xxx" # without space and use one = #1.judge whether the assignmen ...

  9. Linux - 简明Shell编程06 - 循环语句(Loop)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 #!/bin/bash # for循环 for fil ...

  10. Linux shell编程 4 ---- shell中的循环

    1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...

随机推荐

  1. linux查看网卡驱动

    [root@hudson ~]# yum install ethtool -y [root@hudson ~]# ethtool -i em1driver: bnx2version: 2.2.3fir ...

  2. Servlet Response 重定向

    重定向 response.sendRedirect("index.jsp");       //登录用户名不存在,重定向到index.jsp 1重定向在客户端发挥作用,通过浏览器重 ...

  3. 群晖Nas中搭建Intellij Idea的LicenseServer服务

    下载IntelliJIDEALicenseServer(直接找度娘) 准备 shellX 或其他 ssh工具,个人比较喜欢 mobaxterm. 通过 ssh工具连接到群晖中,用户名和密码就是登陆群晖 ...

  4. webpack 常用插件及作用

    copy-webpack-plugin :复制文件到目标文件夹.在开发时使用热模替换,(没有生成dist 文件夹,都在内存中),如果想引用某一个js文件,直接写script标签是找不到的,因为服务器内 ...

  5. clear(), evict(), flush()三种方法的用法实例

    先贴代码: @Before public void init() { System.out.println("Test开始之前执行"); Configuration configu ...

  6. 配置Maven从私服下载构件

    --------------------siwuxie095                                     配置 Maven 从私服下载构件         从 Nexus ...

  7. PS切图导出代码后出现的图片布局散乱的解决方法——table布局

    前言: 一般来说,大部分美工PS切图后导出的都是使用PS默认的table布局的页面,出现最多的异常是上传代码,替换图片后,发现图片布局散乱,完全不是想要的效果.轻微的是浏览器不兼容,只有部分浏览器可以 ...

  8. 如何利用jQuery post传递含特殊字符的数据【转】

    在jQuery中,我们通常利用$.ajax或$.post进行数据传递处理,但这里通常不能传递特殊字符,如:“<”.本文就介绍如何传递这种含特殊字符的数据. 1.准备页面和控制端代码 页面代码如下 ...

  9. CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板

    C#生成sql视图的实体类 using System;using System.Text;using CodeSmith.Engine;using SchemaExplorer;using Syste ...

  10. CentOS7下搭建基本LNMP环境,部署WordPress

    系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:nginx-1.12.2.tar.gz php 7.1.11 ...