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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。