Bash 脚本是在 Linux 环境中完成自动化任务的强大工具。任何编程或脚本语言的关键元素之一都是条件逻辑,在 Bash 中,条件逻辑是通过 if 语句实现的。
在 bash 脚本中,if 语句检查一个条件是否为真。如果是,shell 执行与 If 语句相关的代码块。如果语句不为真,则 shell 跳过 If 语句块的末尾并继续执行。
在本指南中,我们将解释如何在 bash 脚本中使用 if 语句。在 bash shell 脚本中,If 语句可以以 If、If- else、If- If- else、netsted If 和 case 的形式使用。
If Statement
Syntax:
if [ condition_command ]
then
command1
command2
……..
last_command
fi
Bash 提供逻辑运算符来组合 if 语句中的多个条件,常见的逻辑运算符如下:
- -eq: 等于
- -ne: 不等于
- -lt: 小于
- -le: 小于等于
- -gt: 大于
- -ge: 大于等于
在下面的 bash 脚本示例中,我们使用 if 条件语句比较两个数字。
#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -lt 150 ]
then
echo "Number is $n"
fi
当我们运行这个脚本时,如果小于 150,它将打印数字。
if-else Statement
除了普通的 if 语句之外,我们还可以用 else 块扩展 if 语句。基本思想是,如果语句为真,则执行 if 块。如果语句为假,则执行 else 块。
Syntax :
if [ condition_command ]
then
command1
command2
……..
last_command
else
command1
command2
……..
last_command
fi
如果我们修改上面的脚本并插入 else 块,那么它将看起来像下面这样:
#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
echo "Number $n is greater than 150"
else
echo "Number $n is smaller than 150"
fi
如果我们运行脚本并输入数字 129,那么它将执行 else 块,如下所示:
使用 if-else 比较字符串
#!/bin/bash
string1=Debian
string2=RHEL
if [ "$string1" = "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
使用 if-else 检查文件是否存在
#!/bin/bash
file_path=/var/scripts/migratedb.sh
if [ -e "$file_path" ]; then
echo "The file exists."
else
echo "The file does not exist."
fi
If-elif-else Statement
在 bash 脚本中,如果希望使用 if 语句应用多个条件,则使用 if elif else。在这种类型的条件语句中,如果满足第一个条件,则执行下面的代码,否则检查下一个 if 条件,如果不匹配,则执行下面的 else 语句中提到的命令。其语法和示例如下所示。
Syntax :
if [ condition_command ]
then
command1
command2
……..
last_command
elif [ condition_command2 ]
then
command1
command2
……..
last_command
else
command1
command2
……..
last_command
fi
示例如下:
#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
echo "Number $n is greater than 150"
elif [ $n -lt 150 ]
then
echo "Number $n is smaller than 150"
else
echo "Number $n is equal to 150"
fi
脚本执行后,输出如下:
Nested if Statement
If 语句和 else 语句可以嵌套在 bash 脚本中。关键字 fi 显示了内部 if 语句的结束,所有 if 语句都应该以关键字 fi 结束。
Syntax :
if [ condition_command ]
then
command1
command2
……..
last_command
else
if [ condition_command2 ]
then
command1
command2
……..
last_command
else
command1
command2
……..
last_command
fi
fi
当我们修改上面的脚本以使用嵌套 if 语句时,其代码将如下所示
#!/bin/bash
echo "Enter the Number: "
read n
if [ $n -gt 150 ]
then
echo "Number $n is greater than 150"
else
if [ $n -lt 150 ]
then
echo "Number $n is smaller than 150"
else
echo "Number $n is equal to 150"
fi
fi
执行脚本,输出如下:
Case Statement
Case 语句类似于 if 语句,但带有多个 elif。bash 脚本中的 Case 语句展开表达式,然后尝试找到与所有模式的匹配。当找到匹配时,将执行所有语句,直到双分号 “;;”。如果没有找到匹配,则将执行 “*)” 模式下提到的语句。
Syntax :
case expression in
pattern1)
statements
;;
pattern2)
statements
;;
pattern3)
statements
;;
pattern-n)
statements
;;
*)
statements
;;
esac
让我们看一个输入和判断操作系统的例子,脚本如下:
#!/bin/bash
echo "Which is Your Favorite Linux Distro?"
echo "Debian, Ubuntu, SUSE, RHEL, Arch"
read -p "Type Your Linux Distro:" OS
case $OS in
Debian)
echo "Most Stable Linux OS, good choice "
;;
Ubuntu)
echo "Best Linux OS for Desktop and Servers"
;;
SUSE)
echo "Top Linux OS for SAP Application"
;;
Arch)
echo "Flexible OS for experienced Linux users"
;;
*)
echo "Please Enter Correct OS from the list"
;;
esac
执行脚本,输出如下:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。