Shell 脚本基础 - 使用 if 语句进行条件检测

简介:

Shell 脚本基础 - 使用 if 语句进行条件检测

Bourne Shell 的 if 语句和大部分编程语言一样 - 检测条件是否真实,如果条件为真,shell 会执行这个 if 语句指定的代码块,如果条件为假,shell 就会跳过 if 代码块,继续执行之后的代码。

if 语句的语法:

 
 
  1. if [ 判断条件 ]
  2. then
  3. command1
  4. command2
  5. ……..
  6. last_command
  7. fi

Example:

 
 
  1. #!/bin/bash
  2. number=150
  3. if [ $number -eq 150 ]
  4. then
  5. echo "Number is 150"
  6. fi

if-else 语句:

除了标准的 if 语句之外,我们还可以加入 else 代码块来扩展 if 语句。这么做的主要目的是:如果 if 条件为真,执行 if 语句里的代码块,如果 if 条件为假,执行 else 语句里的代码块。

语法:

 
 
  1. if [ 判断条件 ]
  2. then
  3. command1
  4. command2
  5. ……..
  6. last_command
  7. else
  8. command1
  9. command2
  10. ……..
  11. last_command
  12. fi

Example:

 
 
  1. #!/bin/bash
  2. number=150
  3. if [ $number -gt 250 ]
  4. then
  5. echo "Number is greater"
  6. else
  7. echo "Number is smaller"
  8. fi

If..elif..else..fi 语句 (简写的 else if)

Bourne Shell 的 if 语句语法中,else 语句里的代码块会在 if 条件为假时执行。我们还可以将 if 语句嵌套到一起,来实现多重条件的检测。我们可以使用 elif 语句(else if 的缩写)来构建多重条件的检测。

语法 :

 
 
  1. if [ 判断条件1 ]
  2. then
  3. command1
  4. command2
  5. ……..
  6. last_command
  7. elif [ 判断条件2 ]
  8. then
  9. command1
  10. command2
  11. ……..
  12. last_command
  13. else
  14. command1
  15. command2
  16. ……..
  17. last_command
  18. fi

Example :

 
 
  1. #!/bin/bash
  2. number=150
  3. if [ $number -gt 300 ]
  4. then
  5. echo "Number is greater"
  6. elif [ $number -lt 300 ]
  7. then
  8. echo "Number is Smaller"
  9. else
  10. echo "Number is equal to actual value"
  11. fi

多重 if 语句 :

If 和 else 语句可以在一个 bash 脚本里相互嵌套。关键词 “fi” 表示里层 if 语句的结束,所有 if 语句必须使用 关键词 “fi” 来结束。

基本 if 语句的嵌套语法

 
 
  1. if [ 判断条件1 ]
  2. then
  3. command1
  4. command2
  5. ……..
  6. last_command
  7. else
  8. if [ 判断条件2 ]
  9. then
  10. command1
  11. command2
  12. ……..
  13. last_command
  14. else
  15. command1
  16. command2
  17. ……..
  18. last_command
  19. fi
  20. fi

Example:

 
 
  1. #!/bin/bash
  2. number=150
  3. if [ $number -eq 150 ]
  4. then
  5. echo "Number is 150"
  6. else
  7. if [ $number -gt 150 ]
  8. then
  9. echo "Number is greater"
  10. else
  11. echo "'Number is smaller"
  12. fi
  13. fi

原文发布时间:2014-12-21

本文来自云栖合作伙伴“linux中国”
目录
相关文章
|
4天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
5天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
12 1
|
6天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
6天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
25 5
|
4月前
|
存储 Shell
Shell编程自动化之if、for、while和函数
本文主要介绍了Shell编程自动化之if、for、while和函数,并结合实例测试。
29 3
|
Shell C语言 Windows
【转】shell编程if语句
文章转自:互联网 if 语句格式 if 条件 then   Command else   Command fi             别忘了这个结尾 If语句忘了结尾fi test.
594 0
|
8天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)