getopt getopts 解析Shell脚本命令行参数

 

http://yejinxin.github.io/parse-shell-options-with-getopts-command

 

http://yejinxin.github.io/parse-shell-options-with-getopt-command

 

 

shell内置的getopts命令来帮助我们处理shell脚本选项和参数,其缺点是只能处理短选项,无法处理长选项。下面,本文将要介绍的是getopt命令,它可以同时处理短选项和长选项。

首先,getopt命令不是一个标准的unix命令,但它在大多数Linux的发行版中都自带了有,如果没有,也可以从getopt官网上下载安装。

在getopt的较老版本中,存在一些bug,不大好用,在后来的版本中解决了这些问题,我们称之为getopt增强版。通过-T选项,我们可以检查当前的getopt是否为增强版,返回值为4,则表明是增强版的。

 

#getopt -T
#echo $?
4
#getopt -V
getopt (enhanced) 1.1.4
 

getopt命令与getopts命令不同,它实际上是通过将参数规范化来帮助我们处理的。具体的用法,如下面的脚本:

#!/bin/bash

#echo $@

#-o或--options选项后面接可接受的短选项,如ab:c::,表示可接受的短选项为-a -b -c,其中-a选项不接参数,-b选项后必须接参数,-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项,用逗号分开,冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -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) 
            echo "Option a";
            shift
            ;;
        -b|--blong)
            echo "Option b, argument $2";
            shift 2
            ;;
        -c|--clong)
            case "$2" in
                "")
                    echo "Option c, no argument";
                    shift 2  
                    ;;
                *)
                    echo "Option c, argument $2";
                    shift 2;
                    ;;
            esac
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

#处理剩余的参数
for arg in $@
do
    echo "processing $arg"
done

 

 

需要注意的是,像上面的-c选项,后面是可接可不接参数的,如果需要传递参数给-c选项,则必须使用如下的方式:

#./getopt.sh -b 123 -a -c456 file1 file2 
Option b, argument 123
Option a
Option c, argument 456
processing file1
processing file2
#./getopt.sh --blong 123 -a --clong=456 file1 file2  
Option b, argument 123
Option a
Option c, argument 456
processing file1
processing file2

 

 

本文出自夜惊心的博客,转载请保留出处

 

 

 自己实现 长选项 参数解析

#!/bin/bash
set -e
set -x

## Global variables
workspace=$PWD/$(dirname $0)
readonly workspace=${workspace%%lite/tools*}
WITH_LOG=OFF
WITH_CV=ON
WITH_EXTRA=ON
WITH_EXCEPTION=ON
WITH_METAL=OFF
MODEL_DIR=""

function print_usage() {
    echo "----------------------------------------------------------------------------------------------------------------------------------------"
    echo -e "| Methods of compiling Padddle-Lite iOS library:                                                                                       |"
    echo "----------------------------------------------------------------------------------------------------------------------------------------"
    echo -e "|  compile iOS armv8+armv7 library:                                                                                                    |"
    echo -e "|     ./lite/tools/build_ios_by_models.sh --model_dir=${model_dir}                                                                   |"
    echo -e "|  compile iOS armv8+armv7 library for GPU:                                                                                            |"
    echo -e "|     ./lite/tools/build_ios_by_models.sh  --with_metal=ON --model_dir=${model_dir}                                                  |"
    echo -e "|  print help information:                                                                                                             |"
    echo -e "|     ./lite/tools/build_ios_by_models.sh help                                                                                         |"
    echo -e "|                                                                                                                                      |"
    echo -e "|  optional argument:                                                                                                                  |"
    echo -e "|     --with_metal: (ON|OFF), controls whether to use metal default is OFF                                                             |"
    echo -e "|     --model_dir: (absolute path to optimized model dir) required when compiling striped library                                      |"
    echo -e "|     --with_cv: (OFF|ON); controls whether to compile cv functions into lib, default is OFF                                           |"
    echo -e "|     --with_extra: (OFF|ON); controls whether to publish extra operators and kernels for (sequence-related model such as OCR or NLP)  |"
    echo -e "|     --with_log: (OFF|ON); controls whether to print log information, default is ON                                                   |"
    echo -e "|     --with_exception: (OFF|ON); controls whether to throw the exception when error occurs, default is OFF                            |"
    echo "----------------------------------------------------------------------------------------------------------------------------------------"
}

# parse command
function init() {
  for i in "$@"; do
    case $i in
      --with_cv=*)
          WITH_CV="${i#*=}"
          shift
          ;;
      --with_extra=*)
          WITH_EXTRA="${i#*=}"
          shift
          ;;
      --with_log=*)
          WITH_LOG="${i#*=}"
          shift
          ;;
      --with_exception=*)
          WITH_EXCEPTION="${i#*=}"
          shift
          ;;
      --with_metal=*)
          WITH_METAL="${i#*=}"
          shift
          ;;
      --model_dir=*)
          MODEL_DIR="${i#*=}"
          shift
          ;;
      help)
          print_usage
          exit 0
          ;;
      *)
          # unknown option
          print_usage
          exit 1
          ;;
    esac
  done
}

init $@

 

https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/lite/tools/build_ios_by_models.sh

 

posted @ 2022-10-25 14:50  sinferwu  阅读(173)  评论(0编辑  收藏  举报