shell getopts and getopt用法

 1 #!/bin/bash
 2 TEMP=`getopt -o hm: --long aai:,mai: -n 'getopts.sh'  -- "$@"`
 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 $2
12     shift 2;;
13     --)
14     shift
15     break;;
16     *)
17     exit 1;;
18 esac
19 #case "$2" in
20 #        -m|--mai)
21 #        echo 'mai'
22 #        exit;;
23 #esac
24 
25 done
26 [root@www 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 

 

posted @ 2015-05-02 16:07  xiaohui163  阅读(477)  评论(0编辑  收藏  举报