[转]shell getopts用法

 1 function usage()
 2 {
 3         [[ ! -z $2 ]] && ( echo "$2"; echo ""; )
 4         echo "Usage: $(basename $1) vm_image=PATH [-h|--help]"
 5         echo "-h, --help            Print help and exit"
 6         echo "    --vm_image=PATH   Path to VM image used in these tests"
 7 }
 8 while getopts 'h-:' optchar; do
 9         case "$optchar" in
10                 -)
11                 case "$OPTARG" in
12                         vm_image=*) os_image="${OPTARG#*=}" ;;
13                         *) usage $0 echo "Invalid argument '$OPTARG'" && exit 1 ;;
14                 esac
15                 ;;
16                 h) usage $0 && exit 0 ;;
17                 *) usage $0 "Invalid argument '$optchar'" && exit 1 ;;
18         esac
19 done
原文地址:https://www.cnblogs.com/yi-mu-xi/p/12072693.html