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是否开启?字符串测试 #!/ ...
随机推荐
- 相看系统中用户的信息 passwd, shadow
用用户的信息都保存在 etc/passwd 和 etc/shadow 文件中,其中 shadow 保存的是经过加密码的 能过 cat etc/passwd 和 cat etc/shadow 来查看相关 ...
- demo_06Canvas
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- javaScript & jquery完美判断图片是否加载完毕
好久没写东西了,正好最近因为工作需要,写了一个瀑布流异步加载的程序. 今天就不谈瀑布流,来谈一下关于load的问题. ----------------------------------------- ...
- xml转array
1.字串 $xml = simplexml_load_string($data);$array = json_decode(json_encode($xml),TRUE); 2.文件$xml = si ...
- 快速设置IP的脚本
@echo off cls ::set NAME="本地连接" set NAME="无线网络连接" set IP=192.168.1.55 set MASK=2 ...
- 解析sql中的表名
最近的项目需求中需要解析sql得表名,由于只需要表名我觉得应该用相对粗暴一点的方式来解析 初步思路: 1.转义字符:去除两个引号连在一起的 2.字符串: 去除所有被引号包裹的 3.括号:识别括号处理 ...
- [日语歌词] If
原唱:西野カナ (にしのカナ) 作词:西野カナ/GIORGIO 13 作曲:GIORGIO CANCEMI 1.单词表 仮名 漢字 ひ 日 あめ 雨 や 止 ちがい 違い とおり 通り じかん 時間 ...
- hdu 4751
一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...
- .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系
转自.h头文件. .lib库文件. .dll动态链接库文件之间的关系 h头文件作用:声明函数接口 dll动态链接库作用:含有函数的可执行代码 lib库有两种: (1)静态链接库(Static Liba ...
- Rotate
hdu4998:http://acm.hdu.edu.cn/showproblem.php?pid=4998 题意:给你n个点,以及绕每个点旋转的弧度.然后,问你经过这n次旋转,平面中的点总的效果是相 ...