简体   繁体   English

带有所有可选参数的getopts

[英]getopts with all optional arguments

In order to run a bash script I need some arguments and flags, since the requirements are pretty tricky I've chosen to use a getopt function like this 为了运行bash脚本,我需要一些参数和标志,因为要求非常棘手,所以我选择使用这样的getopt函数

while getopts ":s:g:r" o; do
    case "${o}" in
        s)
        # Variables that require value            
        VALUE1=${OPTARG}
        ;;
        g)
        # Variables that require value
        MGROUP=${OPTARG}
        ;;
        r)
        # Variables that, if present, set just a flag
        ASROOT=1
        ;;
        *)
        echo "Usage: ./myscript.sh -s value1 -g value2 -r"
        ;;
    esac
done

I would like to have all parameters optional so I can write some conditions later in my code, and first two (s and g) with an arguments, the third (r) is only a optional flag. 我希望所有参数都是可选的,以便稍后可以在代码中编写一些条件,并且前两个(s和g)带有参数,第三个(r)只是可选标记。 In the future I may need to add additional parametrers, always optional. 将来我可能需要添加其他参数,这些参数始终是可选的。 Any advice? 有什么建议吗?

Sample script illustrating optional arguments with bash getopts in a style similar to mysql client. 示例脚本以类似于mysql客户端的样式说明了bash getopts的可选参数。 "--" is also supported. 也支持“-”。

Usage: script.sh [-u ] [-p | 用法:script.sh [-u] [-p | -p] [--] arg1 arg2 ... -p] [-] arg1 arg2 ...

#!/bin/bash
#
# Sample script illustrating optional arguments with bash getopts in
#   a style similar to mysql client.  "--" is also supported.
#
# tgage@nobigfoot.com - 5-Oct-2019 - Distribute/use without restriction.
#

# Defaults
username="$(whoami)"
unset password

while getopts ":u:p:" arg; do
  case $arg in
      u)  # Username
      username="$OPTARG"
      ;;
      p)  # Password
      thisarg=$((OPTIND - 1)); thisarg="${!thisarg}"; thisarg="${thisarg:2}"

      if [[ ! -z $OPTARG ]] && [[ "$OPTARG" != "$thisarg" ]]; then
          unset password
          OPTIND=$((OPTIND - 1))
      else
          password="$thisarg"
      fi
      if [[ -z $password ]]; then
          read -s -p "Password: " password
          echo
      fi
      ;;
      \?)
      # Request Help
      echo "Sorry that I can be of no help."
      ;;
      *)
      # Deal with option at end of line
      OPTION=$((OPTIND - 1)); OPTION="${!OPTION}"
      case $OPTION in
          --)
          # Add support for --
          break
          ;;
          -p)
          # Option with optional argument
          unset password
          read -s -p "Password: " password
          echo
          ;;
          *)
          # Invalid argument
          echo "Error; Invalid option \"$OPT\"." >&2
          ;;
      esac
      ;;
  esac
done
shift $((OPTIND -1))

echo "Username: $username"
echo "Password: $password"
echo
echo "Remaining arguments:$*"
echo

The optional parameter are optional to make mandatory check VALUE1 and MGROUP . 可选参数是可选的,用于进行强制检查VALUE1MGROUP For example: 例如:

[[ -n $VALUE1 && -n $MGROUP ]] || {
    echo "mandatory paramter missing"
    exit 1
}

getopts getopts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM