在帮助选项中包含子参数以便在没有 getopt 或 getopts 的情况下明智地执行?

在帮助选项中包含子参数以便在没有 getopt 或 getopts 的情况下明智地执行?

我正在编写一个可以选择文件并打印特定内容的脚本。例如,

san#./script.sh

Expected Usage : ./script.sh --file1 --dns

(这里检查file1,搜索dns名称并打印。基本上一个参数下还有子参数)

我尝试了单个参数/选项,如下所示:

options=$@

arguments=($options)

index=0;
for argument in $options
do
    index=`expr $index + 1`;
    case $argument in
    -a | --fun1 ) run_function1 ;;
    -b | --fun2 ) run_function2 ;;
    -c | --fun3 ) run_function3 ;;
    esac
done
exit;

[ ${1} ] || helpinfo

任何人都可以建议双参数(子选项)吗?

预期目标选项:

./script.sh


OPTIONS : ./script.sh -h

./script --fun1 stackoverflow
        microsoft
        Google
     --fun2 Yahoo 

基本上每个函数都会查看一个文件。我研究过 getopt 或 getopts,但它没有 long 选项(--long 是不可能的,相反我们只能使用 -l)。但再次不确定子参数。任何人都可以帮忙解决这个问题吗?我不想使用getoptgetopts

答案1

这是一个比我在这里给出的第一个版本更方便使用的版本,特别是它避免了等效长选项和短选项的重复代码。它应该处理您想要的任何选项:短选项 ( -q)、长选项 ( --quiet)、带参数的选项、累积短选项(-qlfinput 而不是-q -l -f input)、唯一缩写的长选项(--qui而不是--quiet)、选项结尾--

大部分代码是固定的;您只需修改标记的部分即可。

#!/bin/bash

# Update USAGE (USAGE1, USAGE2, USAGE3 may remain unchanged):
USAGE='Usage: prog [-q|--quiet] [-l|--list] [-f file|--file file] [-Q arg|--query arg] args'
USAGE1='
Ambiguously abbreviated long option:'
USAGE2='
No such option:'
USAGE3='
Missing argument for'

# List all long options here (including leading --):
LONGOPTS=(--quiet --list --file --query)

# List all short options that take an option argument here
# (without separator, without leading -):
SHORTARGOPTS=fQ

while [[ $# -ne 0 ]] ; do
  # This part remains unchanged
  case $1 in
  --) shift ; break ;;  ### no more options
  -)  break ;;          ### no more options
  -*) ARG=$1 ; shift ;;
  *)  break ;;          ### no more options
  esac

  # This part remains unchanged
  case $ARG in
  --*)
    FOUND=0
    for I in "${LONGOPTS[@]}" ; do
      case $I in
      "$ARG")  FOUND=1 ; OPT=$I ; break ;;
      "$ARG"*) (( FOUND++ )) ; OPT=$I ;;
      esac
    done
    case $FOUND in
    0) echo "$USAGE$USAGE2 $ARG" 1>&2 ; exit 1 ;;
    1) ;;
    *) echo "$USAGE$USAGE1 $ARG" 1>&2 ; exit 1 ;;
    esac ;;
  -["$SHORTARGOPTS"]?*)
    OPT=${ARG:0:2}
    set dummy "${ARG:2}" "$@"
    shift ;;
  -?-*)
    echo "$USAGE" 1>&2 ; exit 1 ;;
  -??*)
    OPT=${ARG:0:2}
    set dummy -"${ARG:2}" "$@"
    shift ;;
  -?)
    OPT=$ARG ;;
  *)
    echo "OOPS, this can't happen" 1>&2 ; exit 1 ;;
  esac

  # Give both short and long form here.
  # Note: If the option takes an option argument, it it found in $1.
  # Copy the argument somewhere and shift afterwards!
  case $OPT in
  -q|--quiet) QUIETMODE=yes ;;
  -l|--list)  LISTMODE=yes ;;
  -f|--file)  [[ $# -eq 0 ]] && { echo "$USAGE$USAGE3 $OPT" 1>&2 ; exit 1 ; }
              FILE=$1 ; shift ;;
  -Q|--query) [[ $# -eq 0 ]] && { echo "$USAGE$USAGE3 $OPT" 1>&2 ; exit 1 ; }
              QUERYARG=$1 ; shift ;;
  *)          echo "$USAGE$USAGE2 $OPT" 1>&2 ; exit 1 ;;
  esac
done

# Remaining arguments are now in "$@":

echo "QUIETMODE = $QUIETMODE"
echo "LISTMODE = $LISTMODE"
echo "FILE = $FILE"
echo "QUERYARG = $QUERYARG"
echo "REMAINING ARGUMENTS:" "$@"

答案2

引用。总是引用。有疑问时;引用。当没有疑问时;引用

例如:你的测试$1(由于 而从未达到exit,如果使用例如调用,会使您的脚本崩溃./myscript "file name with spaces"


当谈到参数循环时,你也许可以从这样的事情开始:

#!/bin/bash

prnt_help()
{
    printf "Usage: %s [OPTION]\n" $(basename "$1")
}

# A simple function only to visualize what gets passed and one of many
# ways to deal with it.
do_list()
{
    printf "do_list:\n"
    printf "OPT: %s\n" "$@"
    printf "My list:\n"
    while [[ -n "$1" && "${1:0:1}" != "-" ]]; do
        printf "Processing %s\n" "$1"
        shift
    done
}

main()
{
    while [[ -n "$1" ]];do
           # This test is not needed, but include it if you find use for it.
        if [[ "${1:0:1}" != "-" ]]; then
            shift
            continue
        fi
        # Check option and pass rest of arguments to matching function
        printf "Checking option %s\n" "$1"
        case "$1" in
        "-l"|"--list") shift; do_list "$@";;
        "-h"|"--help") prnt_help "$0";;
        "--") printf "Rest is not options even if starts with -\n"
            break;;
        esac
        shift
    done
    # If you use "--" to separate out e.g. filenames starting with -
    # then process them here.
}

main "$@"

答案3

内置getopts只解析短选项(ksh93 除外),但您仍然可以添加几行脚本以使 getopts 处理长选项。

这是在中找到的代码的一部分http://www.uxora.com/unix/shell-script/22-handle-long-options-with-getopts

  #== set short options ==#
SCRIPT_OPTS=':fbF:B:-:h'
  #== set long options associated with short one ==#
typeset -A ARRAY_OPTS
ARRAY_OPTS=(
    [foo]=f
    [bar]=b
    [foobar]=F
    [barfoo]=B
    [help]=h
    [man]=h
)

  #== parse options ==#
while getopts ${SCRIPT_OPTS} OPTION ; do
    #== translate long options to short ==#
    if [[ "x$OPTION" == "x-" ]]; then
        LONG_OPTION=$OPTARG
        LONG_OPTARG=$(echo $LONG_OPTION | grep "=" | cut -d'=' -f2)
        LONG_OPTIND=-1
        [[ "x$LONG_OPTARG" = "x" ]] && LONG_OPTIND=$OPTIND || LONG_OPTION=$(echo $OPTARG | cut -d'=' -f1)
        [[ $LONG_OPTIND -ne -1 ]] && eval LONG_OPTARG="\$$LONG_OPTIND"
        OPTION=${ARRAY_OPTS[$LONG_OPTION]}
        [[ "x$OPTION" = "x" ]] &&  OPTION="?" OPTARG="-$LONG_OPTION"

        if [[ $( echo "${SCRIPT_OPTS}" | grep -c "${OPTION}:" ) -eq 1 ]]; then
            if [[ "x${LONG_OPTARG}" = "x" ]] || [[ "${LONG_OPTARG}" = -* ]]; then 
                OPTION=":" OPTARG="-$LONG_OPTION"
            else
                OPTARG="$LONG_OPTARG";
                if [[ $LONG_OPTIND -ne -1 ]]; then
                    [[ $OPTIND -le $Optnum ]] && OPTIND=$(( $OPTIND+1 ))
                    shift $OPTIND
                    OPTIND=1
                fi
            fi
        fi
    fi

    #== discard option argument start with hyphen ==#
    if [[ "x${OPTION}" != "x:" ]] && [[ "x${OPTION}" != "x?" ]] && [[ "${OPTARG}" = -* ]]; then 
        OPTARG="$OPTION" OPTION=":"
    fi

    #== manage options ==#
    case "$OPTION" in
        f  ) foo=1 bar=0                    ;;
        b  ) foo=0 bar=1                    ;;
        B  ) barfoo=${OPTARG}               ;;
        F  ) foobar=1 && foobar_name=${OPTARG} ;;
        h ) usagefull && exit 0 ;;
        : ) echo "${SCRIPT_NAME}: -$OPTARG: option requires an argument" >&2 && usage >&2 && exit 99 ;;
        ? ) echo "${SCRIPT_NAME}: -$OPTARG: unknown option" >&2 && usage >&2 && exit 99 ;;
    esac
done
shift $((${OPTIND} - 1))

这是一个测试:

# Short options test
$ ./foobar_any_getopts.sh -bF "Hello world" -B 6 file1 file2
foo=0 bar=1
barfoo=6
foobar=1 foobar_name=Hello world
files=file1 file2

# Long and short options test
$ ./foobar_any_getopts.sh --bar -F Hello --barfoo 6 file1 file2
foo=0 bar=1
barfoo=6
foobar=1 foobar_name=Hello
files=file1 file2

否则在最近的 Korn Shell 中ksh93,getopts可以自然地解析长选项,甚至显示类似的手册页。 (看http://www.uxora.com/unix/shell-script/20-getopts-with-man-page-and-long-options)。

相关内容