Shell 脚本基础 - 使用 if 语句进行条件检测
创始人
2024-03-01 14:31:45
0

Bourne Shell 的 if 语句和大部分编程语言一样 - 检测条件是否真实,如果条件为真,shell 会执行这个 if 语句指定的代码块,如果条件为假,shell 就会跳过 if 代码块,继续执行之后的代码。

if 语句的语法:

if [ 判断条件 ]
then
        command1
        command2
        ……..
        last_command
fi

Example:

#!/bin/bash
number=150
if [ $number -eq 150 ]
then
    echo "Number is 150"
fi

if-else 语句:

除了标准的 if 语句之外,我们还可以加入 else 代码块来扩展 if 语句。这么做的主要目的是:如果 if 条件为真,执行 if 语句里的代码块,如果 if 条件为假,执行 else 语句里的代码块。

语法:

if [ 判断条件 ]
then
       command1
       command2
       ……..
       last_command
else
       command1
       command2
       ……..
       last_command
fi

Example:

#!/bin/bash
number=150
if [ $number -gt 250 ]
then
    echo "Number is greater"
else
    echo "Number is smaller"
fi

If..elif..else..fi 语句 (简写的 else if)

Bourne Shell 的 if 语句语法中,else 语句里的代码块会在 if 条件为假时执行。我们还可以将 if 语句嵌套到一起,来实现多重条件的检测。我们可以使用 elif 语句(else if 的缩写)来构建多重条件的检测。

语法 :

if [ 判断条件1 ]
then
       command1
       command2
       ……..
       last_command
elif [ 判断条件2 ]
then
        command1
        command2
        ……..
        last_command
else
command1
command2
……..
last_command
fi

Example :

#!/bin/bash
number=150
if [ $number -gt 300 ]
then
    echo "Number is greater"
elif [ $number -lt 300 ]
then
    echo "Number is Smaller"
else
    echo "Number is equal to actual value"
fi

多重 if 语句 :

If 和 else 语句可以在一个 bash 脚本里相互嵌套。关键词 “fi” 表示里层 if 语句的结束,所有 if 语句必须使用 关键词 “fi” 来结束。

基本 if 语句的嵌套语法

if [ 判断条件1 ]
then
        command1
        command2
        ……..
        last_command
else
if [ 判断条件2 ]
then
        command1
        command2
        ……..
        last_command
else
        command1
        command2
         ……..
         last_command
      fi
fi

Example:

#!/bin/bash
number=150
if [ $number -eq 150 ]
then
   echo "Number is 150"
else
if [ $number -gt 150 ]
then
    echo "Number is greater"
else
    echo "'Number is smaller"
   fi
fi

via: http://www.linuxtechi.com/shell-scripting-checking-conditions-with-if/

作者:Pradeep Kumar 译者:ThomazL 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关内容

捕捉添加组成员到本地组的P...
要捕捉添加组成员到本地组的PowerShell脚本的异常,可以使用...
2025-01-12 17:02:04
捕捉shell脚本中的“p...
当在shell脚本中运行psql命令时,如果出现“psql: co...
2025-01-12 17:02:01
不同于Postman,当用...
当使用PowerShell脚本与Microsoft Graph A...
2025-01-10 00:01:49
不确定如何在shell脚本...
在shell脚本中,可以使用条件语句来判断命令执行的结果是否成功,...
2024-12-27 19:31:27
不能通过注册表正确调用Po...
此错误通常是由于在注册表中未正确传递参数所致。以下是一个示例注册表...
2024-12-27 08:01:21
BizTalk通过一个Po...
解决方法可能是通过使用异步处理来提高性能。下面是一个代码示例,展示...
2024-12-20 07:30:49

热门资讯

Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
《Apex 英雄》正式可在 S... 《Apex 英雄》现已通过 Steam Deck 验证,这使其成为支持 Linux 的顶级多人游戏之...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...