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


    需求描述

      在写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

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

  • 相关阅读:
    2013上半年中国CRM市场分析报告
    windows运行命令大全
    JVM探索(二)
    JVM探索(一)
    1.数据结构和算法的基础笔记
    java程序性能优化
    http状态码
    mongodb清洗数据
    List的数据结构
    Foundation Data Structure
  • 原文地址:https://www.cnblogs.com/chuanzhang053/p/8566043.html
Copyright © 2020-2023  润新知