linux shell获取参数 getopt

-o表示短选项,两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项。如-carg 而不能是-c arg
--long表示长选项
-n:出错时的信息

# 例子:

    TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.bash' -- "$@"`

# 脚本

function usage(){
echo "usage"
exit 0
}

ARGS=`getopt -a -o I:D:T:e:k:LMSsth -l instence:,database:,table:,excute:,key:,list,master,slave,status,tableview,help -n 'example.bash' -- "$@"`
# check execute result
[ $? -ne 0 ] && usage

# TODO `set` help
eval set -- "${ARGS}"

while true
do
case \"$1\" in
-I|--instence)
instence="$2"
shift
;;
-D|--database)
database="$2"
shift
;;
-T|--table)
table="$2"
shift
;;
-e|--excute)
;;
-k|--key)
key="$2"
shift
;;
-L|--list)
LIST="yes"
;;
-M|--master)
MASTER="yes"
;;
-S|--slave)
SLAVE="yes"
;;
-A|--alldb)
ALLDB="yes"
;;
-s|--status)
STATUS="yes"
;;
-t|--tableview)
TABLEVIEW="yes"
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac

shift
done

echo \"instence:$instence,database=$database,table=$table\"
echo \"execute=$execute,key=$key,list=$LIST\"
echo \"master=$MASTER,slave=$slave,ALLDB=$ALLDB\"
echo \"status=$STATUS,tableview=$TABLEVIEW\"

exit 0



[root@dev1 tmp]# sh t.sh -I ss -T ss -L
instence:ss,database=,table=ss
execute=,key=,list=yes
master=,slave=,ALLDB=
status=,tableview=
[root@dev1 tmp]# sh t.sh -I ss -T ss -L -m
instence:ss,database=,table=ss
execute=,key=,list=yes
master=yes,slave=,ALLDB=
status=,tableview=


请使用浏览器的分享功能分享到微信等