比如,我有一个脚本,用这行代码调用:

./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile

或者这个:

./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile 

什么是公认的解析方式,以便在每种情况下(或两者的某种组合)$v、$f和$d都将设置为true,而$outFile将等于/fiz/someOtherFile?


当前回答

注意,getopt(1)是AT&T的一个短暂错误。

getopt创建于1984年,但1986年就已经被埋没了,因为它实际上并不可用。

getopt非常过时的一个证据是,getopt(1)手册页仍然提到“$*”而不是“$@”,这是1986年与getopts(1)Shell内置程序一起添加到Bourne Shell中的,目的是处理带有空格的参数。

顺便说一句:如果您对解析shell脚本中的长选项感兴趣,那么可能需要知道libc(Solaris)和ksh93中的getopt(3)实现都添加了一个统一的长选项实现,该实现支持长选项作为短选项的别名。这使得ksh93和BourneShell通过getopts为长选项实现了统一的接口。

Bourne Shell手册页中的长选项示例:

getopts“f:(file)(输入文件)o:(输出文件)”OPTX“$@”

显示了Bourne Shell和ksh93中可以使用多长时间的选项别名。

请参阅最近Bourne Shell的手册页:

http://schillix.sourceforge.net/man/man1/bosh.1.html

以及OpenSolaris的getopt(3)手册页:

http://schillix.sourceforge.net/man/man3c/getopt.3c.html

最后,使用getopt(1)手册页验证过时的$*:

http://schillix.sourceforge.net/man/man1/getopt.1.html

其他回答

假设我们创建一个名为test_args.sh的shell脚本,如下所示

#!/bin/sh
until [ $# -eq 0 ]
do
  name=${1:1}; shift;
  if [[ -z "$1" || $1 == -* ]] ; then eval "export $name=true"; else eval "export $name=$1"; shift; fi  
done
echo "year=$year month=$month day=$day flag=$flag"

运行以下命令后:

sh test_args.sh  -year 2017 -flag  -month 12 -day 22 

输出将是:

year=2017 month=12 day=22 flag=true

我想提供我的选项解析版本,它允许以下内容:

-s p1
--stage p1
-w somefolder
--workfolder somefolder
-sw p1 somefolder
-e=hello

也允许这样做(可能不需要):

-s--workfolder p1 somefolder
-se=hello p1
-swe=hello p1 somefolder

您必须在使用前决定是否在选项上使用=。这是为了保持代码干净。

while [[ $# > 0 ]]
do
    key="$1"
    while [[ ${key+x} ]]
    do
        case $key in
            -s*|--stage)
                STAGE="$2"
                shift # option has parameter
                ;;
            -w*|--workfolder)
                workfolder="$2"
                shift # option has parameter
                ;;
            -e=*)
                EXAMPLE="${key#*=}"
                break # option has been fully handled
                ;;
            *)
                # unknown option
                echo Unknown option: $key #1>&2
                exit 10 # either this: my preferred way to handle unknown options
                break # or this: do this to signal the option has been handled (if exit isn't used)
                ;;
        esac
        # prepare for next option in this key, if any
        [[ "$key" = -? || "$key" == --* ]] && unset key || key="${key/#-?/-}"
    done
    shift # option(s) fully processed, proceed to next input argument
done

这也可能很有用:您可以设置一个值,如果有人提供输入,则使用该值覆盖默认值。

myscript.sh-f/serverlist.txt或只是/myscript.sh(它采用默认值)

    #!/bin/bash
    # --- set the value, if there is inputs, override the defaults.

    HOME_FOLDER="${HOME}/owned_id_checker"
    SERVER_FILE_LIST="${HOME_FOLDER}/server_list.txt"

    while [[ $# > 1 ]]
    do
    key="$1"
    shift
    
    case $key in
        -i|--inputlist)
        SERVER_FILE_LIST="$1"
        shift
        ;;
    esac
    done

    
    echo "SERVER LIST   = ${SERVER_FILE_LIST}"

如果您正在制作可与其他实用程序互换的脚本,下面的灵活性可能很有用。

以下任一项:

command -x=myfilename.ext --another_switch 

Or:

command -x myfilename.ext --another_switch

代码如下:

STD_IN=0

prefix=""
key=""
value=""
for keyValue in "$@"
do
  case "${prefix}${keyValue}" in
    -i=*|--input_filename=*)  key="-i";     value="${keyValue#*=}";; 
    -ss=*|--seek_from=*)      key="-ss";    value="${keyValue#*=}";;
    -t=*|--play_seconds=*)    key="-t";     value="${keyValue#*=}";;
    -|--stdin)                key="-";      value=1;;
    *)                                      value=$keyValue;;
  esac
  case $key in
    -i) MOVIE=$(resolveMovie "${value}");  prefix=""; key="";;
    -ss) SEEK_FROM="${value}";          prefix=""; key="";;
    -t)  PLAY_SECONDS="${value}";           prefix=""; key="";;
    -)   STD_IN=${value};                   prefix=""; key="";; 
    *)   prefix="${keyValue}=";;
  esac
done

注意,getopt(1)是AT&T的一个短暂错误。

getopt创建于1984年,但1986年就已经被埋没了,因为它实际上并不可用。

getopt非常过时的一个证据是,getopt(1)手册页仍然提到“$*”而不是“$@”,这是1986年与getopts(1)Shell内置程序一起添加到Bourne Shell中的,目的是处理带有空格的参数。

顺便说一句:如果您对解析shell脚本中的长选项感兴趣,那么可能需要知道libc(Solaris)和ksh93中的getopt(3)实现都添加了一个统一的长选项实现,该实现支持长选项作为短选项的别名。这使得ksh93和BourneShell通过getopts为长选项实现了统一的接口。

Bourne Shell手册页中的长选项示例:

getopts“f:(file)(输入文件)o:(输出文件)”OPTX“$@”

显示了Bourne Shell和ksh93中可以使用多长时间的选项别名。

请参阅最近Bourne Shell的手册页:

http://schillix.sourceforge.net/man/man1/bosh.1.html

以及OpenSolaris的getopt(3)手册页:

http://schillix.sourceforge.net/man/man3c/getopt.3c.html

最后,使用getopt(1)手册页验证过时的$*:

http://schillix.sourceforge.net/man/man1/getopt.1.html