Bash If语句

在本小节中,我们将了解如何在Bash脚本中使用if语句来完成自动化任务。

if语句用于在顺序执行语句的流程中执行条件任务。If语句通常用于在Bash脚本中做出决定。它们根据可能设置的条件来决定是否运行一段代码。

1. if语句基础

基本if语句命令判断如果特定条件为true,则仅执行给定的一组操作。如果条件为false,则不要执行这些操作。if语句基本语法格式如下:

if [ expression ];  
then  
statements  
fi

仅当表达式(在方括号之间)为真时,才会执行thenfi之间的语句。

注意:观察第一行中使用的空格,在第一行末尾使用分号。两者都必须使用,if条件语句以fi结尾。

  • 使用AND运算符使用多个条件:

    if [ expression_1 ] && [ expression_2 ];  
    then  
    statements  
    fi
    
  • 使用OR运算符使用多个条件:

    if [ expression_1 ] || [ expression_2 ];  
    then  
    statements  
    fi
    
  • 对于具有AND&OR运算符的复合表达式,可以使用以下语法:

    if [ expression_1 && expression_2 || expression_3 ];  
    then  
    statements  
    fi
    

以下是一些演示if语句用法的示例:

示例1

在此示例中,读取用户输入任意数字,并检查该值是否大于100

脚本文件:if-example1.sh

#!/bin/bash  

read -p " Enter number : " number  

if [ $number -gt 100 ]  
then  
echo "Value is greater than 100"  
fi

执行上面示例代码,得到以下结果:
《Bash If语句》

示例2

在此示例中,通过比较两个字符串的值,简单演示了if语句的用法。
脚本文件:if-example2.sh

#!/bin/bash  

# if condition is true  
if [ "yiibai" == "yiibai" ];  
then  
echo "true condition"  
fi  

# if condition is false  
if [ "yiibai.com" == "xntutor.com" ];  
then  
echo "false condition"  
fi

执行上面示例代码,得到以下结果:
《Bash If语句》

示例3

在此示例中,演示如何使用if语句比较数字。
脚本文件:if-example3.sh

#!/bin/bash  

#if condition (greater than) is true  
if [ 10 -gt 3 ];  
then  
echo "10 is greater than 3."  
fi  

#if condition (greater than) is false  
if [ 3 -gt 10 ];  
then  
echo "3 is not greater than 10."  
fi  

#if condition (lesser than) is true  
if [ 3 -lt 10 ];  
then  
echo "3 is less than 10."  
fi  

#if condition (lesser than) is false  
if [ 10 -lt 3 ];  
then  
echo "10 is not less than 3."  
fi  

#if condition (equal to) is true  
if [ 10 -eq 10 ];  
then  
echo "10 is equal to 10."  
fi  

#if condition (equal to) is false  
if [ 10 -eq 9 ];  
then  
echo "10 is not equal to 9"  
fi

执行上面示例代码,得到以下结果:
《Bash If语句》

示例4

在此示例中,将演示如何在if表达式中使用AND运算符包括多个条件。

脚本文件:if-example4.sh

#!/bin/bash  

# TRUE && TRUE  
if [ 8 -gt 6 ] && [ 10 -eq 10 ];  
then  
echo "Conditions are true"  
fi  

# TRUE && FALSE  
if [ "mylife" == "mylife" ] && [ 3 -gt 10 ];  
then  
echo "Conditions are false"  
fi

执行上面示例代码,得到以下结果:

maxsu@ubuntu:~/bashcode$ vi if-example4.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example4.sh
maxsu@ubuntu:~/bashcode$ ./if-example4.sh 
Conditions are true
maxsu@ubuntu:~/bashcode$

示例5

在此示例中,将演示如何在if表达式中使用OR运算符包括多个条件。
脚本文件:if-example5.sh

#!/bin/bash  

# TRUE || FALSE  
if [ 8 -gt 7 ] || [ 10 -eq 3 ];  
then   
echo " Condition is true. "  
fi  

# FALSE || FALSE  
if [ "mylife" == "yourlife" ] || [ 3 -gt 10 ];  
then  
echo " Condition is false. "  
fi

执行上面示例代码,得到以下结果:

maxsu@ubuntu:~/bashcode$ vi if-example5.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example5.sh
maxsu@ubuntu:~/bashcode$ ./if-example5.sh 
 Condition is true.

示例6

在此示例中,将演示如何在if表达式中使用ANDOR包括多个条件:
脚本文件:if-example6.sh

#!/bin/bash  

# TRUE && FALSE || FALSE || TRUE  
if [[ 10 -eq 10 && 5 -gt 4 || 3 -eq 4 || 3 -lt 6 ]];  
then  
echo "Condition is true."  
fi  

# TRUE && FALSE || FALSE  
if [[ 8 -eq 8 && 8 -gt 10 || 9 -lt 5 ]];  
then  
echo "Condition is false"  
fi

执行上面示例代码,得到以下结果:

maxsu@ubuntu:~/bashcode$ vi if-example6.sh
maxsu@ubuntu:~/bashcode$ chmod +x if-example6.sh 
maxsu@ubuntu:~/bashcode$ ./if-example6.sh 
Condition is true.

2. Bash If语句选项

if语句包含许多执行特定任务的选项。这些选项可用于文件操作,字符串操作等。以下是一些最常用的选项:

选项(操作符)描述
! EXPRESSION检查EXPRESSION是否为假。
-n STRING检查STRING的长度是否大于零。
-z STRING检查STRING的长度是否为零(即为空)
STRING1 == STRING2检查STRING1是否等于STRING2
STRING1 != STRING2检查STRING1是否不等于STRING2
INTEGER1 -eq INTEGER2检查INTEGER1在数值上是否等于INTEGER2
INTEGER1 -gt INTEGER2检查INTEGER1在数值上是否大于INTEGER2
INTEGER1 -lt INTEGER2检查INTEGER1在数值上是否小于INTEGER2
-d FILE检查FILE是否存在并且它是一个目录。
-e FILE检查FILE是否存在。
-r FILE检查FILE是否存在,并授予读取权限。
-s FILE检查FILE是否存在并且其大小大于零(表示它不为空)。
-w FILE检查FILE是否存在并授予写权限。
-x FILE检查FILE是否存在并授予执行权限。

3. 嵌套if

可以在bash脚本中根据需要应用多个if语句。也可以在一个if语句中使用另一个if语句。这种情况称为嵌套If语句。

示例
在此示例中,将通过使用嵌套的if表达式来找到“给定数字是否大于50,并且是否为偶数”。
脚本文件:if-nested.sh

#!/bin/bash  
#Nested if statement  

if [ $1 -gt 50 ]  
then  
  echo "Number is greater than 50."  

  if (( $1 % 2 == 0 ))  
  then  
    echo "and it is an even number."  
  fi  
fi

执行上面示例代码,得到以下结果:

maxsu@ubuntu:~/bashcode$ vi if-nested.sh 
maxsu@ubuntu:~/bashcode$ chmod +x if-nested.sh 
maxsu@ubuntu:~/bashcode$ ./if-nested.sh 100
Number is greater than 50.
and it is an even number.

        原文作者:Bash Shell教程
        原文地址: https://www.yiibai.com/bash/bash-if-statement.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞