標籤:
1 #!/bin/bash 2 TEMP=`getopt -o hm: --long aai:,mai: -n ‘getopts.sh‘ -- "[email protected]"` 3 eval set -- "$TEMP" 4 while true 5 do 6 case "$1" in 7 -h|--aai) 8 echo $2 9 shift 2;;10 -m|--mai)11 echo $212 shift 2;;13 --)14 shift15 break;;16 *)17 exit 1;;18 esac19 #case "$2" in20 # -m|--mai)21 # echo ‘mai‘22 # exit;;23 #esac24 25 done26 [[email protected] python_back]# cat getopts.sh 27 #!/bin/bash 28 echo "OPTIND starts at $OPTIND" 29 while getopts ":pq:" optname 30 do 31 case "$optname" in 32 "p") 33 echo "Option $optname is specified" 34 ;; 35 "q") 36 echo "Option $optname has value $OPTARG" 37 ;; 38 "?") 39 echo "Unknown option $OPTARG" 40 ;; 41 ":") 42 echo "No argument value for option $OPTARG" 43 ;; 44 *) 45 # Should not occur 46 echo "Unknown error while processing options" 47 ;; 48 esac 49 echo "OPTIND is now $OPTIND" 50 done
shell getopts and getopt用法