linux,shell中if else if的写法,if elif

需求描述mysql

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

  那么就想到了if else if的用法,因而进行以下的测试。sql

测试过程shell

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

#!/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.执行脚本,看脚本是否正常执行bash

[oracle@standby ~]$ ./ts01.sh zookeeper
./ts01.sh: line 12: syntax error: unexpected end of file

备注:发现执行是错误的,通过查看能够知道,shell脚本中不是else if而是elif这个写法oracle

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.再次执行修改过的脚本spa

[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.

备注:脚本执行正常,正确的输出了须要的结果。code

shell脚本中else if的正确使用方法

if condition;
then
    commands;
elif condition;then
    commands;
else
     commands;
fi

 

文档建立时间:2018年3月14日10:54:11

相关文章
相关标签/搜索