Bash 中的条件语句

简介: 【8月更文挑战第19天】

在 Bash 脚本中,条件语句用于控制脚本的执行流程。通过条件语句,可以根据条件的真假来决定执行不同的代码块。Bash 提供了多种条件语句,包括 ifelifelsecase 等。本文将详细介绍 Bash 中的条件语句,包括其语法、用法、常见示例以及最佳实践。

1. if 语句

定义if 语句用于执行一个或多个命令,如果指定的条件为真,则执行 if 块中的代码。如果条件为假,则跳过 if 块中的代码。

基本语法

if [ condition ]; then
    # commands to be executed if condition is true
fi

示例

#!/bin/bash

num=10

if [ $num -gt 5 ]; then
    echo "Number is greater than 5."
fi

在这个示例中,[ $num -gt 5 ] 是一个条件测试,检查变量 num 是否大于 5。如果条件为真,则执行 echo 命令。

2. if-else 语句

定义if-else 语句用于在条件为假时执行另一个代码块。else 块中的代码在 if 条件为假时执行。

基本语法

if [ condition ]; then
    # commands to be executed if condition is true
else
    # commands to be executed if condition is false
fi

示例

#!/bin/bash

num=3

if [ $num -gt 5 ]; then
    echo "Number is greater than 5."
else
    echo "Number is 5 or less."
fi

在这个示例中,如果 num 不大于 5,则执行 else 块中的 echo 命令。

3. if-elif-else 语句

定义if-elif-else 语句用于处理多个条件。elif 语句是 else if 的缩写,用于检查多个条件。

基本语法

if [ condition1 ]; then
    # commands to be executed if condition1 is true
elif [ condition2 ]; then
    # commands to be executed if condition2 is true
else
    # commands to be executed if all conditions are false
fi

示例

#!/bin/bash

num=7

if [ $num -gt 10 ]; then
    echo "Number is greater than 10."
elif [ $num -eq 7 ]; then
    echo "Number is equal to 7."
else
    echo "Number is less than or equal to 10 and not 7."
fi

在这个示例中,脚本检查 num 是否大于 10,如果不满足,则检查是否等于 7。如果两者都不满足,则执行 else 块中的代码。

4. case 语句

定义case 语句用于处理多个可能的值。它类似于其他编程语言中的 switch 语句,根据变量的值执行不同的代码块。

基本语法

case $variable in
    pattern1)
        # commands to be executed if $variable matches pattern1
        ;;
    pattern2)
        # commands to be executed if $variable matches pattern2
        ;;
    *)
        # commands to be executed if $variable doesn't match any pattern
        ;;
esac

示例

#!/bin/bash

day="Monday"

case $day in
    Monday)
        echo "Today is Monday."
        ;;
    Tuesday)
        echo "Today is Tuesday."
        ;;
    *)
        echo "It's some other day."
        ;;
esac

在这个示例中,case 语句根据变量 day 的值来决定执行哪个代码块。

5. 条件测试运算符

在 Bash 中,条件测试使用方括号 [ ] 或双括号 [[ ]]。常用的条件测试运算符如下:

  • 比较整数

    • -eq:等于
    • -ne:不等于
    • -gt:大于
    • -lt:小于
    • -ge:大于或等于
    • -le:小于或等于
  • 字符串比较

    • =:相等
    • !=:不相等
    • <:小于(按字典顺序)
    • >:大于(按字典顺序)
  • 文件测试

    • -e:文件存在
    • -f:普通文件
    • -d:目录
    • -r:可读
    • -w:可写
    • -x:可执行

示例

#!/bin/bash

file="example.txt"

if [ -e $file ]; then
    echo "File exists."
else
    echo "File does not exist."
fi

在这个示例中,脚本检查 example.txt 文件是否存在。

6. 组合条件

在 Bash 中,可以使用逻辑运算符组合多个条件:

  • 逻辑与-a&&
  • 逻辑或-o||
  • 逻辑非!

示例

#!/bin/bash

num=8

if [ $num -gt 5 ] && [ $num -lt 10 ]; then
    echo "Number is between 6 and 9."
fi

在这个示例中,&& 用于组合两个条件,检查 num 是否在 6 到 9 之间。

7. 结论

Bash 中的条件语句是控制脚本执行流程的关键工具。通过使用 ifif-elseif-elif-elsecase 等条件语句,可以根据不同的条件执行不同的代码块,提高脚本的灵活性和功能。掌握这些条件语句的用法,有助于编写更复杂、更高效的 Bash 脚本。通过合理使用条件测试运算符和逻辑运算符,可以实现更加精确的条件判断,优化脚本的逻辑和性能。

目录
相关文章
|
26天前
|
Shell Linux
在Linux中,使用bash shell实现条件判断和循环结构的例子是什么样的?
在Linux中,使用bash shell实现条件判断和循环结构的例子是什么样的?
|
4月前
|
Shell
在Shell(如Bash)中,`while`循环
在Shell(如Bash)中,`while`循环
69 2
|
10月前
|
编解码 Shell
将条件判断写在bash命令行
将条件判断写在bash命令行
|
Shell
Shell if else 条件判断
Shell if else 条件判断
61 0
|
Shell
【Shell编程】Shell中Bash变量-预定义变量
【Shell编程】Shell中Bash变量-预定义变量
97 0
|
测试技术 Shell
bash之条件判断与字符测试_学习笔记
时间:2017.12.10作者:李强参考:man,info,magedu讲义,万能的internet实验环境:CentOS 6.9与CentOS7.4声明:以下英文纯属个人翻译,英文B级,欢迎纠正,以下内容纯属个人理解,并没有对错,只是参考,盗版不纠,才能有限,希望不误人子弟为好。
842 0
|
Shell 容器 Docker
|
Shell 机器学习/深度学习 Windows