0
test-fun(){    
    OPTIND=1
    while  getopts  "waz" arg
    do
        case  $arg  in
            w)
                 echo  "ok w"
                ;;
            a)
                echo  "ok a"
                ;;
            z)
                echo  "ok z"
                ;;   
        esac
    done
}

For the above test-fun, test-fun -wz result in

ok w
ok z

test-fun -zw result in

ok z
ok w

I want to specify the arguments' order for test-fun function,all arguments in the order w a z,if you input test-fun zw,an error occur wrong arguments' order for the function,how to fix it?

1 Answers1

0

Put the arguments in a variable with line feeds (vars), sort it and then process the new variable (vars1).

test-fun(){    
OPTIND=1
while  getopts  "waz" arg
do
    vars=$vars"\n"$arg
done
vars1=$(printf $vars | sort)
for i in $vars1

do
    case  $arg  in
        w)
             echo  "ok w"
            ;;
        a)
            echo  "ok a"
            ;;
        z)
            echo  "ok z"
            ;;   
    esac
done
}
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18