4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )
来源:互联网 发布:mac怎么更改字体 编辑:程序博客网 时间:2024/05/16 15:01
http://www.thegeekstuff.com/2010/06/bash-if-statement-examples/
Bash conditional statements perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. These statements are used to execute different parts of your shell program depending on whether certain conditions are true. The ability to branch makes shell scripts powerful.
In Bash, we have the following conditional statements:
- if..then..fi statement (Simple If)
- if..then..else..fi statement (If-Else)
- if..elif..else..fi statement (Else If ladder)
- if..then..else..if..then..fi..fi..(Nested if)
These are similar to the awk if statements we discussed earlier.
1. Bash If..then..fi statement
if [ conditional expression ]thenstatement1statement2.fi
This if statement is also called as simple if statement. If the given conditional expression is true, it enters and executes the statements enclosed between the keywords “then” and “fi”. If the given expression returns zero, then consequent statement list is executed.
if then fi example:
#!/bin/bashcount=100if [ $count -eq 100 ]then echo "Count is 100"fi
2. Bash If..then..else..fi statement
If [ conditional expression ]thenstatement1statement2.elsestatement3statement4.fi
If the conditional expression is true, it executes the statement1 and 2. If the conditional expression returns zero, it jumps to else part, and executes the statement3 and 4. After the execution of if/else part, execution resume with the consequent statements.
if then else fi example:
#!/bin/bashcount=99if [ $count -eq 100 ]then echo "Count is 100"else echo "Count is not 100"fi
Note: This article is part of the ongoing Bash Tutorial series.
3. Bash If..elif..else..fi
If [ conditional expression1 ]thenstatement1statement2.elif [ conditional expression2 ]thenstatement3statement4...elsestatement5fi
You can use this if .. elif.. if , if you want to select one of many blocks of code to execute. It checks expression 1, if it is true executes statement 1,2. If expression1 is false, it checks expression2, and if all the expression is false, then it enters into else block and executes the statements in the else block.
if then elif then else fi example:
#!/bin/bashcount=99if [ $count -eq 100 ]then echo "Count is 100"elif [ $count -gt 100 ]then echo "Count is greater than 100"else echo "Count is less than 100"fi
4. Bash If..then..else..if..then..fi..fi..
If [ conditional expression1 ]thenstatement1statement2.elseif [ conditional expression2 ]thenstatement3.fifi
If statement and else statement could be nested in bash. The keyword “fi” indicates the end of the inner if statement and all if statement should end with the keyword “fi”.
The “if then elif then else fi” example mentioned in above can be converted to the nested if as shown below.
#!/bin/bashcount=99if [ $count -eq 100 ]then echo "Count is 100"else if [ $count -gt 100 ] then echo "Count is greater than 100" else echo "Count is less than 100" fifi
In our next article, we’ll discuss about how to use Bash conditional expressionswith practical examples.
- 4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )
- 4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法--if/then/elif/else/fi
- shell 脚本 if then else fi
- if then fi
- bash if... then....else
- Linux学习 分支(if-then fi, if then elif then fi, case in );;easc
- IDCAMS IF-THEN-ELSE statement
- linux shell ---if...else...fi
- shell结构化命令if-then-elif-fi
- PLSQL: IF-THEN-ELSE
- If...Then...Else
- if-then和if-then-else语句
- 控制流之if..then..fi
- shell编程:理解if-test-then-fi
- 混淆、android应用提高反编译难度
- 《大型网站技术架构》读书笔记二
- javadoc生成doc文件时报编码错误的解决
- IO流列出目录下所有内容,带层次
- response响应给客户端(json、xml格式)
- 4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )
- Hive:ORC与RC存储格式之间的区别
- Struts2访问Servlet API
- java类集
- JVM系列一:JVM内存组成及分配
- PyGobject(九)布局容器之Button篇——Gtk.ToggleButton
- How do I tell if a regular file does not exist in bash?
- “服务主机本地系统占用高内存”,“已禁用IME”和“win8.1 wifi经常掉线”的解决方法
- nginx负载均衡