linux,shell中if else if的写法,if elif
需求描述:
在写shell脚本的过程中,用到了if else的写法,突然有多个参数需要判断
那么就想到了if else if的用法,于是进行如下的测试。
测试过程:
1.写如下的测试脚本,进行多个值的判断
#!/bin/bash if [[ $ = 'tomcat' ]];
then
echo "Input is tomcat"
else if [[ $ = 'redis' ]] || [[ $ = 'zookeeper' ]];
then
echo "Input is $1"
else
echo "Input Is Error."
fi
2.执行脚本,看脚本是否正常执行
[oracle@standby ~]$ ./ts01.sh zookeeper
./ts01.sh: line 12: syntax error: unexpected end of file
备注:发现执行是错误的,经过查看可以知道,shell脚本中不是else if而是elif这个写法
3.修改脚本
#!/bin/bash if [[ $ = 'tomcat' ]];
then
echo "Input is tomcat"
elif [[ $ = 'redis' ]] || [[ $ = 'zookeeper' ]];
then
echo "Input is $1"
else
echo "Input Is Error."
fi
4.再次执行修改过的脚本
[oracle@standby ~]$ ./ts01.sh zookeeper
Input is zookeeper
[oracle@standby ~]$ ./ts01.sh tomcat
Input is tomcat
[oracle@standby ~]$ ./ts01.sh redis
Input is redis
[oracle@standby ~]$ ./ts01.sh mysql
Input Is Error.
备注:脚本执行正常,正确的输出了需要的结果。
shell脚本中else if的正确使用方法:
if condition;
then
commands;
elif condition;then
commands;
else
commands;
fi
文档创建时间:2018年3月14日10:54:11
linux,shell中if else if的写法,if elif的更多相关文章
- Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)
case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变量名 in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; *) 默认执行的命令序列 ...
- Linux Shell 中 > 和 >> 的异同点和应用场景
Linux Shell 中 > 和 >> 的异同点和应用场景 > 和 >> 的异同点 举例说明(start.sh 为某个服务的启动脚本,start.log 为某服务 ...
- [转帖]Linux shell中2>&1的含义解释 (全网最全,看完就懂)
Linux shell中2>&1的含义解释 (全网最全,看完就懂) https://blog.csdn.net/zhaominpro/article/details/82630528 ...
- linux shell 中的sleep命令
开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...
- 理解 Linux shell 中的一个方言:2>&1
理解 Linux shell 中的一个方言:2>&1 2016-11-14 杜亦舒 前言 在使用 linux 命令或者 shell 编程时,这个用法常会遇到 2>&1 如 ...
- linux shell 中的位置变量
对于linux shell 中的位置变量,我一直以来都是吐不出来又咽不下去,每次看到都不懂,不懂了就去百度google,看完了又忘,真是慢性咽炎啊.现在认真想想也是,其实自己一直以来都没有好好学习过, ...
- Linux shell中的一个问题 ${}带正则匹配的表达式
目前在准备龙芯项目的PMON,在研究其编译过程的时候,看到一些make 语句,百思不得其解.后来在shell编程中看到一点资料,牵扯到Shell中的正则表达式.故记录下来,以备后来查阅. 问题: 在某 ...
- Linux shell中的竖线(|)——…
原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command ...
- [转载]Linux shell中的竖线(|)——管道符号
原文地址:Linux shell中的竖线(|)--管道符号作者:潇潇 管道符号,是unix一个很强大的功能,符号为一条竖线:"|". 用法: command 1 | command ...
随机推荐
- linux命令(24):/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc
/etc/profile./etc/bashrc.~/.bash_profile.~/.bashrc很容易混淆,他们之间有什么区别?它们的作用到底是什么? /etc/profile: 用来设置系统环境 ...
- hdoj1160 FatMouse's Speed 动态规划
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- js中作用域和闭包
作用域链实例 (1) function example() { var age = 23; alert(age) } var age = 25; example(); alert(age); // ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- egret学习记录
最近h5小游戏比较流行,本来我是做cocos2dx的,一开始想用它的js版. 可惜看着js真是头大.于是选择了egret,egret采用typescript,学过面向对象的,上手还是比较快的,而且ap ...
- BusyBox inittab
# /etc/inittab init(8) configuration for BusyBox## Copyright (C) 1999-2004 by Erik Andersen <ande ...
- CDH impala安装
环境 CDH版本:5.12.1 添加impala parcel 1.菜单“主机”-->Parcel-->配置-->远程 Parcel 存储库 URL,点击添加按钮,添加一个URL,h ...
- ORACLE11G 字符集更改(这里更改为AL32UTF8)
ORACLE11G 字符集更改(这里更改为AL32UTF8)更改步骤:1.用sysdba角色用户登录sqlplus: 命令行输入:sqlplus sys as sysdba 2.输入口令,进入sqlp ...
- MDL---Material Design Lite框架推荐
INTRO material design相比不会陌生, 现在的移动端基本遵循了这个设计规范, 微软退出过一个残次品universal design(花了半个月时间赶出来的规范)也是借鉴了MD的思想, ...
- ApplicationEventMulticaster not initialized - call 'refresh' before
https://stackoverflow.com/questions/45318618/applicationeventmulticaster-not-initialized-call-refres ...