• shell脚本传可选参数 getopts 和 getopt的方法


    写了一个shell脚本,需要向shell脚本中传参数供脚本使用,达到的效果是传的参数可以是可选参数

    下面是一个常规化的shell脚本:

            echo "执行的文件名为: $0";
            echo "第一个参数名为: $1";
            echo "第二个参数名为: $2"

    正常的向shell脚本中传参数的方法为:

                 ./test.sh 1 2 3

    最后执行的结果为:

            执行的文件名为: ./test.sh
            第一个参数名为: 1
            第二个参数名为: 2

    但是这个是只能按照顺序传递参数,并且不能传递可选参数,然后查资料,发现了一个shell的getopts 用法

     首先贴个例子

    复制代码
    [hello@Git shell]$ bash test.sh -a hello
    this is -a the arg is ! hello
    [hello@Git shell]$ more test.sh 
    #!/bin/bash
     
    while getopts "a:" opt; do
      case $opt in
        a)
          echo "this is -a the arg is ! $OPTARG" 
          ;;
        ?)
          echo "Invalid option: -$OPTARG" 
          ;;
      esac
    done
    复制代码

    getopts一共有两个参数,第一个是-a这样的选项,第二个参数是 hello这样的参数。

    选项之间可以通过冒号:进行分隔,也可以直接相连接,:表示选项后面必须带有参数,如果没有可以不加实际值进行传递

    例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。

    复制代码
    [hello@Git shell]$ bash test.sh -a hello -b
    this is -a the arg is ! hello
    test.sh: option requires an argument -- b
    Invalid option: -
    [hello@Git shell]$ bash test.sh -a hello -b hello -c 
    this is -a the arg is ! hello
    this is -b the arg is ! hello
    this is -c the arg is ! 
    [hello@Git shell]$ more test.sh 
    #!/bin/bash
     
    while getopts "a:b:cdef" opt; do
      case $opt in
        a)
          echo "this is -a the arg is ! $OPTARG" 
          ;;
        b)
          echo "this is -b the arg is ! $OPTARG" 
          ;;
        c)
          echo "this is -c the arg is ! $OPTARG" 
          ;;
        ?)
          echo "Invalid option: -$OPTARG" 
          ;;
      esac
    done
    [hello@Git shell]$ 
    复制代码

    执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号,比如下面的

    情况一,没有冒号:

    复制代码
    [hello@Git shell]$ bash test.sh -a hello
    this is -a the arg is ! hello
    [hello@Git shell]$ bash test.sh -a
    test.sh: option requires an argument -- a
    Invalid option: -
    [hello@Git shell]$ more test.sh 
    #!/bin/bash
     
    while getopts "a:" opt; do
      case $opt in
        a)
          echo "this is -a the arg is ! $OPTARG" 
          ;;
        ?)
          echo "Invalid option: -$OPTARG" 
          ;;
      esac
    done
    [hello@Git shell]$ 
    复制代码

    情况二,有冒号:

    复制代码
    [hello@Git shell]$ bash test.sh -a hello
    this is -a the arg is ! hello
    [hello@Git shell]$ bash test.sh -a 
    [hello@Git shell]$ more test.sh 
    #!/bin/bash
     
    while getopts ":a:" opt; do
      case $opt in
        a)
          echo "this is -a the arg is ! $OPTARG" 
          ;;
        ?)
          echo "Invalid option: -$OPTARG" 
          ;;
      esac
    done
    复制代码

    情况一输入 -a 但是后面没有参数的的时候,会报错误,但是如果像情况二那样就不会报错误了,会被忽略。

    while getopts ":a:bc" opt  #第一个冒号表示忽略错误;字符后面的冒号表示该选项必须有自己的参数 

    参考 https://blog.csdn.net/xluren/article/details/17489667

    但是这样还是无法满足可以输入可选参数的要求,这样输入的参数名称也是固定的,参数的数量也是固定的,然后接下来了解了shell 的getopt用法。

    看过官方文档后,自己写了个小demo

    复制代码
    #!/bin/bash
    
    #获取对应的参数 没有的话赋默认值
    ARGS=`getopt -o a::b::l::n::t::p:: --long along::,blong::,llong::,plong:: -n 'example.sh' -- "$@"`
    if [ $? != 0 ]; then
        echo "Terminating..."
        exit 1
    fi
    
    #echo $ARGS
    #将规范化后的命令行参数分配至位置参数($1,$2,...)
    eval set -- "${ARGS}"
    
    while true;
    do
        case "$1" in
            -a|--along)
                case "$2" in
                    "")
                        project1_name=master;
                        shift 2;
                        ;;
                     *)
                        project1_name=$2;
                        shift 2;
                        ;;
                esac
                 ;;
            -b|--blong)
                case "$2" in
                    "")
                        project2_name=master;
                        shift 2
                        ;;
                     *)
                      project2_name=$2;
                        shift 2;
                        ;;
                esac
                 ;;
            -l|--llong)
                case "$2" in
                    "")
                        layer=layer_1;
                        shift 2
                        ;;
                    *)
                        layer=$2;
                        shift 2;
                        ;;
                esac
                ;;
            -n)
                case "$2" in
                    "")
                        number=1000;
                        shift 2
                        ;;
                    *)
                        number=$2;
                        shift 2;
                        ;;
                esac
                ;;
            -t)
                case "$2" in
                    "")
                        select_type=top;
                        shift 2
                        ;;
                    *)
                        select_type=$2;
                        shift 2;
                        ;;
                esac
                ;;
             -p)
                case "$2" in
                    "")
                        data_point=;
                        shift 2
                        ;;
                    *)
                        data_point=$2;
                        shift 2;
                        ;;
                esac
                ;;
    
            --)
                shift
                break
                ;;
            *)
                echo "Internal error!"
                exit 1
                ;;
        esac
    
    done
    
    project1_name=${project1_name:-master}
    project2_name=${project2_name:-master}
    layer=${layer:-layer_1}
    number=${number:-100}
    select_type=${select_type:-top}
    data_point=${data_point:-}
    复制代码

    这一部分为输入参数对应的字符

    a::b::l::n::t::p::

    #-o表示短选项,两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项,如-carg 而不能是-c arg

    #--long表示长选项

    一目了然,首先是根据传递过来的 是 -a  还是 -b  -l 进行判断,如果只有-a后面没有参数的话 则赋默认值,同时在

    project1_name=${project1_name:-master}
    project2_name=${project2_name:-master}
    layer=${layer:-layer_1}
    number=${number:-100}
    select_type=${select_type:-top}
    data_point=${data_point:-}

    这一步 也进行了默认值的赋值操作。如果上面没有 project1_name  则赋值为master

    例子如下:

    复制代码
    参数的默认值设置
    
    $cat myscript.sh
    
    print ${1:-hello}
    
    print ${2:-kshell}
    
    $sh myscript.sh
    
    hello
    
    kshell
    复制代码
  • 相关阅读:
    Django学习-开篇
    php7 安装event扩展
    nginx try_files 举例详解
    php7.2.3 安装pcntl 扩展
    nginx配置php-pathinfo
    thinkphp3.2.3使用PDO的问题
    niginx参数配置详解(转)
    lnmp编译安装:centos7+nginx-1.12+mariadb10.2+php7.2
    Git 误删本地代码恢复
    Typora + PicGo + Gitee 解放你对图片的管理
  • 原文地址:https://www.cnblogs.com/linwenbin/p/10800756.html
Copyright © 2020-2023  润新知