Shell脚本参数解析

时间:2010-04-15 04:22:17

标签: linux parsing shell arguments

关于这类事情有很多问题,但我们想象一下,我们的目标是安装了getopt和getopts的通用Linux系统(不是我们会使用它们,但它们看起来很受欢迎)

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

1 个答案:

答案 0 :(得分:33)

您希望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将错误报告为我的程序名称,而不是getopt-o定义了一个短选项列表(:在一个选项表示需要的参数后),--long指定长选项列表(对应于短选项)。

其余只是一个简单的开关,适当地调用shift来推进参数指针。请注意,调用shift; shift;只是一种顽固的习惯。在当前的现代世界中,shift 2可能就足够了。

现代getopt在新平台上非常一致,但是在旧版(大约在Redhat 9之前)系统上可能会遇到一些可移植性问题。有关向后兼容性的信息,请参阅man getopt。但是,你不太可能遇到它的需要。

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

eval set -- "$@"

这将在getopt完成解析选项后将参数指针移动到命令行上剩下的任何内容。然后,您可以shift继续阅读它们。例如,如果命令看起来像这样:

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

一旦完成,不要忘记制作一个方便的-h / --help选项来打印新的精美选项。 :)如果你使输出help2man友好,你有一个即时手册页可以使用你的新工具。

修改

在大多数发行版中,您可以在getopt中找到更多示例/usr/share/doc/util-linux/examples代码,默认情况下应安装该代码。