击getopts的:读书$ OPTARG可选的标志吗? [英] Bash getopts: reading $OPTARG for optional flags?

查看:222
本文介绍了击getopts的:读书$ OPTARG可选的标志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能在我的脚本接受强制性和可选标志。这里是我到目前为止所。

I'd like to be able to accept both mandatory and optional flags in my script. Here's what I have so far.

#!bin/bash

while getopts ":a:b:cdef" opt; do
      case $opt in
        a ) APPLE="$OPTARG";;
        b ) BANANA="$OPTARG";;
        c ) CHERRY="$OPTARG";;
        d ) DFRUIT="$OPTARG";;
        e ) EGGPLANT="$OPTARG";;
        f ) FIG="$OPTARG";;
        \?) echo "Invalid option: -"$OPTARG"" >&2
            exit 1;;
        : ) echo "Option -"$OPTARG" requires an argument." >&2
            exit 1;;
      esac
    done
echo "Apple is "$APPLE""
echo "Banana is "$BANANA""
echo "Cherry is "$CHERRY""
echo "Dfruit is "$DFRUIT""
echo "Eggplant is "$EGGPLANT""
echo "Fig is "$FIG""

然而,输出为以下

However, the output for the following:

bash script.sh -a apple -b banana -c cherry -d dfruit -e eggplant -f fig

...此输出:

...outputs this:

Apple is apple
Banana is banana
Cherry is 
Dfruit is 
Eggplant is 
Fig is

正如你所看到的,可选的标志不能含有$ OPTARG拉动参数,因为它与所需的标志一样。有没有办法读上可选的标志$ OPTARG没有摆脱整齐的:)错误处理?

As you can see, the optional flags are not pulling the arguments with $OPTARG as it does with the required flags. Is there a way to read $OPTARG on optional flags without getting rid of the neat ":)" error handling?

编辑:我清盘遵循以下吉尔伯特的意见。下面是我所做的:

I wound up following the advice of Gilbert below. Here's what I did:

#!/bin/bash

  if [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
    print_usage; exit 1
  else
    while [[ $# -gt 0 ]]; do
      opt="$1"
      shift;
      current_arg="$1"
      if [[ "$current_arg" =~ ^-{1,2}.* ]]; then
        echo "WARNING: You may have left an argument blank. Double check your command." 
      fi
      case "$opt" in
        "-a"|"--apple"      ) APPLE="$1"; shift;;
        "-b"|"--banana"     ) BANANA="$1"; shift;;
        "-c"|"--cherry"     ) CHERRY="$1"; shift;;
        "-d"|"--dfruit"     ) DFRUIT="$1"; shift;;
        "-e"|"--eggplant"   ) EGGPLANT="$1"; shift;;
        "-f"|"--fig"        ) FIG="$1"; shift;;
        *                   ) echo "ERROR: Invalid option: \""$opt"\"" >&2
                              exit 1;;
      esac
    done
  fi

  if [[ "$APPLE" == "" || "$BANANA" == "" ]]; then
    echo "ERROR: Options -a and -b require arguments." >&2
    exit 1
  fi

非常感谢大家。这完美的作品至今。

Thanks so much, everyone. This works perfectly so far.

推荐答案

大多数getopts的外壳已经烦了我很长一段时间,其中包括缺乏支持的可选参数。

Most shell getopts have been annoying me for a long time, including lack of support of optional arguments.

但如果你愿意用--posix类型的参数,请访问<一个href=\"http://stackoverflow.com/questions/14062895/bash-argument-case-for-args-in/14063511#14063511\">bash参数的情况下在$ @

But if you are willing to use "--posix" style arguments, visit bash argument case for args in $@

这篇关于击getopts的:读书$ OPTARG可选的标志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆