linux shell 条件判断if else, if elif else....
在linux的shell中
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=10
b=20
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=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
执行结果:
a is not equal to b
3) if ... elif ... fi 语句
if ... elif ... fi 语句可以对多个条件进行判断,语法为:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi
哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。
举个例子:
#!/bin/sh
a=10
b=20
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 语句也可以写成一行,以命令的方式来运行,像这样:
- if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;
if ... else 语句也经常与 test 命令结合使用,如下所示:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出:
The two numbers are equal!
test 命令用于检查某个条件是否成立,与方括号([ ])类似。
linux shell 条件判断if else, if elif else....的更多相关文章
- LINUX SHELL条件判断
算术运算的条件判断 [] [[]]: -eq -ne -lt -le -gt -ge (( )):><>=<== [root@monitor ~]# if (( 2 == 3) ...
- Python基础(条件判断和循环) if elif else for while break continue;
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- Linux shell脚本判断服务器网络是否可以上网
Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需要有个函数来判断服务器网络状态 我们可以通过curl来访问 www ...
- [Shell]条件判断与流程控制:if, case, for, while, until
---------------------------------------------------------------------------------------------------- ...
- Linux shell if判断语句
无论什么编程语言都离不开条件判断.SHELL也不例外. 大体的格式如下: if list then do something here elif list then do another thing ...
- shell条件判断与流程控制
一 条件判断式语句 1.按文件类型进行判断 测试类型 作用 -b 文件 判断文件是否存在,并且是否为块设备文件(是块设备文件为真) -c 文件 判断文件是否存在,并且是否为字符设备文件(是字符设备设备 ...
- shell条件判断if中的-a到-z的意思
[ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则 ...
- shell 条件判断语句整理
常用系统变量 1) $0 当前程式的名称 2) $n 当前程式的第n个参数,n=1,2,…9 3) $* 当前程式的任何参数(不包括程式本身) 4) ...
- 【Linux】条件判断eq、ne、gt、lt、ge、le
整数比较: -eq(equal) 相等 -ne(inequality) 不相等 -gt(greater than) 大于 -lt(less than) 小于 -ge(greater equal) 大于 ...
随机推荐
- webapplicationContext之ServletContext等相关概念说明
1)ServletContext是一个全局的储存信息的空间,所有用户共用一个,其信息必须是线程安全且共享的. ServletContext有一个接口定义:ServletContext接口.此接口定义了 ...
- linux中的dmesg命令以及确定进程是否被系统主动kill
linux中的dmesg命令以及确定进程是否被系统主动kill Feb 21, 2017 | java | 185 Hits 近期发现线上项目的进程莫名其妙的就不见了,也没有崩溃日志,就怀疑是被操作系 ...
- Linux 时间同步 02 ntpd、ntpdate的区别
Linux 时间同步 02 ntpd.ntpdate的区别 目录 Linux 时间同步 02 ntpd.ntpdate的区别 [一]这样做不安全. [二]这样做不精确. [三]这样做不够优雅. ntp ...
- 一键配置网卡IP(win10)脚本
前两天有个小伙伴问我,如何快速配置IP,在公司在家里在宿舍,快速配置IP,然后我特别为这个小伙伴写了一个脚本. @echo off mode con: cols=60 lines=25 title 网 ...
- aix5.3安装httpd服务
1.安装gcc(1)从IBM上下载 gcc-4.0.0-1.aix5.3.ppc.rpm gcc-cplusplus-4.0.0-1.aix5.3.ppc.rpm libgcc-4.0.0-1.aix ...
- Java 并发编程要点
使用线程 有三种使用线程的方法: 实现 Runnable 接口: 实现 Callable 接口: 继承 Thread 类. 实现 Runnable 和 Callable 接口的类只能当做一个可以在线程 ...
- 【win10】win10下两个显示器不同桌面壁纸
win10系统下,双屏显示为不同的桌面壁纸 操作: 1.鼠标右键点击个性化 2.点击背景选项 3.在图片上右键选择要添加为背景的图片 同理,将另一个屏幕壁纸设为监视器1 最后效果为两个分屏为不同桌面壁 ...
- 【ORACLE错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
执行set autotrace traceonly的时候,报错 SQL> set autotrace traceonly SP2-0618: Cannot find the Session Id ...
- C语言字符串结束符“\0”
介绍 '\0'就是8位的00000000,因为字符类型中并没有对应的这个字符,所以这么写.'\0'就是 字符串结束标志. '\0'是转译字符,意思是告诉编译器,这不是字符0,而是空字符.空字符\0对应 ...
- 分布式系统:分布式任务调度xxl-job较深入使用
目录 系统关键概念介绍 执行器 任务 任务配置项描述 阻塞策略 路由策略 日志问题 客户端日志 服务端日志 框架目前发现的缺点以及存在的问题 xxl-job是一个分布式定时任务调度框架,功能强大,底层 ...