测试过程中用shell的地方_linux,shell中if else if的写法,if elif

aeb21e47747e93d9d5af0dc8b16ddbdb.png

需求描述

  在写shell脚本的过程中,用到了if else的写法,突然有多个参数需要判断

  那么就想到了if else if的用法,于是进行如下的测试。

测试过程

1.写如下的测试脚本,进行多个值的判断

#!/bin/bash
if [[ $1 = 'tomcat' ]];
then
echo "Input is tomcat"
else if [[ $1 = 'redis' ]] || [[ $1 = '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 [[ $1 = 'tomcat' ]];
then
echo "Input is tomcat"
elif [[ $1 = 'redis' ]] || [[ $1 = '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

转自:

linux,shell中if else if的写法,if elif​www.cnblogs.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值