使用getopts的读取一个可选参数放在为final的地方 [英] Using getopts to read one optional parameter placed as final place

查看:173
本文介绍了使用getopts的读取一个可选参数放在为final的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个bash脚本,它的参数的灵活数量,现在我想一个可选的参数( -l <​​/ code>)添加到每个人。

I wrote a bash script that takes flexible number of parameters and now I would like to add an optional argument (-l) to each of them.

我目前有难以获得期望的行为。

I am currently having difficulty getting the desired behavior.

我希望所有的下列正确执行:

I want all of the following to execute correctly:

./Script.sh arg1 arg2 arg3 -l opt 
./Script.sh arg1 arg2 arg3
./Script.sh arg1 arg2 arg3 arg4 -l opt
./Script.sh arg1 arg2 arg3 arg4 arg5

问题是, $ OPTIND 不能设置。
如果 -l选择被放在第一个参数之前,下面的循环工作。

The problem is that $OPTIND cannot be set. The following loop works if the -l opt is placed before first argument.

while getopts ":l:" option
do
    case "$option" in
        t) 
            F_NAME=$OPTARG 
            ;;
    esac
done
shift $((OPTIND - 1))

然而,将可选的 -l <​​/ code>作为最后一个参数是必须的。
什么是实现这一目标的最简单的方法?

However, place the optional -l as last parameter is a requirement. What's the easiest way to achieve this?

推荐答案

下面是一个把戏,我发现使用带有可选参数与参数getopts的。

Here is a trick I have found to use arguments with optional parameters with getopts.

来管理,其中可选的参数是里面的命令是由Jan Schampera中给出的情况下,该方法是在bash-hackers.org回复:

The way to manage the case where the optional parameter is inside the command is given by Jan Schampera in is reply on bash-hackers.org :

  if [[ $OPTARG = -* ]]; then
    ((OPTIND--))
    continue
  fi

(参见: http://wiki.bash-hackers.org/howto/getopts_tutorial在页面深)
但它不管理,其中的选项是在命令的结尾给出的情况。

(see : http://wiki.bash-hackers.org/howto/getopts_tutorial deep in the page) But it does not manage the case where the option is given at the end of the command.

在这种情况下,因为没有给出参数被认为是错误的, getopts的设置选择变量 (冒号)和 OPTARG 来故障选项值。
所以我们要管理'。情况下,与情况下$ OPTARG

In that case, that is considered wrong because no parameter is given, getopts set the opt variable to ':' (colon) and OPTARG to the fault option value. So we have to manage the ':' case, with a case $OPTARG.

让我们说我们写的有三个选项的脚本:

Let us say we write a script having three options :


  • A :不带参数

  • B :用所需的参数

  • v :设置的冗长的从 0 到<$值C $ C> 2 。默认值为 0 和preSET珍惜我们使用时,脚本与 -v 调用无参数或用错误的值。

  • a : without parameter
  • b : with a required parameter
  • v : to set the verbosity with a value from 0 to 2. The default value is 0 and a preset value us used when the script is call with -v without parameter or with a bad value.

下面是code:

Here is the code :

#!/bin/bash

VERBOSITY=0 # default verbosity set to 0
PRESET_VERBOSITY=1 # preset verbosity when asked

while getopts :ab:v: opt; do
  case $opt in
    a)
      echo "manage option a"
    ;;
    b)
      echo "manage option b with value '$OPTARG'"
    ;;
    v)
      if [[ $OPTARG = -* ]]; then # Jan Schampera reply
        echo "set verbosity to PRESET (no value given, not last position)"
        VERBOSITY=$PRESET_VERBOSITY
        ((OPTIND--))
        continue
      fi

      if [[ "$OPTARG" =~ ^[0-2]$ ]]; then
        echo "set verbosity to $OPTARG (good value given)"
        VERBOSITY=$OPTARG
      else
        echo "set verbosity to PRESET (bad value given)"
        VERBOSITY=$PRESET_VERBOSITY
      fi
    ;;
    :)
      case $OPTARG in
        v)
          echo "set verbosity to PRESET (no value given, last option)"
          VERBOSITY=$PRESET_VERBOSITY
        ;;
      esac
    ;;
    \?)
      echo "WTF!"
    ;;
  esac
done

echo "**verbosity is set to $VERBOSITY**"

这篇关于使用getopts的读取一个可选参数放在为final的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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