代码之家  ›  专栏  ›  技术社区  ›  Peter Coulton

Shell脚本参数分析

  •  18
  • Peter Coulton  · 技术社区  · 15 年前

    如何解析long(--example |--example simple option)和short参数(-e |-esimple example |-e simple example)

    1 回复  |  直到 15 年前
        1
  •  35
  •   Expedito    11 年前

    你想用 getopt 有多空两种选择。工作代码示例:

    # Parse arguments
    TEMP=$(getopt -n $PROGRAM_NAME -o p:P:cCkhnvVS \
    --long domain-password:,pop3-password:\         
    ,create,cron,kill,help,no-sync-passwords,version,verbose,skip-pop3 \
    -- "$@")                                                            
    
    # Die if they fat finger arguments, this program will be run as root
    [ $? = 0 ] || die "Error parsing arguments. Try $PROGRAM_NAME --help"       
    
    eval set -- "$TEMP"
    while true; do     
            case $1 in 
                    -c|--create)
                            MODE="CREATE"; shift; continue
                    ;;                                    
                    -C|--cron)                            
                            MODE="CRON"; shift; continue  
                    ;;                                    
                    -k|--kill)                            
                            MODE="KILL"; shift; continue  
                    ;;                                    
                    -h|--help)                            
                            usage                         
                            exit 0                        
                    ;;                                    
                    -n|--no-sync-passwords)               
                            SYNC_VHOST=0; shift; continue 
                    ;;                                    
                    -p|--domain-password)                 
                            DOMAIN_PASS="$2"; shift; shift; continue
                    ;;                                              
                    -P|--pop3-password)                             
                            POP3_PASS="$2"; shift; shift; continue  
                    ;;                                              
                    -v|--version)                                   
                            printf "%s, version %s\n" "$PROGRAM_NAME" "$PROGRAM_VERSION"
                            exit 0                                                      
                    ;;                                                                  
                    -v|--verbose)                                                       
                            VERBOSE=1; shift; continue                                  
                    ;;                                                                  
                    -S|--skip-pop3)                                                     
                            SKIP_POP=1; shift; continue                                 
                    ;;                                                                  
                    --)                                                                 
                            # no more arguments to parse                                
                            break                                                       
                    ;;                                                                  
                    *)                                                                  
                            printf "Unknown option %s\n" "$1"                           
                            exit 1                                                      
                    ;;                                                                  
            esac                                                                        
    done     
    

    注意, die 是以前定义的函数(未显示)。

    这个 -n 选项告诉getopt将错误报告为我的程序名,而不是 格托普 . -o : 在选项指示所需参数后)和 --long 指定长选项列表(按短选项的顺序对应)。

    剩下的只是一个简单的开关,呼叫 shift 适当地推进参数指针。注意,呼叫 shift; shift; 只是个顽固的习惯。在当今世界, shift 2 可能就够了。

    现代的getopt在较新的平台上是相当一致的,但是在较旧的(大约在redhat9之前)系统上您可能会遇到一些可移植性问题。看到了吗 man getopt 有关向后兼容性的信息。然而,你不太可能会遇到这种需要。

    最后,在解析选项之后,您可以再次调用:

    eval set -- "$@"
    

    这将在getopt解析完选项后,将参数指针移到命令行上剩下的任何内容。然后你就可以 转移 继续读下去。例如,如果命令如下所示:

    ./foo --option bar file1.txt file2.txt file3.txt
    

    别忘了做个便当 -h / --help help2man 友好,你有一个即时的手册页去与你的新工具。

    编辑

    格托普 代码输入 /usr/share/doc/util-linux/examples ,默认情况下应已安装。