Shell脚本参数解析

Pet*_*ton 18 linux shell parsing arguments

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

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

Tim*_*ost 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     
Run Code Online (Sandbox Code Playgroud)

注意,die是先前定义的功能(未示出).

-n选项告诉getopt报告错误作为我的程序的名称,而不是getopt.-o定义一个短选项列表(:在选项表示需要的参数之后)并--long指定长选项列表(对应于短选项).

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

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

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

eval set -- "$@"
Run Code Online (Sandbox Code Playgroud)

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

./foo --option bar file1.txt file2.txt file3.txt
Run Code Online (Sandbox Code Playgroud)

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

编辑

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