linux中用if语句查成绩,linux bash中if条件语句结构总结

bash作为一种脚本语言,自然也提供了if语句,思想跟其他语言类似,但语法区别还是很大的。下面是转载的一片文章,介绍的比较详细。

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)

1. Bash If..then..fi statementif [ conditional expression ]

then

statement1

statement2

.

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/bash

count=100

if [ $count -eq 100 ]

then

echo "Count is 100"

fi

2. Bash If..then..else..fi statementIf [ conditional expression ]

then

statement1

statement2

.

else

statement3

statement4

.

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/bash

count=99

if [ $count -eq 100 ]

then

echo "Count is 100"

else

echo "Count is not 100"

fi

3. Bash If..elif..else..fiIf [ conditional expression1 ]

then

statement1

statement2

.

elif [ conditional expression2 ]

then

statement3

statement4

.

.

.

else

statement5

fi

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/bash

count=99

if [ $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 ]

then

statement1

statement2

.

else

if [ conditional expression2 ]

then

statement3

.

fi

fi

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/bash

count=99

if [ $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"

fi

fi

最后我再提一点我个人使用的经验:if后的expression跟括号之间需要有空格,不然就会报错。

转自:http://www.thegeekstuff.com/2010/06/bash-if-statement-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值