Bash条件语句


文档摘要

Bash 条件语句 在上一节中,我们介绍了一些最常用的条件表达式。现在,我们可以将这些表达式与标准的条件语句(如 、 和 语句)一起使用。 if 语句 Bash 中 语句的格式如下: 下面是一个快速示例:如果用户未输入姓名,脚本会提示用户输入姓名: if-else 语句 通过 语句,你可以在 语句中的条件不满足时指定一个操作。我们可以将其与上一节中的条件表达式结合使用,如下所示: 你可以将上述 if 语句与前几章中的所有条件表达式一起使用: 以下是另一个 语句的示例,它会检查当前的 是否为 用户: 如果你将此代码放在脚本的顶部,当 EUID 为 0 时,脚本会退出,并且不会执行脚本的其余部分。这一点曾在 DigitalOcean 社区论坛 上讨论过。 你还可以使用 语句测试多个条件。

Bash 条件语句

在上一节中,我们介绍了一些最常用的条件表达式。现在,我们可以将这些表达式与标准的条件语句(如 ifif-elseswitch case 语句)一起使用。

if 语句

Bash 中 if 语句的格式如下:

if [[ some_test ]] then <commands> fi

下面是一个快速示例:如果用户未输入姓名,脚本会提示用户输入姓名:

#!/bin/bash # Bash if statement example read -p "What is your name? " name if [[ -z ${name} ]] then echo "Please enter your name!" fi

if-else 语句

通过 if-else 语句,你可以在 if 语句中的条件不满足时指定一个操作。我们可以将其与上一节中的条件表达式结合使用,如下所示:

#!/bin/bash # Bash if statement example read -p "What is your name? " name if [[ -z ${name} ]] then echo "Please enter your name!" else echo "Hi there ${name}" fi

你可以将上述 if 语句与前几章中的所有条件表达式一起使用:

#!/bin/bash admin="devdojo" read -p "Enter your username? " username # Check if the username provided is the admin if [[ "${username}" == "${admin}" ]] ; then echo "You are the admin user!" else echo "You are NOT the admin user!" fi

以下是另一个 if 语句的示例,它会检查当前的 User ID 是否为 root 用户:

#!/bin/bash if (( $EUID == 0 )); then echo "Please do not run as root" exit fi

如果你将此代码放在脚本的顶部,当 EUID 为 0 时,脚本会退出,并且不会执行脚本的其余部分。这一点曾在 DigitalOcean 社区论坛 上讨论过。

你还可以使用 if 语句测试多个条件。在此示例中,我们希望确保用户既不是管理员用户,也不是 root 用户,以确保脚本不会造成太大破坏。我们将使用 or operator in this example, noted by ||. This means that either of the conditions needs to be true. If we used the and operator of &&,这意味着两个条件都必须为真。

#!/bin/bash admin="devdojo" read -p "Enter your username? " username # Check if the username provided is the admin if [[ "${username}" != "${admin}" ]] && [[ $EUID != 0 ]] ; then echo "You are not the admin or root user, but please be safe!" else echo "You are the admin user! This could be very destructive!" fi

如果你有多个条件和场景,可以结合 ifelse 语句使用 elif 语句。

#!/bin/bash read -p "Enter a number: " num if [[ $num -gt 0 ]] ; then echo "The number is positive" elif [[ $num -lt 0 ]] ; then echo "The number is negative" else echo "The number is 0" fi

switch-case 语句

与其他编程语言类似,当存在多个不同选项时,你可以使用 case 语句来简化复杂的条件逻辑。与其使用多个 ifif-else 语句,不如使用单个 case statement.

The Bash case 语句。其语法如下:

case $some_variable in pattern_1) commands ;; pattern_2| pattern_3) commands ;; *) default commands ;; esac

结构简要说明如下:

  • 所有 case statements start with the case keyword.
  • On the same line as the case keyword, you need to specify a variable or an expression followed by the in keyword.
  • After that, you have your case patterns, where you need to use ) to identify the end of the pattern.
  • You can specify multiple patterns divided by a pipe: |.
  • After the pattern, you specify the commands that you would like to be executed in case that the pattern matches the variable or the expression that you've specified.
  • All clauses have to be terminated by adding ;; at the end.
  • You can have a default statement by adding a * as the pattern.
  • To close the case statement, use the esac (case typed backwards) keyword.

Here is an example of a Bash case 语句:

#!/bin/bash read -p "Enter the name of your car brand: " car case $car in Tesla) echo -n "${car}'s car factory is in the USA." ;; BMW | Mercedes | Audi | Porsche) echo -n "${car}'s car factory is in Germany." ;; Toyota | Mazda | Mitsubishi | Subaru) echo -n "${car}'s car factory is in Japan." ;; *) echo -n "${car} is an unknown car brand" ;; esac

在此脚本中,我们要求用户输入汽车品牌名称,例如 Tesla、BMW、Mercedes 等。

然后,我们使用 case statement, we check the brand name and if it matches any of our patterns, and if so, we print out the factory's location.

If the brand name does not match any of our case statements, we print out a default message: an unknown car brand.

Conclusion

I would advise you to try and modify the script and play with it a bit so that you could practice what you've just learned in the last two chapters!

For more examples of Bash case statements, make sure to check chapter 16, where we would create an interactive menu in Bash using a cases 语句来处理用户的输入。

免责声明
本文件由基于人工智能的机器翻译服务翻译而成。尽管我们力求翻译准确,但请注意,自动翻译可能包含错误或不准确之处。应以原始语言版本的文件为准。对于关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。


发布者: 作者: 转发
评论区 (0)
U