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语句的更多相关文章

  1. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  2. shell编程——if语句【转载】

    (2)shell编程——if语句_macg_新浪博客http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html shell编程——if语句转载 if 语句 ...

  3. shell的case语句简述(shell的流控制)

    shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...

  4. Shell 编程 until语句

    本篇主要写一些shell脚本until语句的使用. 计算1-50的和 #!/bin/bash i=0 s=0 until [ $i -eq 51 ];do let s+=i;let i++ done ...

  5. Shell 编程 循环语句

    本篇主要写一些shell脚本循环语句的使用. for 循环 指定次数 #!/bin/bash for ((i=1;i<=10;i++)) do echo $i done [root@localh ...

  6. Shell 编程 case语句

    本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...

  7. Shell 编程 条件语句

    本篇主要写一些shell脚本条件语句的使用. 条件测试 test 条件表达式 [ 条件表达式 ] 文件测试 -d:测试是否为目录(Directory). -e:测试文件或目录是否存在(Exist). ...

  8. Shell case in语句详解

    和其它编程语言类似,Shell 也支持两种分支结构(选择结构),分别是 if else 语句和 case in 语句.在<Shell if else>一节中我们讲解了 if else 语句 ...

  9. shell 的 功能语句--1

    [1]说明性语句 (1)shell 程序和语句 shell 程序由零或多条shell语句构成. shell语句包括三类:说明性语句.功能性语句和结构性语句. 说明性语句: 以#号开始到该行结束,不被解 ...

  10. shell条件测试语句实例-测试apache是否开启

    终于理解了shell条件测试语句"!="和"-n"的用法区别,于是有了如下的shell脚本,做为练习. 第一种方法:测试apache是否开启?字符串测试 #!/ ...

随机推荐

  1. [转]利用maven的surefire插件实现单元测试与集成测试

    原文链接 http://my.oschina.net/dlpinghailinfeng/blog/301136 maven单元测试与集成测试 通过maven的Profile 配置生命周期 通过mave ...

  2. 负载均衡-多台机子session不起效:把php.ini中file改为memcache存储

    一 开启memcache服务 二 修改php.ini中session配置 php/lib/php.ini session.save_handler = memcache session.save_pa ...

  3. linux正确重启MySQL的方法

    修改了my.cnf,需要重启MySQL服务,正确重启MYSQL方法请看下面的文章 由于是从源码包安装的Mysql,所以系统中是没有红帽常用的servcie mysqld restart这个脚本 只好手 ...

  4. VMware Workstation 10.0 下载 – 正版序列号+简体中文官方原版

    1.https://download3.vmware.com/software/wkst/file/VMware-workstation-full-10.0.0-1295980.exe 2.https ...

  5. 在ubuntu下利用minicom实现串口通信

    windos有串口调试助手,linux下也有这样的工具——minicom.不过,minicom和linux下的许多工具都一样,也是命令行模式,没有图形化界面供我们享受.作为一款串口调试工具,虽然难看但 ...

  6. C++返回引用的函数

    要以引用返回函数值,则函数定义时的格式如下: 类型标识符&类型名 (形参列表及类型说明) { 函数体 } 用const限定引用的声明方式为: const 类型标识符&引用名=目标变量名 ...

  7. BZOJ 1571: [Usaco2009 Open]滑雪课Ski

    Description Farmer John 想要带着 Bessie 一起在科罗拉多州一起滑雪.很不幸,Bessie滑雪技术并不精湛. Bessie了解到,在滑雪场里,每天会提供S(0<=S& ...

  8. hdu 4751

    一道很简单的题,不过在比赛的时候没有写出来: 刚刚看到这个题,我以为是一个图论题,后来发现其实就是一个暴力的题: 用bfs,因为一个人与他不认识的人肯定不会在一个集合,如果判断出现冲突则分配失败,否则 ...

  9. java service

    http://www.regexlab.com/zh/jar2exe/demo1.htm http://www.zhangxinxu.com/wordpress/?p=1058 http://www. ...

  10. win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321)

    转自win7 任务计划 任务映像已损坏或篡改(异常来自HRESULT:0x80041321) 请这样操作: 1. 以管理员身份运行命令提示符并执行命令 chcp 437 schtasks /query ...