Shell if else语句
if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句:
- if ... fi 语句;
- if ... else ... fi 语句;
- if ... elif ... else ... fi 语句。
1) if ... else 语句
if ... else 语句的语法:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。
最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。
注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。
#!/bin/sh
a=
b= #下面两种写法都是正确的,除此之外的写法都是错误的
if [ $a == $b ]
then
echo 'a is equal to b'
fi if [ $a != $b ] ; then
echo 'a is not equal to b'
fi
运行结果:
a is not equal to b
2) if ... else ... fi 语句
if ... else ... fi 语句的语法:
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
如果 expression 返回 true,那么 then 后边的语句将会被执行;否则,执行 else 后边的语句。
举个例子:
#!/bin/sh a=
b= if [ $a == $b ] ;then
echo "a is equal to b"
else
echo "a is not equal to b"
fi if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi :<<EOF 下面的写法是错误的,请注意
if [ $a == $b ] then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
EOF
执行结果:
a is not equal to b
a is not equal to b
3) if ... elif ... fi 语句
if ... elif ... fi 语句可以对多个条件进行判断,语法为:
if [ expression ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
elif [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if no expression is true
fi
哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。
举个例子:
#!/bin/sh a=
b= if [ $a == $b ] ; then
echo "a is equal to b"
elif [ $a -gt $b ] ; then
echo "a is greater than b"
elif [ $a -lt $b ] ; then
echo "a is less than b"
else
echo "None of the condition met"
fi
运行结果:
a is less than b
if ... else 语句也可以写成一行,以命令的方式来运行,像这样:
#!/bin/bash if test $[*] -eq $[+]; then echo 'The two numbers are euqal!'; fi;
#!/bin/bash if test $(*) -eq $(+); then echo 'The two numbers are euqal!'; fi;
if ... else 语句也经常与 test 命令结合使用,如下所示:
#!/bin/bash num1=$[*]
num1_1=$[*]
num2=$[+]
num2_1=$[+]
if test $[num1] -eq $[num2] ; then
echo "The two numbers are euqal!"
else
echo 'The tow numbers are not equal!'
fi if test $num1_1 -eq $num2_1 ; then
echo "The two numbers are euqal!"
else
echo 'The tow numbers are not equal!'
fi
输出:
The two numbers are euqal!
The tow numbers are not equal!
test 命令用于检查某个条件是否成立,与方括号([ ])类似。
Shell if else语句的更多相关文章
- (二)shell中case语句、程序传参、while
2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...
- shell编程——if语句【转载】
(2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...
- shell的case语句简述(shell的流控制)
shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...
- Shell 编程 until语句
本篇主要写一些shell脚本until语句的使用. 计算1-50的和 #!/bin/bash i=0 s=0 until [ $i -eq 51 ];do let s+=i;let i++ done ...
- Shell 编程 循环语句
本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...
- Shell 编程 case语句
本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...
- Shell 编程 条件语句
本篇主要写一些shell脚本条件语句的使用. 条件测试 test 条件表达式 [ 条件表达式 ] 文件测试 -d:测试是否为目录(Directory). -e:测试文件或目录是否存在(Exist). ...
- Shell case in语句详解
和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...
- shell 的 功能语句--1
[1]说明性语句 (1)shell 程序和语句 shell 程序由零或多条shell语句构成. shell语句包括三类:说明性语句.功能性语句和结构性语句. 说明性语句: 以#号开始到该行结束,不被解 ...
- shell条件测试语句实例-测试apache是否开启
终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...
随机推荐
- express开发实例
express获取参数有三种方法:官网介绍如下 Checks route params (req.params), ex: /user/:id Checks query string params ( ...
- php版网易视频云api
最近在做在线教育课程,使用网易云视频作为在线视频直播. 网易官方只有java示例,我们使用php,就自己写个api. 当然实现也是很简单的. 演示:http://www.deitui.com/inde ...
- 在ECSHOP首页今日特价(促销商品)增加倒计时效果
看到不少朋友在找首页特价商品倒计时的修改方法,写了这篇文章希望能帮到有此需要的朋友们 1.首先修改程序部分 打开includes/lib_goods.php 找到get_promote_goods() ...
- html meta标签用法详细介绍
meta是html语言head区的一个辅助性标签. 在页面中都有类似这样的html代码: <head> <meta http-equiv="content-Type&quo ...
- PowerDesigner 如何添加每个表中共用的字段及自动添加注释
PowerDesigner 如何添加每个表中共用的字段: 有时候在创建表的时候会有一些共用的字段,但是每一张表都要去创建,这样做很麻烦,特别是这样重复的工作,稍不留意就会出现问题,实际上在PD中有这样 ...
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays
Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...
- JS单元测试框架:QUnit
QUnit:jQuery的单元测试框架,但不仅限于jQuery(从这个工具不需要引用jquery.js可以看出) index.html <!-- 官网 http://qunitjs.com/ - ...
- 标签form表单里的属性简单介绍了一下
<form id="form1" name="form1" method="post" action=""> ...
- [原博客] POJ 1740 A New Stone Game
题目链接题意:有n堆石子,两人轮流操作,每次每个人可以从一堆中拿走若干个扔掉(必须),并且可以从中拿走一些分到别的有石子的堆里(可选),当一个人不能拿时这个人输.给定状态,问是否先手必胜. 我们参考普 ...
- 高远介绍的好东东--django-celery
终于可以到异步消息机制的高档产品啦~~~ 不知能不能代替AJAX.. 参照官方文档试下: 中文文档: http://docs.jinkan.org/docs/celery/getting-starte ...