2012-09-12 57 views
0
while getopts ":hufc:p:i" opt; do 

    case $opt in 
    h) 
     usage 
     exit 1 
     ;; 
    u) 
     DOUPDATE=false 
     ;; 
    f) 
     DOCONFIRMATION=false 
     ;; 
    c) 
     CUSTOMERTYPE=$OPTARG 
     ;; 
    p) 
     CUSTOMERPROFILE=$OPTARG 
     ;; 
    i) 
     echo "LOL $INSTALL" 
     INSTALL=true 
     ;; 
    \?) 
     echo "Invalid option: -$OPTARG" >&2 
     exit 1 
     ;; 
    :) 
     echo "Option -$OPTARG requires an argument." >&2 
     exit 1 
     ;; 
    esac 
done 

上面是我的bash代码。当我尝试在命令行中使用“i”作为参数时,不会输入案例i。Bash getopts无法识别参数

我该怎么办?

+1

适合我。将这个确切的代码复制到'foo.sh'中,然后运行'sh foo.sh -i'并打印'LOL'。 – Thomas

+1

这应该工作,你用'-i'吗?或者只是'i',就像这样:'./myScript i'?你必须像这样使用'-i':'./myScript -i'。 – pb2q

+0

我不明白,因为在我的终端中同样使用-i或者我不打印“LOOL”。 – Kefka

回答

0

如果你的命令行在我之前有任何无效的选项,它不会解析任何比错误更​​远的选项。如果你的命令行有一个没有后续参数的-c或-p进入CustomerType或CustomerProfile,它不会解析任何更远的参数。

#Examples that should work 
script.sh -i 
script.sh -c customerX -p profileX -i 
script.sh -iz 

#Examples that will not work 
script.sh -hi #-h option will exit before it parses -i 
script.sh -zi #invalid option z will exit 
script.sh -c customerX -p -i #Missing required argument after -p