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是否开启?字符串测试 #!/ ...
随机推荐
- Dom操作--跑马灯效果
这里给园友们演示的是Dom操作实现跑马灯效果,相信我们很多人都用Winform实现过跑马灯效果,其中的关键就是Tirm控件,那么在Dom操作中是用setInterval方法来实现隔一段时间执行一段代码 ...
- scp命令使用
从 本地 复制到 远程 scp /home/daisy/full.tar.gz root@172.19.2.75:/home/root (然后会提示你输入另外那台172.19.2.75主机的root用 ...
- 如何定时备份远程mysql数据库
通常,站长们都没有自己的服务器,每天都要手动备份数据库那也很麻烦.这里推荐一个方法,利用windows的计划任务来实现. 前提:本地机器上装有mysql服务. 假设本地机器上mysql服务目录 d: ...
- python入门第一天作业。讲师写的代码。
#!/uer/bin/env python # _*_ coding: utf-8 _*_ import sys retry_limit = 3 retry_count = 0 account_fil ...
- WPF中将四个数字字符串值(比如:"10,10,300,300")转为Rect
RectConverter rectConverter = new RectConverter(); string parseString = viewportEntry.Text; if (pars ...
- but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)
以下是错误日志信息: 严重: The web application [/news] registered the JDBC driver [com.mysql.jdbc.Driver] but fa ...
- Ajax编程相对路径与绝对路径
http://www.worlduc.com/blog2012.aspx?bid=16946309 ajax同一域名调用采用相对路径 var url = 'QuerySingleDataByField ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...
- Mongodb与关系型数据库
MongoDB没有固定的关系约束 没有事务, 安全性不高 不一定保证数据的一致性. ACID不符合 NoSQL 放弃了传统关系型数据库严格的事务一致性和范式约束,采用弱一致性模型. http://os ...
- ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57
转自: ARM内核全解析,从ARM7,ARM9到Cortex-A7,A8,A9,A12,A15到Cortex-A53,A57 前不久ARM正式宣布推出新款ARMv8架构的Cortex-A50处理器系列 ...