AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1327517
Accepted
Cas
Cas
Asked: 2021-03-28 05:00:38 +0800 CST2021-03-28 05:00:38 +0800 CST 2021-03-28 05:00:38 +0800 CST

如何使用 getopts 不接受任何内容、一个标志、一个带参数的标志和一个带两个参数的标志

  • 772

我目前通过以下方式设置 getopts:

while getopts ":p:s:d:g:i:h:" opt; do
        case ${opt} in
            p )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose plex from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
                fi
                selection="plex"
                argument=true
                optarg="$OPTARG"
                ;;

            s )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose sonarr from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
                fi
                selection="sonarr"
                argument=true
                optarg="$OPTARG"
                ;;

            d )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose deluge from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
                fi
                selection="deluge"
                argument=true
                optarg="$OPTARG"
                ;;

            g )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose ip geolocation from the command line with the argument $OPTARG" >> $logfolder/advancedplexapi.log
                fi
                selection="ip geolocation"
                argument=true
                optarg="$OPTARG"
                ;;

            i )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose the info page from the command line but supplied an argument" >> $logfolder/advancedplexapi.log
                fi
                selection="info"
                argument=true
                optarg="$OPTARG"
                ;;

            h )
                if [[ $logging = true ]]
                then
                    echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose help page for flags from the command line but supplied an argument" >> $logfolder/advancedplexapi.log
                fi
                echo "The help flag doesn't support an argument"
                usage | column -t -s "|"
                exit
                ;;

            : )
                case "$OPTARG" in
                    p )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose plex from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        selection="plex"
                        ;;

                    s )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose sonarr from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        selection="sonarr"
                        ;;

                    d )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose deluge from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        selection="deluge"
                        ;;

                    g )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose ip geolocation from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        selection="ip geolocation"
                        ;;

                    i )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose the info page from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        selection="info"
                        ;;

                    h )
                        if [[ $logging = true ]]
                        then
                            echo "$(date '+%d/%m/%Y %H:%M:%S') |     info      | user chose help page for flags from the command line" >> $logfolder/advancedplexapi.log
                        fi
                        usage | column -t -s "|"
                        exit
                        ;;
                esac
                ;;

            \? )
                echo "Invalid usage"
                usage | column -t -s "|"
                exit
                ;;
        esac
    done 2>/dev/null
    shift $((OPTIND -1))

此代码接受以下内容:

./script.sh                #-> just run the script without any variables set
./script.sh -p             #-> selection=plex
./script.sh -s series      #-> selection=sonarr & argument=true & optarg=series
./script.sh -h info        #-> error because -h flag doens't support arguments

#script can be run barebones (./script.sh)
#script can be run with a flag (only one allowed: -p|-s|-d|-g|-i|-h) (./script.sh -p)
#script can be run with a flag and an argument (argument only allowed when using: -p|-s|-d|-g|-i)(./script.sh -s series) (./script.sh -h info -> not allowed)

我的问题:

我需要调整代码以接受第二个参数,但我真的不知道如何:

My goal is:
./script.sh -p history list
=
selection=plex
argument=true
optarg=history
second_argument=true
second_optarg=list

./script.sh -p sessions
=
selection=plex
argument=true
optarg=sessions
second_argument=false
second_optarg=
  • -p -s 和 -d 允许使用第二个参数。
  • 如果另一个标志与两个参数一起使用(一个只允许零个或一个参数的标志),运行echo "The help flag doesn't support an second argument" && usage | column -t -s "|"
  • 当没有给出第二个参数时,second_optarg 需要为空字节
  • 当没有给出第二个参数时,需要将 second_argument 设置为 false
  • 第二个参数(当然只适用于 -p -s 和 -d)不是必需的,它是可选的
command-line
  • 1 1 个回答
  • 581 Views

1 个回答

  • Voted
  1. Best Answer
    glenn jackman
    2021-03-28T07:26:35+08:002021-03-28T07:26:35+08:00

    看起来用户只允许输入一个选项:-p 或 -s 或 ... 但不能输入任何组合。我建议解析不带参数的选项:

    selections=()
    while getopts ":psdgih" opt; do
        case ${opt} in
            p)  selections+=("plex") ;; 
            s)  selections+=("sonarr") ;; 
            d)  selections+=("deluge") ;; 
            g)  selections+=("ip geolocation") ;; 
            i)  selections+=("info") ;; 
            h)  show_help; exit ;;
            \?) echo "Invalid usage"
                usage | column -t -s "|"
                exit
                ;;
        esac
    done 2>/dev/null
    
    case ${#selections[@]} in
        0)  selection="default case" ;;
        1)  selection=${selections[0]} ;;
        *)  echo "only one selection allowed" >&2; exit 1 ;;
    esac
    shift $((OPTIND - 1))
    

    现在您可以随意使用"$@":

    case $# in
        0)  echo "some error" >&2; exit 1 ;;
        1)  optarg=$1
            second_argument=false
            ;;
        *)  optarg=$1
            second_argument=true
            second_optarg=$2
            ;;
    esac
    

    或者

    case $1 in
        history)
            case $2 in
                list) : ...
                      ;;
    
    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve