安卓系统

Bash if..else语句

11 5 1 Shell编程 流程控制 if语句

11 5 1 Shell编程 流程控制 if语句

目录:

Anonim

决策是计算机编程的最基本概念之一。 像在任何其他编程语言中一样, if if..else if..elif..else 和嵌套的Bash语句可用于基于特定条件执行代码。

在本教程中,我们将向您介绍Bash if 语句的基础知识,并向您展示如何在shell脚本中使用它们。

if 声明

if 条件可以具有不同的形式,则进行重击。 最基本的 if 语句采用以下形式:

if TEST-COMMAND then STATEMENTS fi

if 语句以 if 关键字开头,后跟条件表达式和 then 关键字。 该语句以 fi 关键字结尾。

如果 TEST-COMMAND 评估为 True ,则执行 STATEMENTS 。 如果 TEST-COMMAND 返回 False ,则什么也没有发生,则将忽略 STATEMENTS

通常,最好始终缩进代码并用空行分隔代码块。 大多数人选择使用4空格还是2空格缩进。 缩进和空白行使您的代码更具可读性和组织性。

让我们看下面的示例脚本,该脚本检查给定数字是否大于10。

#!/bin/bash echo -n "Enter a number: " read VAR if] then echo "The variable is greater than 10." fi

将代码保存在文件中,然后从命令行运行它:

bash test.sh

该脚本将提示您输入数字。 例如,如果输入15,则 test 命令将评估为 true 因为15大于10, then 将执行 then 子句中的 echo 命令。

The variable is greater than 10.

if..else 语句

Bash if..else 语句采用以下形式:

if TEST-COMMAND then STATEMENTS1 else STATEMENTS2 fi

如果 TEST-COMMAND 评估为 True ,则将执行 STATEMENTS1 。 否则,如果 TEST-COMMAND 返回 False ,则将执行 STATEMENTS2 。 语句中只能有一个 else 子句。

让我们在前面的示例脚本中添加一个 else 子句:

#!/bin/bash echo -n "Enter a number: " read VAR if] then echo "The variable is greater than 10." else echo "The variable is equal or less than 10." fi

if..elif..else 语句

Bash if..elif..else 语句采用以下形式:

if TEST-COMMAND1 then STATEMENTS1 elif TEST-COMMAND2 then STATEMENTS2 else STATEMENTS3 fi

如果 TEST-COMMAND1 评估为 True ,则将执行 STATEMENTS1 。 如果 TEST-COMMAND2 评估为 True ,则将执行 STATEMENTS2 。 如果没有任何测试命令求值为 True ,则执行 STATEMENTS2

语句中可以有一个或多个 elif 子句。 else 子句是可选的。

条件被顺序评估。 条件返回 True ,其余条件将不执行,并且程序控制将移至 if 语句的末尾。

让我们在前面的脚本中添加一个 elif 子句:

#!/bin/bash echo -n "Enter a number: " read VAR if] then echo "The variable is greater than 10." elif] then echo "The variable is equal to 10." else echo "The variable is less than 10." fi

嵌套 if 语句

Bash允许您将 if 语句嵌套在 if 语句中。 您可以在另一个 if 语句中放置多个 if 语句。

以下脚本将提示您输入三个数字,并在三个数字中打印最大的数字。

#!/bin/bash echo -n "Enter the first number: " read VAR1 echo -n "Enter the second number: " read VAR2 echo -n "Enter the third number: " read VAR3 if] then if] then echo "$VAR1 is the largest number." else echo "$VAR3 is the largest number." fi else if] then echo "$VAR2 is the largest number." else echo "$VAR3 is the largest number." fi fi

输出如下所示:

Enter the first number: 4 Enter the second number: 7 Enter the third number: 2 7 is the largest number. 通常,使用case语句比使用嵌套的 if 语句更有效。

多种条件

逻辑 OR AND 运算符允许您在 if 语句中使用多个条件。

这是脚本的另一个版本,可打印三个数字中最大的数字。 在此版本中,我们将使用逻辑 AND && )运算符代替嵌套的 if 语句。

#!/bin/bash echo -n "Enter the first number: " read VAR1 echo -n "Enter the second number: " read VAR2 echo -n "Enter the third number: " read VAR3 if] &&] then echo "$VAR1 is the largest number." elif] &&] then echo "$VAR2 is the largest number." else echo "$VAR3 is the largest number." fi

测试操作员

在Bash中, test 命令采用以下语法格式之一:

test EXPRESSION]

要否定测试表达式,请使用逻辑 NOT ! )运算符。 比较字符串时,请始终使用单引号或双引号,以避免出现单词拆分或模糊问题。

以下是一些最常用的运算符:

  • -n VAR 如果 VAR 的长度大于零,则为true。 -z VAR 如果VAR为空,则为True。 STRING1 = STRING2 真[STRING1和STRING2相等。 STRING1 != STRING2 真STRING1和STRING2不相等。 INTEGER1 -eq INTEGER2 正确的INTEGER1和INTEGER2相等。 INTEGER1 -gt INTEGER2 正确的INTEGER1大于INTEGER2。 INTEGER1 -lt INTEGER2 正确的INTEGER1小于INTEGER2。 INTEGER1 -ge INTEGER2 正确的INTEGER1等于或大于INTEGER2。 INTEGER1 -le INTEGER2 正确的INTEGER1等于或小于INTEGER2。 -h FILE 如果FILE存在并且是符号链接,则为true。 -r FILE 如果FILE存在且可读,则为true。 -w FILE 如果FILE存在且可写,则为true。 -x FILE 如果FILE存在且可执行,则为true。 -d FILE 如果FILE存在并且是目录,则为true。 -e FILE 如果FILE存在且是一个文件,则与类型(节点,目录,套接字等)无关,为true。 -f FILE 如果FILE存在并且是常规文件(不是目录或设备),则为true。

结论

if if..else if..elif..else 语句使您可以通过评估给定条件来控制Bash脚本的执行流程。

bash终端