欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Shell多参数解析模板

程序员文章站 2022-03-28 16:11:29
shell 多参数模板split_param="false"sql=""is_head=falseis_echo_sql=truefor param in "$@"doif [ "${split_param}" = "false" ] && [ "${param:0:1}" = "-" ];thentype=${param:0:2}if [ ${#param} -gt "2" ];thenvalue=${param:2}elsesplit_p...

shell 多参数模板

split_param="false"
sql=""
is_head=false
is_echo_sql=true
for param in "$@"
do
	if [ "${split_param}" = "false" ] && [ "${param:0:1}" = "-" ];then
		type=${param:0:2}
		if [ ${#param} -gt "2" ];then
			value=${param:2}
		else
			split_param="true"
		fi
	else
		if [ $split_param = "true" ];then
			if [ "${param:0:1}" = "-" ];then
				echo "wrong param"
				exit 1
			else
				value=${param}
				split_param="false"
			fi
		else
			echo "wrong param"
			exit 1
		fi
	fi
	if [ "${type}" = "-N" ];then
		split_param="false"
		value="default"
	fi
	if [ "${type}" = "-S" ];then
		split_param="false"
		value="default"
	fi
	if [ "${split_param}" = "false" ];then
		if [ "${type}"x = "x" ] || [ "${value}"x = "x" ];then
			echo "wrong param"
			exit 1
		fi
	fi
	if [ ! "${type}"x = "x" ] && [ ! "${value}"x = "x" ];then
		case "${type}" in
			"-p")
			case "${value}" in
				"hoff")
				HOST=127.0.0.1
				PORT=3306
				NAME=name0
				PASSWD=pwd0
				DB=database0
				;;
				"oncm")
				HOST=xx.xxx.xx.xxx
				PORT=xxxx
				NAME=xxxxx
				PASSWD=xxxxx
				DB=xxxxx
				;;
				*)
				echo "param -p must in [on|off]"
				exit 1

			esac
			;;
			"-d")
			DB=${value}
			;;
			"-ch")
			CHARSET=${value}
			;;
			"-e")
			sql=${value}
			;;
			"-N")
			is_head=true
			;;
			"-S")
			is_echo_sql=false
			;;
			*)
			echo "wrong param. please use --help"
			exit 1
		esac
	fi
done
if [ "${split_param}" = "true" ];then
	echo "wrong param"
	exit 1
fi

本文地址:https://blog.csdn.net/LaMole_Ryouji/article/details/107582142