From 8789a3843d81af1e748a61abbf9bb8e2a6800db1 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Fri, 5 Jul 2013 16:57:55 +0800 Subject: [PATCH 01/42] update --- show-busy-java-threads.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/show-busy-java-threads.sh b/show-busy-java-threads.sh index 9071a867..86fc1b4b 100755 --- a/show-busy-java-threads.sh +++ b/show-busy-java-threads.sh @@ -89,7 +89,7 @@ printStackOfThreads() { [ ! -f "${jstackFile}" ] && { jstack ${pid} > ${jstackFile} || { - redEcho "Fail to jstack java process ${pid}" + redEcho "Fail to jstack java process ${pid}!" rm ${jstackFile} continue } @@ -100,7 +100,7 @@ printStackOfThreads() { done } -if [ -z ${pid} ] ; then +if [ -z "${pid}" ] ; then ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk '$4=="java"{print $0}' | sort -k5 -r -n | head --lines "${count}" | printStackOfThreads else From 36d1ede43362fb2381d335b18f35832d619be456 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 16:24:14 +0800 Subject: [PATCH 02/42] add -n on getopt --- show-busy-java-threads.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/show-busy-java-threads.sh b/show-busy-java-threads.sh index 86fc1b4b..8a086fcb 100755 --- a/show-busy-java-threads.sh +++ b/show-busy-java-threads.sh @@ -24,7 +24,7 @@ EOF exit $1 } -ARGS=`getopt -a -o c:p:h -l count:,pid:,help -- "$@"` +ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"` [ $? -ne 0 ] && usage 1 eval set -- "${ARGS}" From 428ba98be1afe7c4a557e8606602e6df6ade0b65 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 16:27:16 +0800 Subject: [PATCH 03/42] add parse.sh --- parse.sh | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 parse.sh diff --git a/parse.sh b/parse.sh new file mode 100644 index 00000000..843f0d3a --- /dev/null +++ b/parse.sh @@ -0,0 +1,197 @@ +#!/bin/bash + +PROG=`basename $0` + +################################################# +# Utils Methods +################################################# + +colorEcho() { + color=$1 + shift + if [ -c /dev/stdout ] ; then + # if stdout is console, turn on color output. + echo -ne "\033[1;${color}m" + echo -n "$@" + echo -e "\033[0m" + else + echo "$@" + fi +} + +redEcho() { + colorEcho 31 "$@" +} + +greenEcho() { + colorEcho 32 "$@" +} + +blueEcho() { + colorEcho 34 "$@" +} + +echoCmdLineThenTimedRun() { + echo =============================================================================== + echo "run command below: " + echo "$@" + echo =============================================================================== + local start=`date +%s` + "$@" + local exitCode=$? + + echo + echo "Execute time: $((`date +%s` - $start))s" + + return $exitCode +} + +# cut head whitespace, delete \r +cutHeadspaceAndCR() { + [ $# -ne 1 ] && { + redEcho "NOT 1 arguemnt when call cutHeadspaceAndCR: $@" + return 1 + } + echo "$1" | tr -d '\r' | sed -r 's/^\s*//' +} + +checkEmptyOrComments() { + [ $# -ne 1 ] && { + redEcho "NOT 1 arguemnt when call checkEmptyOrComments: $@" + return 1 + } + echo "$1" | grep -Eq '^\s*#|^\s*$' +} + +extractValue() { + [ $# -ne 2 ] && { + redEcho "NOT 2 arguemnts when call extractValue: $@" + return 1 + } + + local settingFile=$1 + local key=$2 + grep "$key" -F "$settingFile" | tr -d '\r' | grep -vE '^\s*#' | + awk -F= '{print $2}' | sed -r 's/^\s*//;s/\s*$//' +} + +################################################# +# Parse Methods +################################################# + +parseOpts() { + local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+ + shift + + local OPTS_TUPLES=() + echo "$optsDescription" | + # cut head and tail space + sed -r 's/^\s+//;s/\s+$//' | + awk -F '[\t ]*\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}' | + while read optDesc ; do # optDesc LIKE b,b-long: + local mode="${optDesc:(-1)}" # LIKE : or + + case "$mode" in + +|:) + optDesc="${optDesc:0:(${#optDesc}-1)}" # LIKE b,b-long + ;; + *) + mode="-" + ;; + esac + echo "XXX mode=$mode optDesc=$optDesc" + + local optsLine=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long + + [ $(echo "$optsLine" | wc -l) -gt 2 ] && { + redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 + return 223 + } + + local optTuple=() + echo "$optsLine" | while read opt ; do # opt LIKE a , a-long + [ -z "$opt" ] && continue + + [ ${#opt} -eq 1 ] && { + redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { + echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 + return 223 + } + } || { + echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { + redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 + return 223 + } + } + optTuple=(${optTuple[@]} opt) + done + + echo "XXX $optDesc ${optTuple[@]}" + [ ${#optTuple[@]} -gt 2 ] && { + redEcho "more than 2 opt($optDesc) in option description!" 1>&2 + return 223 + } + + local optTupleNames= + for o in "${optTuple[@]}"; do + optTupleNames+="$o" + done + + eval "optTupleNames=($mode ${optTuple[@]})" + OPTS_TUPLES=("${OPTS_TUPLES[@]}", optTupleNames) + done + + echo "${OPTS_TUPLES[@]}" + for o in "${OPTS_TUPLES[@]}" ; do + eval "echo \"[\${$o[@]}]\"" + done + + # local args=() + + # while true; do + # case "$1" in + # ---*) + # echo "Illegal option($1), more than 2 prefix -!" 1>&2 + # return 221 + # --) + # shift + # args=("${args[@]}" "$@") + # break + # ;; + # --*) + # local opt=${1#--} + # local value=$2 + # shift 2 + # ;; + # -*) + # local opt=${1#-} + # local value=$2 + # shift 2 + # ;; + # *) + # args=("${args[@]}" "$1") + # shift + # break + # ;; + # esac + # done +} + +################################################# +# Main Methods +################################################# + +parseOpts "a,a-long|b,b-long:|c,c-long+" "$@" + +# echo "a: " +# echo "$_OPT_a" +# echo "$_OPT_a_long" + +# echo "b: " +# echo "$_OPT_b" +# echo "$_OPT_b" + +# echo "c:" +# echo "$_OPT_c" +# echo "$_OPT_c_long" + +# echo "${_ARGS_[@]}" From 6e9e4b61df5b8d4831679007ca7acade79c89e88 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 19:29:19 +0800 Subject: [PATCH 04/42] finish to parse option description --- parse.sh | 179 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 134 insertions(+), 45 deletions(-) diff --git a/parse.sh b/parse.sh index 843f0d3a..6348e142 100644 --- a/parse.sh +++ b/parse.sh @@ -75,19 +75,52 @@ extractValue() { awk -F= '{print $2}' | sed -r 's/^\s*//;s/\s*$//' } +indirectReferredArray() { + redEcho "$1" + local arrayName="$1" + local arrayNamePlaceHolder="$arrayName[@]" + local ret=( "${!arrayNamePlaceHolder}" ) + return ret +} + ################################################# # Parse Methods ################################################# +setOptBool() { + return +} + +setOptValue() { + return +} + +setOptArray() { + return +} + +showOptDescInfoList() { + echo "====================================================" + echo "show option desc info list:" + for idx in "${_OPT_INFO_LIST_INDEX[@]}"; do + local arrayPlaceHolder="$idx[@]" + echo "$idx = ${!arrayPlaceHolder}" + done + echo "====================================================" +} + parseOpts() { local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+ shift - local OPTS_TUPLES=() - echo "$optsDescription" | - # cut head and tail space - sed -r 's/^\s+//;s/\s+$//' | - awk -F '[\t ]*\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}' | + _OPT_INFO_LIST_INDEX=() + + local optDescLines=`echo "$optsDescription" | + # cut head and tail space + sed -r 's/^\s+//;s/\s+$//' | + awk -F '[\t ]*\\\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` + blueEcho "xxx optDescLines=$optDescLines" + while read optDesc ; do # optDesc LIKE b,b-long: local mode="${optDesc:(-1)}" # LIKE : or + case "$mode" in @@ -98,7 +131,7 @@ parseOpts() { mode="-" ;; esac - echo "XXX mode=$mode optDesc=$optDesc" + blueEcho "XXX splite mode: mode=$mode optDesc=$optDesc" local optsLine=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long @@ -108,7 +141,7 @@ parseOpts() { } local optTuple=() - echo "$optsLine" | while read opt ; do # opt LIKE a , a-long + while read opt ; do # opt LIKE a , a-long [ -z "$opt" ] && continue [ ${#opt} -eq 1 ] && { @@ -122,58 +155,114 @@ parseOpts() { return 223 } } - optTuple=(${optTuple[@]} opt) - done + optTuple=("${optTuple[@]}" "$opt") + blueEcho "optTuple: ${optTuple[@]}" + done < <(echo "$optsLine") - echo "XXX $optDesc ${optTuple[@]}" + blueEcho "XXX find out optTuple: optDesc=$optDesc optTuple=${optTuple[@]}" [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 return 223 } local optTupleNames= + local evalOpts= for o in "${optTuple[@]}"; do - optTupleNames+="$o" + optTupleNames+=_`echo "$o" | sed 's/-/_/g'` + evalOpts+=" $o" done + blueEcho "XXX optTupleNames=$optTupleNames" + + eval "$optTupleNames=($mode $evalOpts)" + blueEcho "XXX eval $optTupleNames=($mode $evalOpts)" + + local arrayNamePlaceHolder="$optTupleNames[@]" + blueEcho "XXX optTupleNames Array=$optTupleNames ! ${!arrayNamePlaceHolder} " - eval "optTupleNames=($mode ${optTuple[@]})" - OPTS_TUPLES=("${OPTS_TUPLES[@]}", optTupleNames) + _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$optTupleNames") + blueEcho "XXX _OPT_INFO_LIST_INDEX=${_OPT_INFO_LIST_INDEX[@]}" + done < <(echo "$optDescLines") + + showOptDescInfoList + + local args=() + + while true; do + case "$1" in + ---*) + echo "Illegal option($1), more than 2 prefix -!" 1>&2 + return 221 + ;; + --) + shift + args=("${args[@]}" "$@") + break + ;; + --*) + local opt=${1#--} + local mode=findOptMode "$opt" + case "$mode" in + :) + setOptValue "$opt" "$2" + shift 2 + ;; + +) + setOptArray "$opt" + ;; + -) + setOptBool "$opt" + ;; + *) + redEcho "Undefined option $opt!" + return 225 + ;; + esac + ;; + -*) + local opt=${1#-} + local mode=findOptMode "$opt" + case "$mode" in + :) + setOptValue "$opt" "$2" + shift 2 + ;; + +) + setOptArray "$opt" + ;; + -) + setOptBool "$opt" + ;; + *) + redEcho "Undefined option $opt!" + return 225 + ;; + esac + ;; + *) + args=("${args[@]}" "$1") + shift + ;; + esac done +} - echo "${OPTS_TUPLES[@]}" - for o in "${OPTS_TUPLES[@]}" ; do - eval "echo \"[\${$o[@]}]\"" +findOptMode() { + opt="$1" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do + local ele0PlaceHolder="$idxName[0]" + + local arrayLenPlaceHolder="$idxName[#]" + local len=${!arrayLenPlaceHolder} + + for (( i = 1; i < $len; i++)); do + local arrayElePlaceHolder="$idxName[$i]" + [ "$opt" = "${!arrayElePlaceHolder}" ] && { + return "${!ele0PlaceHolder}" + } + done done - # local args=() - - # while true; do - # case "$1" in - # ---*) - # echo "Illegal option($1), more than 2 prefix -!" 1>&2 - # return 221 - # --) - # shift - # args=("${args[@]}" "$@") - # break - # ;; - # --*) - # local opt=${1#--} - # local value=$2 - # shift 2 - # ;; - # -*) - # local opt=${1#-} - # local value=$2 - # shift 2 - # ;; - # *) - # args=("${args[@]}" "$1") - # shift - # break - # ;; - # esac - # done + return "" } ################################################# From 1434bddc5eb7fcf0f84a50a3e19ce995a4c35ff0 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 19:51:02 +0800 Subject: [PATCH 05/42] add exec mode for parse.sh --- parse.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 parse.sh diff --git a/parse.sh b/parse.sh old mode 100644 new mode 100755 From b7091432c4ac8f32c8c1640796cec5a2b97db7ef Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 19:54:53 +0800 Subject: [PATCH 06/42] fix index referred array len problem --- parse.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/parse.sh b/parse.sh index 6348e142..6fe1b7b3 100755 --- a/parse.sh +++ b/parse.sh @@ -200,7 +200,8 @@ parseOpts() { ;; --*) local opt=${1#--} - local mode=findOptMode "$opt" + local mode=`findOptMode "$opt"` + redEcho "mode of $1 = $mode" case "$mode" in :) setOptValue "$opt" "$2" @@ -220,7 +221,8 @@ parseOpts() { ;; -*) local opt=${1#-} - local mode=findOptMode "$opt" + local mode=`findOptMode "$opt"` + redEcho "mode of $1 = $mode" case "$mode" in :) setOptValue "$opt" "$2" @@ -251,18 +253,18 @@ findOptMode() { for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local ele0PlaceHolder="$idxName[0]" - local arrayLenPlaceHolder="$idxName[#]" - local len=${!arrayLenPlaceHolder} + local arrayPlaceHolder="$idxName[@]" + local tmpArray=( "${!arrayPlaceHolder}" ) - for (( i = 1; i < $len; i++)); do + for (( i = 1; i < ${#tmpArray[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" [ "$opt" = "${!arrayElePlaceHolder}" ] && { - return "${!ele0PlaceHolder}" + echo "${!ele0PlaceHolder}" } done done - return "" + echo "" } ################################################# From da360fe5c7538059a4516ac6f41cc2a5769d1736 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 14 Jul 2013 20:56:43 +0800 Subject: [PATCH 07/42] add logic of set _OPT_VALUE_ --- parse.sh | 128 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 100 insertions(+), 28 deletions(-) diff --git a/parse.sh b/parse.sh index 6fe1b7b3..759a8ede 100755 --- a/parse.sh +++ b/parse.sh @@ -87,16 +87,79 @@ indirectReferredArray() { # Parse Methods ################################################# +findOptMode() { + opt="$1" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do + local ele0PlaceHolder="$idxName[0]" + + local arrayPlaceHolder="$idxName[@]" + local tmpArray=( "${!arrayPlaceHolder}" ) + + for (( i = 1; i < ${#tmpArray[@]}; i++)); do + local arrayElePlaceHolder="$idxName[$i]" + [ "$opt" = "${!arrayElePlaceHolder}" ] && { + echo "${!ele0PlaceHolder}" + } + done + done + + echo "" +} + setOptBool() { - return + setOptValue "$@" } setOptValue() { - return + local opt="$1" + local value="$2" + + opt="$1" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do + local ele0PlaceHolder="$idxName[0]" + + local arrayPlaceHolder="$idxName[@]" + local tmpArray=( "${!arrayPlaceHolder}" ) + + for (( i = 1; i < ${#tmpArray[@]}; i++)); do + local arrayElePlaceHolder="$idxName[$i]" + [ "$opt" = "${!arrayElePlaceHolder}" ] && { + for (( j = 1; j < ${#tmpArray[@]}; j++)); do + local name="`echo ${tmpArray[$j]} | sed 's/-/_/g'`" + eval "_OPT_VALUE_$name=\"\$value\"" + done + } + done + done + + redEcho "NOT Fount option $opt!" + return 1 } setOptArray() { - return + local opt="$1" + shift + + opt="$1" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do + local ele0PlaceHolder="$idxName[0]" + + local arrayPlaceHolder="$idxName[@]" + local tmpArray=( "${!arrayPlaceHolder}" ) + + for (( i = 1; i < ${#tmpArray[@]}; i++)); do + local arrayElePlaceHolder="$idxName[$i]" + [ "$opt" = "${!arrayElePlaceHolder}" ] && { + for (( j = 1; j < ${#tmpArray[@]}; j++)); do + local name="`echo ${tmpArray[$j]} | sed 's/-/_/g'`" + eval "_OPT_VALUE_$name=\"(\$@)\"" + done + } + done + done + + redEcho "NOT Fount option $opt!" + return 1 } showOptDescInfoList() { @@ -203,16 +266,30 @@ parseOpts() { local mode=`findOptMode "$opt"` redEcho "mode of $1 = $mode" case "$mode" in + -) + setOptBool "$opt" "true" + shift + ;; :) setOptValue "$opt" "$2" shift 2 ;; +) + local valueArray=() + local foundComma="" + for value in "$@" ; do + [ ";" -eq "$value"] && { + foundComma=true + break + } || valueArray=("${valueArray[@]}" "$value") + done + [ -z ] && { + redEcho "value of option $opt no end comma, value = ${valueArray[@]}" + return 228 + } + shift "$((${#valueArray[@]} + 2))" setOptArray "$opt" ;; - -) - setOptBool "$opt" - ;; *) redEcho "Undefined option $opt!" return 225 @@ -224,16 +301,30 @@ parseOpts() { local mode=`findOptMode "$opt"` redEcho "mode of $1 = $mode" case "$mode" in + -) + setOptBool "$opt" "true" + shift + ;; :) setOptValue "$opt" "$2" shift 2 ;; +) + local valueArray=() + local foundComma="" + for value in "$@" ; do + [ ";" -eq "$value"] && { + foundComma=true + break + } || valueArray=("${valueArray[@]}" "$value") + done + [ -z ] && { + redEcho "value of option $opt no end comma, value = ${valueArray[@]}" + return 228 + } + shift "$((${#valueArray[@]} + 2))" setOptArray "$opt" ;; - -) - setOptBool "$opt" - ;; *) redEcho "Undefined option $opt!" return 225 @@ -248,25 +339,6 @@ parseOpts() { done } -findOptMode() { - opt="$1" - for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local ele0PlaceHolder="$idxName[0]" - - local arrayPlaceHolder="$idxName[@]" - local tmpArray=( "${!arrayPlaceHolder}" ) - - for (( i = 1; i < ${#tmpArray[@]}; i++)); do - local arrayElePlaceHolder="$idxName[$i]" - [ "$opt" = "${!arrayElePlaceHolder}" ] && { - echo "${!ele0PlaceHolder}" - } - done - done - - echo "" -} - ################################################# # Main Methods ################################################# From ee23e8f0f4068c7bba6f69cc608e8ba4a088e149 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 10:03:28 +0800 Subject: [PATCH 08/42] base function is ok; reformat code --- parse.sh | 188 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 102 insertions(+), 86 deletions(-) diff --git a/parse.sh b/parse.sh index 759a8ede..a60182ee 100755 --- a/parse.sh +++ b/parse.sh @@ -78,11 +78,16 @@ extractValue() { indirectReferredArray() { redEcho "$1" local arrayName="$1" - local arrayNamePlaceHolder="$arrayName[@]" - local ret=( "${!arrayNamePlaceHolder}" ) + local arrayPlaceHolder="$arrayName[@]" + local ret=( "${!arrayPlaceHolder}" ) return ret } +convertToVarName() { + local from="$1" + echo "$from" | sed 's/-/_/g' +} + ################################################# # Parse Methods ################################################# @@ -91,14 +96,16 @@ findOptMode() { opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local ele0PlaceHolder="$idxName[0]" + local mode="${!ele0PlaceHolder}" local arrayPlaceHolder="$idxName[@]" - local tmpArray=( "${!arrayPlaceHolder}" ) - - for (( i = 1; i < ${#tmpArray[@]}; i++)); do + local optInfo=("${!arrayPlaceHolder}") + + for ((i = 1; i < ${#optInfo[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" [ "$opt" = "${!arrayElePlaceHolder}" ] && { - echo "${!ele0PlaceHolder}" + echo "$mode" + return } done done @@ -114,25 +121,26 @@ setOptValue() { local opt="$1" local value="$2" - opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local ele0PlaceHolder="$idxName[0]" - local arrayPlaceHolder="$idxName[@]" - local tmpArray=( "${!arrayPlaceHolder}" ) + local optInfo=("${!arrayPlaceHolder}") - for (( i = 1; i < ${#tmpArray[@]}; i++)); do + for ((i = 1; i < ${#optInfo[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" - [ "$opt" = "${!arrayElePlaceHolder}" ] && { - for (( j = 1; j < ${#tmpArray[@]}; j++)); do - local name="`echo ${tmpArray[$j]} | sed 's/-/_/g'`" - eval "_OPT_VALUE_$name=\"\$value\"" + local optName="${!arrayElePlaceHolder}" + [ "$opt" = "$optName" ] && { + # set _OPT_VALUE + for (( j = 1; j < ${#optInfo[@]}; j++)); do + local name=`convertToVarName "${optInfo[$j]}"` + local from='"$value"' + eval "_OPT_VALUE_$name=$from" done + return } done done - redEcho "NOT Fount option $opt!" + redEcho "NOT Found option $opt!" return 1 } @@ -140,20 +148,21 @@ setOptArray() { local opt="$1" shift - opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local ele0PlaceHolder="$idxName[0]" - local arrayPlaceHolder="$idxName[@]" - local tmpArray=( "${!arrayPlaceHolder}" ) + local optInfo=("${!arrayPlaceHolder}") - for (( i = 1; i < ${#tmpArray[@]}; i++)); do + for ((i = 1; i < ${#optInfo[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" - [ "$opt" = "${!arrayElePlaceHolder}" ] && { - for (( j = 1; j < ${#tmpArray[@]}; j++)); do - local name="`echo ${tmpArray[$j]} | sed 's/-/_/g'`" - eval "_OPT_VALUE_$name=\"(\$@)\"" + local optName="${!arrayElePlaceHolder}" + [ "$opt" = "$optName" ] && { + # set _OPT_VALUE + for (( j = 1; j < ${#optInfo[@]}; j++)); do + local name=`convertToVarName "${optInfo[$j]}"` + local from='"$@"' + eval "_OPT_VALUE_$name=($from)" done + return } done done @@ -165,10 +174,42 @@ setOptArray() { showOptDescInfoList() { echo "====================================================" echo "show option desc info list:" - for idx in "${_OPT_INFO_LIST_INDEX[@]}"; do - local arrayPlaceHolder="$idx[@]" - echo "$idx = ${!arrayPlaceHolder}" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do + local arrayPlaceHolder="$idxName[@]" + echo "$idxName = ${!arrayPlaceHolder}" + done + echo "====================================================" +} + +showOptValueInfoList() { + echo "====================================================" + echo "show option value info list:" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do + local ele0PlaceHolder="$idxName[0]" + local mode="${!ele0PlaceHolder}" + + local arrayPlaceHolder="$idxName[@]" + local optInfo=("${!arrayPlaceHolder}") + for ((i = 1; i < ${#optInfo[@]}; i++)); do + local arrayElePlaceHolder="$idxName[$i]" + local optName="${!arrayElePlaceHolder}" + local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" + + case "$mode" in + -) + echo "$optValueVarName=${!optValueVarName}" + ;; + :) + echo "$optValueVarName=${!optValueVarName}" + ;; + +) + local arrPlaceHolder="$optValueVarName[@]" + echo "$optValueVarName=(${!arrPlaceHolder})" + ;; + esac + done done + echo "_OPT_ARGS=(${_OPT_ARGS[@]})" echo "====================================================" } @@ -182,23 +223,25 @@ parseOpts() { # cut head and tail space sed -r 's/^\s+//;s/\s+$//' | awk -F '[\t ]*\\\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` - blueEcho "xxx optDescLines=$optDescLines" + blueEcho "XXX optDescLines=$optDescLines" while read optDesc ; do # optDesc LIKE b,b-long: + [ -z "$optDesc" ] && continue + local mode="${optDesc:(-1)}" # LIKE : or + case "$mode" in - +|:) + +|:|-) optDesc="${optDesc:0:(${#optDesc}-1)}" # LIKE b,b-long ;; *) mode="-" ;; esac - blueEcho "XXX splite mode: mode=$mode optDesc=$optDesc" + blueEcho "XXX splite mode and optDesc: mode=$mode optDesc=$optDesc" - local optsLine=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long + local optLines=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long - [ $(echo "$optsLine" | wc -l) -gt 2 ] && { + [ $(echo "$optLines" | wc -l) -gt 2 ] && { redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 return 223 } @@ -219,38 +262,40 @@ parseOpts() { } } optTuple=("${optTuple[@]}" "$opt") - blueEcho "optTuple: ${optTuple[@]}" - done < <(echo "$optsLine") + done < <(echo "$optLines") + blueEcho "XXX find out optTuple: optDesc=$optDesc optTuple=(${optTuple[@]})" - blueEcho "XXX find out optTuple: optDesc=$optDesc optTuple=${optTuple[@]}" [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 return 223 } - local optTupleNames= + local optInfo= local evalOpts= for o in "${optTuple[@]}"; do - optTupleNames+=_`echo "$o" | sed 's/-/_/g'` + optInfo+="_`convertToVarName "$o"`" evalOpts+=" $o" done - blueEcho "XXX optTupleNames=$optTupleNames" + blueEcho "XXX optInfo=$optInfo" - eval "$optTupleNames=($mode $evalOpts)" - blueEcho "XXX eval $optTupleNames=($mode $evalOpts)" + eval "$optInfo=($mode $evalOpts)" + blueEcho "XXX eval $optInfo=($mode $evalOpts)" - local arrayNamePlaceHolder="$optTupleNames[@]" - blueEcho "XXX optTupleNames Array=$optTupleNames ! ${!arrayNamePlaceHolder} " + local arrayPlaceHolder="$optInfo[@]" + blueEcho "XXX optInfo Array $optInfo = (${!arrayPlaceHolder})" - _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$optTupleNames") - blueEcho "XXX _OPT_INFO_LIST_INDEX=${_OPT_INFO_LIST_INDEX[@]}" + _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$optInfo") + blueEcho "XXX _OPT_INFO_LIST_INDEX=(${_OPT_INFO_LIST_INDEX[@]})" done < <(echo "$optDescLines") showOptDescInfoList - local args=() + redEcho "YYY start parse!" + local args=() while true; do + [ $# -eq 0 ] && break + case "$1" in ---*) echo "Illegal option($1), more than 2 prefix -!" 1>&2 @@ -261,43 +306,8 @@ parseOpts() { args=("${args[@]}" "$@") break ;; - --*) - local opt=${1#--} - local mode=`findOptMode "$opt"` - redEcho "mode of $1 = $mode" - case "$mode" in - -) - setOptBool "$opt" "true" - shift - ;; - :) - setOptValue "$opt" "$2" - shift 2 - ;; - +) - local valueArray=() - local foundComma="" - for value in "$@" ; do - [ ";" -eq "$value"] && { - foundComma=true - break - } || valueArray=("${valueArray[@]}" "$value") - done - [ -z ] && { - redEcho "value of option $opt no end comma, value = ${valueArray[@]}" - return 228 - } - shift "$((${#valueArray[@]} + 2))" - setOptArray "$opt" - ;; - *) - redEcho "Undefined option $opt!" - return 225 - ;; - esac - ;; -*) - local opt=${1#-} + local opt=`echo "$1" | sed -r 's/^--?//'` local mode=`findOptMode "$opt"` redEcho "mode of $1 = $mode" case "$mode" in @@ -310,20 +320,23 @@ parseOpts() { shift 2 ;; +) + shift local valueArray=() local foundComma="" for value in "$@" ; do - [ ";" -eq "$value"] && { + [ ";" = "$value" ] && { foundComma=true break } || valueArray=("${valueArray[@]}" "$value") done - [ -z ] && { + [ "$foundComma" ] || { redEcho "value of option $opt no end comma, value = ${valueArray[@]}" return 228 } - shift "$((${#valueArray[@]} + 2))" - setOptArray "$opt" + redEcho shift "$((${#valueArray[@]} + 2))" + shift "$((${#valueArray[@]} + 1))" + redEcho setOptArray "$opt" "${valueArray[@]}" + setOptArray "$opt" "${valueArray[@]}" ;; *) redEcho "Undefined option $opt!" @@ -332,10 +345,12 @@ parseOpts() { esac ;; *) + redEcho "========= $1" args=("${args[@]}" "$1") shift ;; esac + _OPT_ARGS=("$args") done } @@ -344,6 +359,7 @@ parseOpts() { ################################################# parseOpts "a,a-long|b,b-long:|c,c-long+" "$@" +showOptValueInfoList # echo "a: " # echo "$_OPT_a" From 5de4d664b1b81f197f807afcd3447f351dcfc70f Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 10:51:45 +0800 Subject: [PATCH 09/42] add comment --- parse.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/parse.sh b/parse.sh index a60182ee..ea23604d 100755 --- a/parse.sh +++ b/parse.sh @@ -90,10 +90,15 @@ convertToVarName() { ################################################# # Parse Methods +# Use Globle Variable: +# * _OPT_INFO_LIST_INDEX : Option info, data structure. +# _OPT_INFO_LIST_INDEX ->* _a_a_long -> option value. +# * _OPT_VALUE_* : value of option. is Array type for + mode option +# * _OPT_ARGS : option arguments ################################################# findOptMode() { - opt="$1" + local opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local ele0PlaceHolder="$idxName[0]" local mode="${!ele0PlaceHolder}" From f72b5816cb32dc103415b6eb58ae45f93a2579df Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 13:44:15 +0800 Subject: [PATCH 10/42] fix no value option error; fix wrong _OPT_ARGS --- parse.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/parse.sh b/parse.sh index ea23604d..19df5f23 100755 --- a/parse.sh +++ b/parse.sh @@ -90,6 +90,7 @@ convertToVarName() { ################################################# # Parse Methods +# # Use Globle Variable: # * _OPT_INFO_LIST_INDEX : Option info, data structure. # _OPT_INFO_LIST_INDEX ->* _a_a_long -> option value. @@ -311,7 +312,7 @@ parseOpts() { args=("${args[@]}" "$@") break ;; - -*) + -*) # short & long option(-a, -a-long), use same read-in logic. local opt=`echo "$1" | sed -r 's/^--?//'` local mode=`findOptMode "$opt"` redEcho "mode of $1 = $mode" @@ -321,6 +322,10 @@ parseOpts() { shift ;; :) + [ $# -lt 2 ] && { + redEcho "Option $opt has NO value!" + return 228 + } setOptValue "$opt" "$2" shift 2 ;; @@ -350,13 +355,12 @@ parseOpts() { esac ;; *) - redEcho "========= $1" args=("${args[@]}" "$1") shift ;; esac - _OPT_ARGS=("$args") done + _OPT_ARGS=("${args[@]}") } ################################################# From 03690418dcabcfd78231890ec0a8a70a3acd737f Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 15:01:22 +0800 Subject: [PATCH 11/42] refactor var name; rm useless func indirectReferredArray --- parse.sh | 83 +++++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 52 deletions(-) diff --git a/parse.sh b/parse.sh index 19df5f23..4daf00ae 100755 --- a/parse.sh +++ b/parse.sh @@ -75,14 +75,6 @@ extractValue() { awk -F= '{print $2}' | sed -r 's/^\s*//;s/\s*$//' } -indirectReferredArray() { - redEcho "$1" - local arrayName="$1" - local arrayPlaceHolder="$arrayName[@]" - local ret=( "${!arrayPlaceHolder}" ) - return ret -} - convertToVarName() { local from="$1" echo "$from" | sed 's/-/_/g' @@ -104,10 +96,10 @@ findOptMode() { local ele0PlaceHolder="$idxName[0]" local mode="${!ele0PlaceHolder}" - local arrayPlaceHolder="$idxName[@]" - local optInfo=("${!arrayPlaceHolder}") + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#optInfo[@]}; i++)); do + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" [ "$opt" = "${!arrayElePlaceHolder}" ] && { echo "$mode" @@ -128,16 +120,16 @@ setOptValue() { local value="$2" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local arrayPlaceHolder="$idxName[@]" - local optInfo=("${!arrayPlaceHolder}") + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#optInfo[@]}; i++)); do + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" local optName="${!arrayElePlaceHolder}" [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for (( j = 1; j < ${#optInfo[@]}; j++)); do - local name=`convertToVarName "${optInfo[$j]}"` + for (( j = 1; j < ${#idxNameArray[@]}; j++)); do + local name=`convertToVarName "${idxNameArray[$j]}"` local from='"$value"' eval "_OPT_VALUE_$name=$from" done @@ -155,16 +147,16 @@ setOptArray() { shift for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local arrayPlaceHolder="$idxName[@]" - local optInfo=("${!arrayPlaceHolder}") + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#optInfo[@]}; i++)); do + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" local optName="${!arrayElePlaceHolder}" [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for (( j = 1; j < ${#optInfo[@]}; j++)); do - local name=`convertToVarName "${optInfo[$j]}"` + for (( j = 1; j < ${#idxNameArray[@]}; j++)); do + local name=`convertToVarName "${idxNameArray[$j]}"` local from='"$@"' eval "_OPT_VALUE_$name=($from)" done @@ -173,7 +165,7 @@ setOptArray() { done done - redEcho "NOT Fount option $opt!" + redEcho "NOT Found option $opt!" return 1 } @@ -181,8 +173,8 @@ showOptDescInfoList() { echo "====================================================" echo "show option desc info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do - local arrayPlaceHolder="$idxName[@]" - echo "$idxName = ${!arrayPlaceHolder}" + local idxNameArrayPlaceHolder="$idxName[@]" + echo "$idxName = ${!idxNameArrayPlaceHolder}" done echo "====================================================" } @@ -194,9 +186,10 @@ showOptValueInfoList() { local ele0PlaceHolder="$idxName[0]" local mode="${!ele0PlaceHolder}" - local arrayPlaceHolder="$idxName[@]" - local optInfo=("${!arrayPlaceHolder}") - for ((i = 1; i < ${#optInfo[@]}; i++)); do + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") + + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do local arrayElePlaceHolder="$idxName[$i]" local optName="${!arrayElePlaceHolder}" local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" @@ -209,8 +202,8 @@ showOptValueInfoList() { echo "$optValueVarName=${!optValueVarName}" ;; +) - local arrPlaceHolder="$optValueVarName[@]" - echo "$optValueVarName=(${!arrPlaceHolder})" + local OptInfoArrayPlaceHolder="$optValueVarName[@]" + echo "$optValueVarName=(${!OptInfoArrayPlaceHolder})" ;; esac done @@ -276,21 +269,21 @@ parseOpts() { return 223 } - local optInfo= + local idxName= local evalOpts= for o in "${optTuple[@]}"; do - optInfo+="_`convertToVarName "$o"`" + idxName+="_`convertToVarName "$o"`" evalOpts+=" $o" done - blueEcho "XXX optInfo=$optInfo" + blueEcho "XXX idxName=$idxName" - eval "$optInfo=($mode $evalOpts)" - blueEcho "XXX eval $optInfo=($mode $evalOpts)" + eval "$idxName=($mode $evalOpts)" + blueEcho "XXX eval $idxName=($mode $evalOpts)" - local arrayPlaceHolder="$optInfo[@]" - blueEcho "XXX optInfo Array $optInfo = (${!arrayPlaceHolder})" + local idxNameArrayPlaceHolder="$idxName[@]" + blueEcho "XXX idxName Array $idxName = (${!idxNameArrayPlaceHolder})" - _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$optInfo") + _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$idxName") blueEcho "XXX _OPT_INFO_LIST_INDEX=(${_OPT_INFO_LIST_INDEX[@]})" done < <(echo "$optDescLines") @@ -367,19 +360,5 @@ parseOpts() { # Main Methods ################################################# -parseOpts "a,a-long|b,b-long:|c,c-long+" "$@" +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; bb2 -- cc dd showOptValueInfoList - -# echo "a: " -# echo "$_OPT_a" -# echo "$_OPT_a_long" - -# echo "b: " -# echo "$_OPT_b" -# echo "$_OPT_b" - -# echo "c:" -# echo "$_OPT_c" -# echo "$_OPT_c_long" - -# echo "${_ARGS_[@]}" From 5610134abc1f9c3b53c65d8817297589bca182a3 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 15:10:20 +0800 Subject: [PATCH 12/42] clean return code of parseOpts --- parse.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/parse.sh b/parse.sh index 4daf00ae..4f65fe44 100755 --- a/parse.sh +++ b/parse.sh @@ -242,7 +242,7 @@ parseOpts() { [ $(echo "$optLines" | wc -l) -gt 2 ] && { redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 - return 223 + return 220 } local optTuple=() @@ -252,12 +252,12 @@ parseOpts() { [ ${#opt} -eq 1 ] && { redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 - return 223 + return 221 } } || { echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 - return 223 + return 221 } } optTuple=("${optTuple[@]}" "$opt") @@ -266,7 +266,7 @@ parseOpts() { [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 - return 223 + return 222 } local idxName= @@ -298,7 +298,7 @@ parseOpts() { case "$1" in ---*) echo "Illegal option($1), more than 2 prefix -!" 1>&2 - return 221 + return 230 ;; --) shift @@ -317,7 +317,7 @@ parseOpts() { :) [ $# -lt 2 ] && { redEcho "Option $opt has NO value!" - return 228 + return 231 } setOptValue "$opt" "$2" shift 2 @@ -334,7 +334,7 @@ parseOpts() { done [ "$foundComma" ] || { redEcho "value of option $opt no end comma, value = ${valueArray[@]}" - return 228 + return 231 } redEcho shift "$((${#valueArray[@]} + 2))" shift "$((${#valueArray[@]} + 1))" @@ -343,7 +343,7 @@ parseOpts() { ;; *) redEcho "Undefined option $opt!" - return 225 + return 232 ;; esac ;; From a45bcd6b1348f2728872de5fc4edf5c4a7a64903 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 15 Jul 2013 22:18:49 +0800 Subject: [PATCH 13/42] improve output --- parse.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/parse.sh b/parse.sh index 4f65fe44..be7c2e36 100755 --- a/parse.sh +++ b/parse.sh @@ -32,10 +32,10 @@ blueEcho() { } echoCmdLineThenTimedRun() { - echo =============================================================================== + echo "===============================================================================" echo "run command below: " echo "$@" - echo =============================================================================== + echo "===============================================================================" local start=`date +%s` "$@" local exitCode=$? @@ -170,17 +170,17 @@ setOptArray() { } showOptDescInfoList() { - echo "====================================================" + echo "===============================================================================" echo "show option desc info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" echo "$idxName = ${!idxNameArrayPlaceHolder}" done - echo "====================================================" + echo "===============================================================================" } showOptValueInfoList() { - echo "====================================================" + echo "===============================================================================" echo "show option value info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local ele0PlaceHolder="$idxName[0]" @@ -209,7 +209,7 @@ showOptValueInfoList() { done done echo "_OPT_ARGS=(${_OPT_ARGS[@]})" - echo "====================================================" + echo "===============================================================================" } parseOpts() { @@ -360,5 +360,5 @@ parseOpts() { # Main Methods ################################################# -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; bb2 -- cc dd +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -x -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; bb2 -- cc dd showOptValueInfoList From ae41c18caf025fe61dad759b6236810ff2bd0fd1 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 20:11:52 +0800 Subject: [PATCH 14/42] add test case for parse.sh --- parse.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parse.sh b/parse.sh index be7c2e36..4cd43bcd 100755 --- a/parse.sh +++ b/parse.sh @@ -360,5 +360,7 @@ parseOpts() { # Main Methods ################################################# -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -x -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; bb2 -- cc dd +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee showOptValueInfoList + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee From 96a187032ba6fc3b3118cff118e1f5703e7d44f8 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 20:15:02 +0800 Subject: [PATCH 15/42] remove debug output --- parse.sh | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/parse.sh b/parse.sh index 4cd43bcd..a8e0ae94 100755 --- a/parse.sh +++ b/parse.sh @@ -222,7 +222,6 @@ parseOpts() { # cut head and tail space sed -r 's/^\s+//;s/\s+$//' | awk -F '[\t ]*\\\\|[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` - blueEcho "XXX optDescLines=$optDescLines" while read optDesc ; do # optDesc LIKE b,b-long: [ -z "$optDesc" ] && continue @@ -236,7 +235,6 @@ parseOpts() { mode="-" ;; esac - blueEcho "XXX splite mode and optDesc: mode=$mode optDesc=$optDesc" local optLines=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long @@ -262,7 +260,6 @@ parseOpts() { } optTuple=("${optTuple[@]}" "$opt") done < <(echo "$optLines") - blueEcho "XXX find out optTuple: optDesc=$optDesc optTuple=(${optTuple[@]})" [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 @@ -275,22 +272,14 @@ parseOpts() { idxName+="_`convertToVarName "$o"`" evalOpts+=" $o" done - blueEcho "XXX idxName=$idxName" eval "$idxName=($mode $evalOpts)" - blueEcho "XXX eval $idxName=($mode $evalOpts)" local idxNameArrayPlaceHolder="$idxName[@]" - blueEcho "XXX idxName Array $idxName = (${!idxNameArrayPlaceHolder})" _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$idxName") - blueEcho "XXX _OPT_INFO_LIST_INDEX=(${_OPT_INFO_LIST_INDEX[@]})" done < <(echo "$optDescLines") - showOptDescInfoList - - redEcho "YYY start parse!" - local args=() while true; do [ $# -eq 0 ] && break @@ -361,6 +350,7 @@ parseOpts() { ################################################# parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee +showOptDescInfoList showOptValueInfoList parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee From ef91938c4da091c8a590ecd13cf5a2bf204133ae Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 20:20:52 +0800 Subject: [PATCH 16/42] remove debug output --- parse.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/parse.sh b/parse.sh index a8e0ae94..a0a87d98 100755 --- a/parse.sh +++ b/parse.sh @@ -297,7 +297,6 @@ parseOpts() { -*) # short & long option(-a, -a-long), use same read-in logic. local opt=`echo "$1" | sed -r 's/^--?//'` local mode=`findOptMode "$opt"` - redEcho "mode of $1 = $mode" case "$mode" in -) setOptBool "$opt" "true" @@ -325,9 +324,7 @@ parseOpts() { redEcho "value of option $opt no end comma, value = ${valueArray[@]}" return 231 } - redEcho shift "$((${#valueArray[@]} + 2))" shift "$((${#valueArray[@]} + 1))" - redEcho setOptArray "$opt" "${valueArray[@]}" setOptArray "$opt" "${valueArray[@]}" ;; *) From a5ccaa04f442053537ac7f3485dd64c32f716982 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 20:25:20 +0800 Subject: [PATCH 17/42] add util method yellowEcho --- parse.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parse.sh b/parse.sh index a0a87d98..a650a0ee 100755 --- a/parse.sh +++ b/parse.sh @@ -27,6 +27,10 @@ greenEcho() { colorEcho 32 "$@" } +yellowEcho() { + colorEcho 33 "$@" +} + blueEcho() { colorEcho 34 "$@" } From f790e2fef8ddccf708c97021b64ccadb5a323bb2 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 21:01:25 +0800 Subject: [PATCH 18/42] add parameter count check --- parse.sh | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/parse.sh b/parse.sh index a650a0ee..9fae8452 100755 --- a/parse.sh +++ b/parse.sh @@ -95,6 +95,11 @@ convertToVarName() { ################################################# findOptMode() { + [ $# -ne 1 ] && { + redEcho "NOT 1 arguemnts when call findOptMode: $@" + return 1 + } + local opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local ele0PlaceHolder="$idxName[0]" @@ -120,6 +125,11 @@ setOptBool() { } setOptValue() { + [ $# -ne 2 ] && { + redEcho "NOT 2 arguemnts when call setOptValue: $@" + return 1 + } + local opt="$1" local value="$2" @@ -147,6 +157,11 @@ setOptValue() { } setOptArray() { + [ $# -ne 2 ] && { + redEcho "NOT 2 arguemnts when call setOptArray: $@" + return 1 + } + local opt="$1" shift @@ -193,7 +208,7 @@ showOptValueInfoList() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode local arrayElePlaceHolder="$idxName[$i]" local optName="${!arrayElePlaceHolder}" local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" @@ -220,7 +235,7 @@ parseOpts() { local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+ shift - _OPT_INFO_LIST_INDEX=() + _OPT_INFO_LIST_INDEX=() # Global var local optDescLines=`echo "$optsDescription" | # cut head and tail space From b5e78fdccf2139cb1ce86e30664b2d655db7c547 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 21:34:03 +0800 Subject: [PATCH 19/42] add function cleanOptValueInfoList #22 --- parse.sh | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/parse.sh b/parse.sh index 9fae8452..fdc9175d 100755 --- a/parse.sh +++ b/parse.sh @@ -231,6 +231,10 @@ showOptValueInfoList() { echo "===============================================================================" } +cleanOptValueInfoList() { + unset _OPT_INFO_LIST_INDEX +} + parseOpts() { local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+ shift @@ -259,6 +263,7 @@ parseOpts() { [ $(echo "$optLines" | wc -l) -gt 2 ] && { redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 + cleanOptValueInfoList return 220 } @@ -269,11 +274,13 @@ parseOpts() { [ ${#opt} -eq 1 ] && { redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 + cleanOptValueInfoList return 221 } } || { echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 + cleanOptValueInfoList return 221 } } @@ -282,6 +289,7 @@ parseOpts() { [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 + cleanOptValueInfoList return 222 } @@ -306,6 +314,7 @@ parseOpts() { case "$1" in ---*) echo "Illegal option($1), more than 2 prefix -!" 1>&2 + cleanOptValueInfoList return 230 ;; --) @@ -323,7 +332,8 @@ parseOpts() { ;; :) [ $# -lt 2 ] && { - redEcho "Option $opt has NO value!" + echo "Option $opt has NO value!" 1>&2 + cleanOptValueInfoList return 231 } setOptValue "$opt" "$2" @@ -340,14 +350,16 @@ parseOpts() { } || valueArray=("${valueArray[@]}" "$value") done [ "$foundComma" ] || { - redEcho "value of option $opt no end comma, value = ${valueArray[@]}" + echo "value of option $opt no end comma, value = ${valueArray[@]}" 1>&2 + cleanOptValueInfoList return 231 } shift "$((${#valueArray[@]} + 1))" setOptArray "$opt" "${valueArray[@]}" ;; *) - redEcho "Undefined option $opt!" + echo "Undefined option $opt!" 1>&2 + cleanOptValueInfoList return 232 ;; esac @@ -370,3 +382,5 @@ showOptDescInfoList showOptValueInfoList parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee +showOptDescInfoList +showOptValueInfoList From 4de69abc80fea9d78543cf9fcf452a0ee63d2cbe Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 21:36:33 +0800 Subject: [PATCH 20/42] fix wrong parameter count check --- parse.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/parse.sh b/parse.sh index fdc9175d..4b339c6c 100755 --- a/parse.sh +++ b/parse.sh @@ -157,11 +157,6 @@ setOptValue() { } setOptArray() { - [ $# -ne 2 ] && { - redEcho "NOT 2 arguemnts when call setOptArray: $@" - return 1 - } - local opt="$1" shift From dd705d1e444474fbb9367ff86877fcdafd642c71 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 22:10:26 +0800 Subject: [PATCH 21/42] improve function cleanOptValueInfoList #22 --- parse.sh | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/parse.sh b/parse.sh index 4b339c6c..bff98ce0 100755 --- a/parse.sh +++ b/parse.sh @@ -143,9 +143,9 @@ setOptValue() { [ "$opt" = "$optName" ] && { # set _OPT_VALUE for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local name=`convertToVarName "${idxNameArray[$j]}"` + local optValueVarName="_OPT_VALUE_`convertToVarName "${idxNameArray[$j]}"`" local from='"$value"' - eval "_OPT_VALUE_$name=$from" + eval "$optValueVarName=$from" done return } @@ -170,9 +170,9 @@ setOptArray() { [ "$opt" = "$optName" ] && { # set _OPT_VALUE for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local name=`convertToVarName "${idxNameArray[$j]}"` + local optValueVarName="_OPT_VALUE_`convertToVarName "${idxNameArray[$j]}"`" local from='"$@"' - eval "_OPT_VALUE_$name=($from)" + eval "$optValueVarName=($from)" done return } @@ -227,7 +227,22 @@ showOptValueInfoList() { } cleanOptValueInfoList() { + echo "===============================================================================" + echo "show option value info list:" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") + + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode + local arrayElePlaceHolder="$idxName[$i]" + local optName="${!arrayElePlaceHolder}" + local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" + eval "unset $optValueVarName" + done + done + unset _OPT_INFO_LIST_INDEX + unset _OPT_ARGS } parseOpts() { From 6020d58995f47149cadecccfef0e32ad0d046b91 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 22:14:26 +0800 Subject: [PATCH 22/42] clean code --- parse.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/parse.sh b/parse.sh index bff98ce0..abff7bf1 100755 --- a/parse.sh +++ b/parse.sh @@ -227,8 +227,6 @@ showOptValueInfoList() { } cleanOptValueInfoList() { - echo "===============================================================================" - echo "show option value info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") From 9323b5fcf398981a1423a7a850bf986497d1d9be Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 22:26:47 +0800 Subject: [PATCH 23/42] add _opt prefix for inner function #25 --- parse.sh | 57 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/parse.sh b/parse.sh index abff7bf1..f325f309 100755 --- a/parse.sh +++ b/parse.sh @@ -94,9 +94,9 @@ convertToVarName() { # * _OPT_ARGS : option arguments ################################################# -findOptMode() { +_opts_findOptMode() { [ $# -ne 1 ] && { - redEcho "NOT 1 arguemnts when call findOptMode: $@" + redEcho "NOT 1 arguemnts when call _opts_findOptMode: $@" return 1 } @@ -120,13 +120,18 @@ findOptMode() { echo "" } -setOptBool() { - setOptValue "$@" +_opts_setOptBool() { + [ $# -ne 2 ] && { + redEcho "NOT 2 arguemnts when call _opts_setOptBool: $@" + return 1 + } + + _opts_setOptValue "$@" } -setOptValue() { +_opts_setOptValue() { [ $# -ne 2 ] && { - redEcho "NOT 2 arguemnts when call setOptValue: $@" + redEcho "NOT 2 arguemnts when call _opts_setOptValue: $@" return 1 } @@ -156,7 +161,7 @@ setOptValue() { return 1 } -setOptArray() { +_opts_setOptArray() { local opt="$1" shift @@ -183,7 +188,7 @@ setOptArray() { return 1 } -showOptDescInfoList() { +_opts_showOptDescInfoList() { echo "===============================================================================" echo "show option desc info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do @@ -193,7 +198,7 @@ showOptDescInfoList() { echo "===============================================================================" } -showOptValueInfoList() { +_opts_showOptValueInfoList() { echo "===============================================================================" echo "show option value info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do @@ -226,7 +231,7 @@ showOptValueInfoList() { echo "===============================================================================" } -cleanOptValueInfoList() { +_opts_cleanOptValueInfoList() { for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") @@ -271,7 +276,7 @@ parseOpts() { [ $(echo "$optLines" | wc -l) -gt 2 ] && { redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 220 } @@ -282,13 +287,13 @@ parseOpts() { [ ${#opt} -eq 1 ] && { redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 221 } } || { echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 221 } } @@ -297,7 +302,7 @@ parseOpts() { [ ${#optTuple[@]} -gt 2 ] && { redEcho "more than 2 opt($optDesc) in option description!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 222 } @@ -322,7 +327,7 @@ parseOpts() { case "$1" in ---*) echo "Illegal option($1), more than 2 prefix -!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 230 ;; --) @@ -332,19 +337,19 @@ parseOpts() { ;; -*) # short & long option(-a, -a-long), use same read-in logic. local opt=`echo "$1" | sed -r 's/^--?//'` - local mode=`findOptMode "$opt"` + local mode=`_opts_findOptMode "$opt"` case "$mode" in -) - setOptBool "$opt" "true" + _opts_setOptBool "$opt" "true" shift ;; :) [ $# -lt 2 ] && { echo "Option $opt has NO value!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 231 } - setOptValue "$opt" "$2" + _opts_setOptValue "$opt" "$2" shift 2 ;; +) @@ -359,15 +364,15 @@ parseOpts() { done [ "$foundComma" ] || { echo "value of option $opt no end comma, value = ${valueArray[@]}" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 231 } shift "$((${#valueArray[@]} + 1))" - setOptArray "$opt" "${valueArray[@]}" + _opts_setOptArray "$opt" "${valueArray[@]}" ;; *) echo "Undefined option $opt!" 1>&2 - cleanOptValueInfoList + _opts_cleanOptValueInfoList return 232 ;; esac @@ -386,9 +391,9 @@ parseOpts() { ################################################# parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee -showOptDescInfoList -showOptValueInfoList +_opts_showOptDescInfoList +_opts_showOptValueInfoList parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee -showOptDescInfoList -showOptValueInfoList +_opts_showOptDescInfoList +_opts_showOptValueInfoList From 18acb9214a9cfd4a0f7f27966e74a78f5d06d853 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sat, 27 Jul 2013 22:47:34 +0800 Subject: [PATCH 24/42] separate test code and lib code #27 --- parse.sh | 200 +++++++++++++-------------------------- test-cases/parse-test.sh | 13 +++ 2 files changed, 80 insertions(+), 133 deletions(-) create mode 100755 test-cases/parse-test.sh diff --git a/parse.sh b/parse.sh index f325f309..14cd06dc 100755 --- a/parse.sh +++ b/parse.sh @@ -1,13 +1,11 @@ #!/bin/bash -PROG=`basename $0` - -################################################# +##################################################################### # Utils Methods -################################################# +##################################################################### -colorEcho() { - color=$1 +_opts_colorEcho() { + local color=$1 shift if [ -c /dev/stdout ] ; then # if stdout is console, turn on color output. @@ -19,72 +17,16 @@ colorEcho() { fi } -redEcho() { - colorEcho 31 "$@" -} - -greenEcho() { - colorEcho 32 "$@" -} - -yellowEcho() { - colorEcho 33 "$@" -} - -blueEcho() { - colorEcho 34 "$@" -} - -echoCmdLineThenTimedRun() { - echo "===============================================================================" - echo "run command below: " - echo "$@" - echo "===============================================================================" - local start=`date +%s` - "$@" - local exitCode=$? - - echo - echo "Execute time: $((`date +%s` - $start))s" - - return $exitCode -} - -# cut head whitespace, delete \r -cutHeadspaceAndCR() { - [ $# -ne 1 ] && { - redEcho "NOT 1 arguemnt when call cutHeadspaceAndCR: $@" - return 1 - } - echo "$1" | tr -d '\r' | sed -r 's/^\s*//' -} - -checkEmptyOrComments() { - [ $# -ne 1 ] && { - redEcho "NOT 1 arguemnt when call checkEmptyOrComments: $@" - return 1 - } - echo "$1" | grep -Eq '^\s*#|^\s*$' -} - -extractValue() { - [ $# -ne 2 ] && { - redEcho "NOT 2 arguemnts when call extractValue: $@" - return 1 - } - - local settingFile=$1 - local key=$2 - grep "$key" -F "$settingFile" | tr -d '\r' | grep -vE '^\s*#' | - awk -F= '{print $2}' | sed -r 's/^\s*//;s/\s*$//' +_opts_redEcho() { + _opts_colorEcho 31 "$@" } -convertToVarName() { +_opts_convertToVarName() { local from="$1" echo "$from" | sed 's/-/_/g' } -################################################# +##################################################################### # Parse Methods # # Use Globle Variable: @@ -92,11 +34,11 @@ convertToVarName() { # _OPT_INFO_LIST_INDEX ->* _a_a_long -> option value. # * _OPT_VALUE_* : value of option. is Array type for + mode option # * _OPT_ARGS : option arguments -################################################# +##################################################################### _opts_findOptMode() { [ $# -ne 1 ] && { - redEcho "NOT 1 arguemnts when call _opts_findOptMode: $@" + _opts_redEcho "NOT 1 arguemnts when call _opts_findOptMode: $@" return 1 } @@ -122,16 +64,16 @@ _opts_findOptMode() { _opts_setOptBool() { [ $# -ne 2 ] && { - redEcho "NOT 2 arguemnts when call _opts_setOptBool: $@" + _opts_redEcho "NOT 2 arguemnts when call _opts_setOptBool: $@" return 1 } - + _opts_setOptValue "$@" } _opts_setOptValue() { [ $# -ne 2 ] && { - redEcho "NOT 2 arguemnts when call _opts_setOptValue: $@" + _opts_redEcho "NOT 2 arguemnts when call _opts_setOptValue: $@" return 1 } @@ -148,7 +90,7 @@ _opts_setOptValue() { [ "$opt" = "$optName" ] && { # set _OPT_VALUE for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local optValueVarName="_OPT_VALUE_`convertToVarName "${idxNameArray[$j]}"`" + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${idxNameArray[$j]}"`" local from='"$value"' eval "$optValueVarName=$from" done @@ -157,7 +99,7 @@ _opts_setOptValue() { done done - redEcho "NOT Found option $opt!" + _opts_redEcho "NOT Found option $opt!" return 1 } @@ -175,7 +117,7 @@ _opts_setOptArray() { [ "$opt" = "$optName" ] && { # set _OPT_VALUE for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local optValueVarName="_OPT_VALUE_`convertToVarName "${idxNameArray[$j]}"`" + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${idxNameArray[$j]}"`" local from='"$@"' eval "$optValueVarName=($from)" done @@ -184,53 +126,10 @@ _opts_setOptArray() { done done - redEcho "NOT Found option $opt!" + _opts_redEcho "NOT Found option $opt!" return 1 } -_opts_showOptDescInfoList() { - echo "===============================================================================" - echo "show option desc info list:" - for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do - local idxNameArrayPlaceHolder="$idxName[@]" - echo "$idxName = ${!idxNameArrayPlaceHolder}" - done - echo "===============================================================================" -} - -_opts_showOptValueInfoList() { - echo "===============================================================================" - echo "show option value info list:" - for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do - local ele0PlaceHolder="$idxName[0]" - local mode="${!ele0PlaceHolder}" - - local idxNameArrayPlaceHolder="$idxName[@]" - local idxNameArray=("${!idxNameArrayPlaceHolder}") - - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode - local arrayElePlaceHolder="$idxName[$i]" - local optName="${!arrayElePlaceHolder}" - local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" - - case "$mode" in - -) - echo "$optValueVarName=${!optValueVarName}" - ;; - :) - echo "$optValueVarName=${!optValueVarName}" - ;; - +) - local OptInfoArrayPlaceHolder="$optValueVarName[@]" - echo "$optValueVarName=(${!OptInfoArrayPlaceHolder})" - ;; - esac - done - done - echo "_OPT_ARGS=(${_OPT_ARGS[@]})" - echo "===============================================================================" -} - _opts_cleanOptValueInfoList() { for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" @@ -239,7 +138,7 @@ _opts_cleanOptValueInfoList() { for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode local arrayElePlaceHolder="$idxName[$i]" local optName="${!arrayElePlaceHolder}" - local optValueVarName="_OPT_VALUE_`convertToVarName "$optName"`" + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" eval "unset $optValueVarName" done done @@ -275,7 +174,7 @@ parseOpts() { local optLines=`echo "$optDesc" | awk -F '[\t ]*,[\t ]*' '{for(i=1; i<=NF; i++) print $i}'` # a\na-long [ $(echo "$optLines" | wc -l) -gt 2 ] && { - redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 + _opts_redEcho "Illegal option description($optDesc), more than 2 opt name!" 1>&2 _opts_cleanOptValueInfoList return 220 } @@ -285,14 +184,14 @@ parseOpts() { [ -z "$opt" ] && continue [ ${#opt} -eq 1 ] && { - redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { + _opts_redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList return 221 } } || { echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { - redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 + _opts_redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList return 221 } @@ -301,7 +200,7 @@ parseOpts() { done < <(echo "$optLines") [ ${#optTuple[@]} -gt 2 ] && { - redEcho "more than 2 opt($optDesc) in option description!" 1>&2 + _opts_redEcho "more than 2 opt($optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList return 222 } @@ -309,7 +208,7 @@ parseOpts() { local idxName= local evalOpts= for o in "${optTuple[@]}"; do - idxName+="_`convertToVarName "$o"`" + idxName+="_`_opts_convertToVarName "$o"`" evalOpts+=" $o" done @@ -386,14 +285,49 @@ parseOpts() { _OPT_ARGS=("${args[@]}") } -################################################# -# Main Methods -################################################# +##################################################################### +# Show parsed Option Info Methods +##################################################################### + +_opts_showOptDescInfoList() { + echo "===============================================================================" + echo "show option desc info list:" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do + local idxNameArrayPlaceHolder="$idxName[@]" + echo "$idxName = ${!idxNameArrayPlaceHolder}" + done + echo "===============================================================================" +} + +_opts_showOptValueInfoList() { + echo "===============================================================================" + echo "show option value info list:" + for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do + local ele0PlaceHolder="$idxName[0]" + local mode="${!ele0PlaceHolder}" + + local idxNameArrayPlaceHolder="$idxName[@]" + local idxNameArray=("${!idxNameArrayPlaceHolder}") -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee -_opts_showOptDescInfoList -_opts_showOptValueInfoList + for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode + local arrayElePlaceHolder="$idxName[$i]" + local optName="${!arrayElePlaceHolder}" + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee -_opts_showOptDescInfoList -_opts_showOptValueInfoList + case "$mode" in + -) + echo "$optValueVarName=${!optValueVarName}" + ;; + :) + echo "$optValueVarName=${!optValueVarName}" + ;; + +) + local OptInfoArrayPlaceHolder="$optValueVarName[@]" + echo "$optValueVarName=(${!OptInfoArrayPlaceHolder})" + ;; + esac + done + done + echo "_OPT_ARGS=(${_OPT_ARGS[@]})" + echo "===============================================================================" +} diff --git a/test-cases/parse-test.sh b/test-cases/parse-test.sh new file mode 100755 index 00000000..56d96ece --- /dev/null +++ b/test-cases/parse-test.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +BASE=`dirname $0` + +. $BASE/../parse.sh + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee +_opts_showOptDescInfoList +_opts_showOptValueInfoList + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee +_opts_showOptDescInfoList +_opts_showOptValueInfoList From e94f8cd2e48bdce70f7e8b78ef6450ae08c6226e Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 29 Jul 2013 13:41:08 +0800 Subject: [PATCH 25/42] simplify array operation code --- parse.sh | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/parse.sh b/parse.sh index 14cd06dc..6598d14f 100755 --- a/parse.sh +++ b/parse.sh @@ -44,15 +44,13 @@ _opts_findOptMode() { local opt="$1" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do - local ele0PlaceHolder="$idxName[0]" - local mode="${!ele0PlaceHolder}" - local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do - local arrayElePlaceHolder="$idxName[$i]" - [ "$opt" = "${!arrayElePlaceHolder}" ] && { + local mode="${idxNameArray[0]}" + + for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do + [ "$opt" = "${optName}" ] && { echo "$mode" return } @@ -83,14 +81,12 @@ _opts_setOptValue() { for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do - local arrayElePlaceHolder="$idxName[$i]" - local optName="${!arrayElePlaceHolder}" + + for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${idxNameArray[$j]}"`" + for optName2 in "${idxNameArray:1:${#idxNameArray[@]}}"; do + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$value"' eval "$optValueVarName=$from" done @@ -111,13 +107,11 @@ _opts_setOptArray() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do - local arrayElePlaceHolder="$idxName[$i]" - local optName="${!arrayElePlaceHolder}" + for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for (( j = 1; j < ${#idxNameArray[@]}; j++)); do - local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${idxNameArray[$j]}"`" + for optName2 in "${idxNameArray:1:${#idxNameArray[@]}}"; do + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$@"' eval "$optValueVarName=($from)" done @@ -135,9 +129,7 @@ _opts_cleanOptValueInfoList() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode - local arrayElePlaceHolder="$idxName[$i]" - local optName="${!arrayElePlaceHolder}" + for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do # index from 1, skip mode local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" eval "unset $optValueVarName" done @@ -303,17 +295,13 @@ _opts_showOptValueInfoList() { echo "===============================================================================" echo "show option value info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do - local ele0PlaceHolder="$idxName[0]" - local mode="${!ele0PlaceHolder}" - local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for ((i = 1; i < ${#idxNameArray[@]}; i++)); do # index from 1, skip mode - local arrayElePlaceHolder="$idxName[$i]" - local optName="${!arrayElePlaceHolder}" - local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" + local mode=${idxNameArray[0]} + for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do # index from 1, skip mode + local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" case "$mode" in -) echo "$optValueVarName=${!optValueVarName}" From 4339b4b9aa54879607b937a5728309769431761d Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 29 Jul 2013 23:33:19 +0800 Subject: [PATCH 26/42] fix array var parameter expansions bug; use opt in error msg --- parse.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/parse.sh b/parse.sh index 6598d14f..f58b4275 100755 --- a/parse.sh +++ b/parse.sh @@ -49,7 +49,7 @@ _opts_findOptMode() { local mode="${idxNameArray[0]}" - for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do [ "$opt" = "${optName}" ] && { echo "$mode" return @@ -82,10 +82,10 @@ _opts_setOptValue() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for optName2 in "${idxNameArray:1:${#idxNameArray[@]}}"; do + for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$value"' eval "$optValueVarName=$from" @@ -107,10 +107,10 @@ _opts_setOptArray() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do [ "$opt" = "$optName" ] && { # set _OPT_VALUE - for optName2 in "${idxNameArray:1:${#idxNameArray[@]}}"; do + for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$@"' eval "$optValueVarName=($from)" @@ -129,7 +129,7 @@ _opts_cleanOptValueInfoList() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do # index from 1, skip mode + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" eval "unset $optValueVarName" done @@ -227,11 +227,12 @@ parseOpts() { break ;; -*) # short & long option(-a, -a-long), use same read-in logic. - local opt=`echo "$1" | sed -r 's/^--?//'` - local mode=`_opts_findOptMode "$opt"` + local opt="$1" + local optName=`echo "$1" | sed -r 's/^--?//'` + local mode=`_opts_findOptMode "$optName"` case "$mode" in -) - _opts_setOptBool "$opt" "true" + _opts_setOptBool "$optName" "true" shift ;; :) @@ -240,7 +241,7 @@ parseOpts() { _opts_cleanOptValueInfoList return 231 } - _opts_setOptValue "$opt" "$2" + _opts_setOptValue "$optName" "$2" shift 2 ;; +) @@ -259,7 +260,7 @@ parseOpts() { return 231 } shift "$((${#valueArray[@]} + 1))" - _opts_setOptArray "$opt" "${valueArray[@]}" + _opts_setOptArray "$optName" "${valueArray[@]}" ;; *) echo "Undefined option $opt!" 1>&2 @@ -300,7 +301,7 @@ _opts_showOptValueInfoList() { local mode=${idxNameArray[0]} - for optName in "${idxNameArray:1:${#idxNameArray[@]}}"; do # index from 1, skip mode + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" case "$mode" in -) From 0013bbcdadb9ecd2a440c338cbc4e5c901d89774 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Mon, 29 Jul 2013 23:42:41 +0800 Subject: [PATCH 27/42] clean code --- parse.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/parse.sh b/parse.sh index f58b4275..811befa3 100755 --- a/parse.sh +++ b/parse.sh @@ -49,7 +49,7 @@ _opts_findOptMode() { local mode="${idxNameArray[0]}" - for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "${optName}" ] && { echo "$mode" return @@ -82,7 +82,7 @@ _opts_setOptValue() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "$optName" ] && { # set _OPT_VALUE for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do @@ -107,7 +107,7 @@ _opts_setOptArray() { local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") - for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do + for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "$optName" ] && { # set _OPT_VALUE for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do @@ -287,7 +287,7 @@ _opts_showOptDescInfoList() { echo "show option desc info list:" for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" - echo "$idxName = ${!idxNameArrayPlaceHolder}" + echo "$idxName = (${!idxNameArrayPlaceHolder})" done echo "===============================================================================" } @@ -311,8 +311,8 @@ _opts_showOptValueInfoList() { echo "$optValueVarName=${!optValueVarName}" ;; +) - local OptInfoArrayPlaceHolder="$optValueVarName[@]" - echo "$optValueVarName=(${!OptInfoArrayPlaceHolder})" + local optArrayValueArrayPlaceHolder="$optValueVarName[@]" + echo "$optValueVarName=(${!optArrayValueArrayPlaceHolder})" ;; esac done From 44a7dde71a3d666509979d9c0a40ced16b3b9b8f Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 00:26:06 +0800 Subject: [PATCH 28/42] unify error code --- parse.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parse.sh b/parse.sh index 811befa3..cc585dfe 100755 --- a/parse.sh +++ b/parse.sh @@ -185,7 +185,7 @@ parseOpts() { echo "$opt" | grep -E '^[-a-zA-Z0-9]+$' -q || { _opts_redEcho "Illegal long option name($opt in $optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList - return 221 + return 222 } } optTuple=("${optTuple[@]}" "$opt") @@ -194,7 +194,7 @@ parseOpts() { [ ${#optTuple[@]} -gt 2 ] && { _opts_redEcho "more than 2 opt($optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList - return 222 + return 223 } local idxName= From e09ace1d887d1d58fc28ed24bb092a1b28d641ef Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 00:31:29 +0800 Subject: [PATCH 29/42] add test case #28 --- test-cases/parse-test.sh | 107 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 2 deletions(-) diff --git a/test-cases/parse-test.sh b/test-cases/parse-test.sh index 56d96ece..6248eb61 100755 --- a/test-cases/parse-test.sh +++ b/test-cases/parse-test.sh @@ -4,10 +4,113 @@ BASE=`dirname $0` . $BASE/../parse.sh -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -a -b -c cc a1 a2 \; bb -d d.sh d1 d2 d3 \; cc -- dd ee + +################################################# +# Utils Methods +################################################# + +colorEcho() { + color=$1 + shift + if [ -c /dev/stdout ] ; then + # if stdout is console, turn on color output. + echo -ne "\033[1;${color}m" + echo -n "$@" + echo -e "\033[0m" + else + echo "$@" + fi +} + +redEcho() { + colorEcho 31 "$@" +} + +greenEcho() { + colorEcho 32 "$@" +} + +yellowEcho() { + colorEcho 33 "$@" +} + +blueEcho() { + colorEcho 34 "$@" +} + +arrayEquals() { + local a1PlaceHolder="$1[@]" + local a2PlaceHolder="$2[@]" + local a1=("${!a1PlaceHolder}") + local a2=("${!a2PlaceHolder}") + + [ ${#a1[@]} -eq ${#a2[@]} ] || return 1 + + for((i=0; i<${#a1[@]}; i++)); do + [ "${a1[$i]}" = "${a2[$i]}" ] || return 1 + done +} + +fail() { + redEcho "$@" + exit 1 +} + +################################################# +# Test +################################################# + + +blueEcho "Test case: success parse" + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +exitCode=$? _opts_showOptDescInfoList _opts_showOptValueInfoList -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv \; bb -d -d d.sh d1 d2 d3 \; cc -- dd ee +[ $exitCode -eq 0 ] || fail "Wrong exit code!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ $_OPT_VALUE_a = "true" ] && [ $_OPT_VALUE_a_long = "true" ] || fail "Wrong option value of a!" +[ $_OPT_VALUE_b = "bb" ] && [ $_OPT_VALUE_b_long = "bb" ] || fail "Wrong option value of b!" +cArray=(c.sh -p pv -q qv cc) +arrayEquals cArray _OPT_VALUE_c && arrayEquals cArray _OPT_VALUE_c_long || fail "Wrong option value of c!" +dArray=(d.sh -x xv d1 d2 d3 ) +arrayEquals dArray _OPT_VALUE_d && arrayEquals cArray _OPT_VALUE_d_long || fail "Wrong option value of d!" +argArray=(aa bb cc dd ee) +arrayEquals argArray _OPT_ARGS || fail "Wrong args!" + + + +blueEcho "Test case: illegal option x" + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +exitCode=$? +_opts_showOptDescInfoList +_opts_showOptValueInfoList + +[ $exitCode -eq 221 ] || fail "Wrong exit code!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" +[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" +[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" +[ "$_OPT_ARGS" = "" ] || fail "Wrong args!" + + + +blueEcho "Test case: empty options" + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+" +exitCode=$? _opts_showOptDescInfoList _opts_showOptValueInfoList + +[ $exitCode -eq 0 ] || fail "Wrong exit code!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" +[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" +[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" +[ "$_OPT_ARGS" = "" ] || fail "Wrong args!" + + From 6497a7c66f32f3bacf086a720f253f932f994d1d Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 00:42:26 +0800 Subject: [PATCH 30/42] add test case #28 --- test-cases/parse-test.sh | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test-cases/parse-test.sh b/test-cases/parse-test.sh index 6248eb61..d7f12769 100755 --- a/test-cases/parse-test.sh +++ b/test-cases/parse-test.sh @@ -75,7 +75,7 @@ _opts_showOptValueInfoList cArray=(c.sh -p pv -q qv cc) arrayEquals cArray _OPT_VALUE_c && arrayEquals cArray _OPT_VALUE_c_long || fail "Wrong option value of c!" dArray=(d.sh -x xv d1 d2 d3 ) -arrayEquals dArray _OPT_VALUE_d && arrayEquals cArray _OPT_VALUE_d_long || fail "Wrong option value of d!" +arrayEquals dArray _OPT_VALUE_d && arrayEquals dArray _OPT_VALUE_d_long || fail "Wrong option value of d!" argArray=(aa bb cc dd ee) arrayEquals argArray _OPT_ARGS || fail "Wrong args!" @@ -88,7 +88,7 @@ exitCode=$? _opts_showOptDescInfoList _opts_showOptValueInfoList -[ $exitCode -eq 221 ] || fail "Wrong exit code!" +[ $exitCode -eq 232 ] || fail "Wrong exit code!" [ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" [ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" @@ -114,3 +114,32 @@ _opts_showOptValueInfoList [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" + +blueEcho "Test case: illegal option name" + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|-#,-z-long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +exitCode=$? +_opts_showOptDescInfoList +_opts_showOptValueInfoList + +[ $exitCode -eq 221 ] || fail "Wrong exit code!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" +[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" +[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" +[ "$_OPT_ARGS" = "" ] || fail "Wrong args!" + + +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|-z,-z-#long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +exitCode=$? +_opts_showOptDescInfoList +_opts_showOptValueInfoList + +[ $exitCode -eq 222 ] || fail "Wrong exit code!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" +[ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" +[ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" +[ "$_OPT_VALUE_d" = "" ] && [ "$_OPT_VALUE_d_long" = "" ] || fail "Wrong option value of d!" +[ "$_OPT_ARGS" = "" ] || fail "Wrong args!" From 97971a997042d7a33440da65a28bedba6428b534 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 00:57:29 +0800 Subject: [PATCH 31/42] add test case #28 --- test-cases/parse-test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-cases/parse-test.sh b/test-cases/parse-test.sh index d7f12769..b39d8c83 100755 --- a/test-cases/parse-test.sh +++ b/test-cases/parse-test.sh @@ -106,7 +106,7 @@ _opts_showOptDescInfoList _opts_showOptValueInfoList [ $exitCode -eq 0 ] || fail "Wrong exit code!" -[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 0 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" +[ ${#_OPT_INFO_LIST_INDEX[@]} -eq 4 ] || fail "Wrong _OPT_INFO_LIST_INDEX!" [ "$_OPT_VALUE_a" = "" ] && [ "$_OPT_VALUE_a_long" = "" ] || fail "Wrong option value of a!" [ "$_OPT_VALUE_b" = "" ] && [ "$_OPT_VALUE_b_long" = "" ] || fail "Wrong option value of b!" [ "$_OPT_VALUE_c" = "" ] && [ "$_OPT_VALUE_c_long" = "" ] || fail "Wrong option value of c!" @@ -117,7 +117,7 @@ _opts_showOptValueInfoList blueEcho "Test case: illegal option name" -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|-#,-z-long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|#,z-long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee exitCode=$? _opts_showOptDescInfoList _opts_showOptValueInfoList @@ -131,7 +131,7 @@ _opts_showOptValueInfoList [ "$_OPT_ARGS" = "" ] || fail "Wrong args!" -parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|-z,-z-#long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee +parseOpts "a,a-long|b,b-long:|c,c-long+|d,d-long+|z,z-#long" aa -a -b bb -x -c c.sh -p pv -q qv cc \; bb -d d.sh -x xv d1 d2 d3 \; cc -- dd ee exitCode=$? _opts_showOptDescInfoList _opts_showOptValueInfoList From c0fc7a347207e745daa43c7b5fb3fd2a0a029879 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 01:38:52 +0800 Subject: [PATCH 32/42] add bash doc for parse.sh --- parse.sh | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/parse.sh b/parse.sh index cc585dfe..c2928d3c 100755 --- a/parse.sh +++ b/parse.sh @@ -1,4 +1,21 @@ #!/bin/bash +# @Function +# parse options lib, support multiple values for one option. +# +# @Usage +# source this script to your script file, then use func parseOpts. +# parseOpts func useage sample: +# $ parseOpts "a,a-long|b,b-long:|c,c-long+" -a -b bv -c c.sh -p pv -q qv arg1 \; aa bb cc +# then below globle var is set: +# _OPT_VALUE_a = true +# _OPT_VALUE_a_long = true +# _OPT_VALUE_b = bv +# _OPT_VALUE_b_long = bv +# _OPT_VALUE_c = (c.sh -p pv -q qv arg1) # Array type +# _OPT_VALUE_c_long = (c.sh -p pv -q qv arg1) # Array type +# _OPT_ARGS = (aa bb cc) # Array type +# +# @author Jerry Lee ##################################################################### # Utils Methods @@ -22,8 +39,11 @@ _opts_redEcho() { } _opts_convertToVarName() { - local from="$1" - echo "$from" | sed 's/-/_/g' + [ $# -ne 1 ] && { + _opts_redEcho "NOT 1 arguemnts when call _opts_convertToVarName: $@" + return 1 + } + echo "$1" | sed 's/-/_/g' } ##################################################################### @@ -42,7 +62,7 @@ _opts_findOptMode() { return 1 } - local opt="$1" + local opt="$1" # like a, a-long for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") @@ -75,7 +95,7 @@ _opts_setOptValue() { return 1 } - local opt="$1" + local opt="$1" # like a, a-long local value="$2" for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do @@ -100,7 +120,7 @@ _opts_setOptValue() { } _opts_setOptArray() { - local opt="$1" + local opt="$1" # like a, a-long shift for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do From 3540c0713912f3188ca53b2fce8edea967a81d21 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 01:50:54 +0800 Subject: [PATCH 33/42] check global var use by eyes. #26 --- parse.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/parse.sh b/parse.sh index c2928d3c..a887301a 100755 --- a/parse.sh +++ b/parse.sh @@ -196,7 +196,7 @@ parseOpts() { [ -z "$opt" ] && continue [ ${#opt} -eq 1 ] && { - _opts_redEcho "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { + echo "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList return 221 @@ -225,9 +225,6 @@ parseOpts() { done eval "$idxName=($mode $evalOpts)" - - local idxNameArrayPlaceHolder="$idxName[@]" - _OPT_INFO_LIST_INDEX=("${_OPT_INFO_LIST_INDEX[@]}" "$idxName") done < <(echo "$optDescLines") @@ -237,7 +234,7 @@ parseOpts() { case "$1" in ---*) - echo "Illegal option($1), more than 2 prefix -!" 1>&2 + _opts_redEcho "Illegal option($1), more than 2 prefix '-'!" 1>&2 _opts_cleanOptValueInfoList return 230 ;; From 47ca0f816496540519f421ee3589de8240ef9c8f Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 02:00:07 +0800 Subject: [PATCH 34/42] rename to parseOpts.sh --- parse.sh => parseOpts.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename parse.sh => parseOpts.sh (100%) diff --git a/parse.sh b/parseOpts.sh similarity index 100% rename from parse.sh rename to parseOpts.sh From 739583805ec283ad23a43433b3e9250f16768988 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 02:39:16 +0800 Subject: [PATCH 35/42] add doc for parseOpts #19 --- README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3e4e185e..803b364b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ PS:如何操作可以参见[@bluedavy](http://weibo.com/bluedavy)的《分布 这个脚本的功能是,打印出在运行的`Java`进程中,消耗`CPU`最多的线程栈(缺省是5个线程)。 -用法: +### 用法 ```bash show-busy-java-threads.sh -c <要显示的线程栈数> @@ -56,7 +56,7 @@ show-busy-java-threads.sh -c <要显示的线程栈数> show-busy-java-threads.sh -c <要显示的线程栈数> -p <指定的Java Process> ``` -示例: +### 示例 ```bash $ show-busy-java-threads.sh @@ -90,37 +90,93 @@ The stack of busy(26.1%) thread(24018/0x5dd2) of java process(23269) of user(adm ... ``` +### 贡献者 + [silentforce](https://github.com/silentforce)改进此脚本,增加对环境变量`JAVA_HOME`的判断。 +parseOpts.sh +========================== + +提供命令行选项解析函数`parseOpts`,支持选项的值有多个值(即数组)。 +\# 自己写一个命令行选项解析函数,是因为`bash`的buildin命令`getopts`和加强版本命令`getopt`都不支持数组的值。 + +### 用法 + +parseOpts函数的第一个参数是要解析的选项说明,后面跟实际要解析的输入参数。 + +选项说明可以长选项和短选项,用逗号分隔,如`a,a-long`。不同选项的说明间用坚号分隔,如`a,a-long|b,b-long:`。 + +选项说明最后可以有选项类型说明: + +* `-`,无参数的选项。即有选项则把值设置成`true`。这是缺省的类型。 +* `:`,有参数的选项,值只有一个。 +* `+`,有多个参数值的选项。值要以`;`表示结束。注意,`;`是`Bash`的元字符,可以写成`\;`,具体参见用法示例。 + +实际要解析的输入参数往往是你的脚本参数,这样parseOpts函数调用一般是: +```bash +parseOpts "a,a-long|b,b-long:|c,c-long+" "$@" +# "$@" 即是回放你的脚本参数 +``` + +通过约定的全局变量来获取选项值: + +* 选项名为`a`,通过全局变量`_OPT_VALUE_a`来获取选项的值。 +* 选项名为`a-long`,通过全局变量`_OPT_VALUE_a_long`来获取选项的值。 + +即,把选项名的`-`转`_`,再加上前缀`_OPT_VALUE_`对应的全局变量来获得选项值。 + +选项及选项值剩下的参数,通过`_OPT_ARGS`来获取。 + +### 示例 + +```bash +# 导入parseOpts.sh +source /path/to/parseOpts.sh + +parseOpts "a,a-long|b,b-long:|c,c-long+" -a -b bv -c c.sh -p pv -q qv arg1 \; aa bb cc +# 可以通过下面全局变量来获得解析的参数值: +# _OPT_VALUE_a = true +# _OPT_VALUE_a_long = true +# _OPT_VALUE_b = bv +# _OPT_VALUE_b_long = bv +# _OPT_VALUE_c = (c.sh -p pv -q qv arg1) ,数组类型 +# _OPT_VALUE_c_long = (c.sh -p pv -q qv arg1) ,数组类型 +# _OPT_ARGS = (aa bb cc) # Array type +``` + cp-svn-url.sh ========================== 拷贝当前`svn`目录对应的远程分支。 -用法: +### 用法 ```bash cp-svn-url.sh # 缺省使用当前目录作为SVN工作目录 cp-svn-url.sh /path/to/svn/work/directory ``` -示例: +### 示例 ```bash $ cp-svn-url.sh http://www.foo.com/project1/branches/feature1 copied! ``` -此脚本由[ivanzhangwb](https://github.com/ivanzhangwb)提供。 +### 贡献者 + +[ivanzhangwb](https://github.com/ivanzhangwb)提供此脚本。 -参考资料:[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.com/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。 +### 参考资料 + +[拷贝复制命令行输出放在系统剪贴板上](http://oldratlee.com/post/2012-12-23/command-output-to-clip),给出了不同系统可用命令。 find-in-jars.sh ========================== 在当前目录下所有`Jar`文件里,查找文件名。 -用法: +### 用法 ```bash find-in-jars.sh 'log4j\.properties' @@ -131,7 +187,7 @@ find-in-jars.sh 'log4j\.properties|log4j\.xml' 注意,后面Pattern是`grep`的扩展正则表达式。 -示例: +### 示例 ```bash $ ./find-in-jars 'Service.class$' @@ -139,7 +195,9 @@ $ ./find-in-jars 'Service.class$' ./rpc-benchmark-0.0.1-SNAPSHOT.jar!com/taobao/rpc/benchmark/service/HelloService.class ``` -参考资料:[在多个Jar(Zip)文件查找Log4J配置文件的Shell命令行](http://oldratlee.com/458/tech/shell/find-file-in-jar-zip-files.html) +### 参考资料 + +[在多个Jar(Zip)文件查找Log4J配置文件的Shell命令行](http://oldratlee.com/458/tech/shell/find-file-in-jar-zip-files.html) echo-args.sh ============================== @@ -148,7 +206,7 @@ echo-args.sh 这个脚本输出脚本收到的参数。在控制台运行时,把参数值括起的括号显示成 **红色**,方便人眼查看。 -示例: +### 示例 ```bash $ ./echo-args.sh 1 " 2 foo " "3 3" @@ -158,7 +216,7 @@ $ ./echo-args.sh 1 " 2 foo " "3 3" 3/3: [3 3] ``` -使用方式: +### 使用方式 需要查看某个脚本(实际上也可以是其它的可执行程序)输出参数时,可以这么做: @@ -175,7 +233,7 @@ xpl and xpf * xpf: 在文件浏览器中打开指定的文件或文件夹,并选中。 \# xpf是`explorer and select file`的缩写。 -用法: +### 用法&示例 ```bash xpl /path/to/dir From 02788322b3748f0ca87ed6d9592489e38bb4cb41 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 03:06:20 +0800 Subject: [PATCH 36/42] use red echo for error msg --- parseOpts.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/parseOpts.sh b/parseOpts.sh index a887301a..8c086aa0 100755 --- a/parseOpts.sh +++ b/parseOpts.sh @@ -197,7 +197,7 @@ parseOpts() { [ ${#opt} -eq 1 ] && { echo "$opt" | grep -E '^[a-zA-Z0-9]$' -q || { - echo "Illegal short option name($opt in $optDesc) in option description!" 1>&2 + _opts_redEcho "Illegal short option name($opt in $optDesc) in option description!" 1>&2 _opts_cleanOptValueInfoList return 221 } @@ -254,7 +254,7 @@ parseOpts() { ;; :) [ $# -lt 2 ] && { - echo "Option $opt has NO value!" 1>&2 + _opts_redEcho "Option $opt has NO value!" 1>&2 _opts_cleanOptValueInfoList return 231 } @@ -272,7 +272,7 @@ parseOpts() { } || valueArray=("${valueArray[@]}" "$value") done [ "$foundComma" ] || { - echo "value of option $opt no end comma, value = ${valueArray[@]}" 1>&2 + _opts_redEcho "value of option $opt no end comma, value = ${valueArray[@]}" 1>&2 _opts_cleanOptValueInfoList return 231 } @@ -280,7 +280,7 @@ parseOpts() { _opts_setOptArray "$optName" "${valueArray[@]}" ;; *) - echo "Undefined option $opt!" 1>&2 + _opts_redEcho "Undefined option $opt!" 1>&2 _opts_cleanOptValueInfoList return 232 ;; From ef65ee084bb9d3e70740d355b62b0bcf09153c63 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Tue, 30 Jul 2013 10:36:05 +0800 Subject: [PATCH 37/42] clean comments --- parseOpts.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/parseOpts.sh b/parseOpts.sh index 8c086aa0..b870ea85 100755 --- a/parseOpts.sh +++ b/parseOpts.sh @@ -104,11 +104,10 @@ _opts_setOptValue() { for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "$optName" ] && { - # set _OPT_VALUE for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$value"' - eval "$optValueVarName=$from" + eval "$optValueVarName=$from" # set global var! done return } @@ -133,7 +132,7 @@ _opts_setOptArray() { for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$@"' - eval "$optValueVarName=($from)" + eval "$optValueVarName=($from)" # set global var! done return } @@ -163,7 +162,7 @@ parseOpts() { local optsDescription="$1" # optsDescription LIKE a,a-long|b,b-long:|c,c-long+ shift - _OPT_INFO_LIST_INDEX=() # Global var + _OPT_INFO_LIST_INDEX=() # set global var! local optDescLines=`echo "$optsDescription" | # cut head and tail space @@ -292,7 +291,7 @@ parseOpts() { ;; esac done - _OPT_ARGS=("${args[@]}") + _OPT_ARGS=("${args[@]}") # set global var! } ##################################################################### From 8fec2d0e1a55b286c86521959a0ef6491d7a637a Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 4 Aug 2013 17:15:15 +0800 Subject: [PATCH 38/42] add file check for jstack --- show-busy-java-threads.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/show-busy-java-threads.sh b/show-busy-java-threads.sh index 8a086fcb..3771232b 100755 --- a/show-busy-java-threads.sh +++ b/show-busy-java-threads.sh @@ -62,7 +62,7 @@ redEcho() { ## Check the existence of jstack command! if ! which jstack &> /dev/null; then - if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/jstack" ]; then + if [ -n "$JAVA_HOME" ] && [ -f "$JAVA_HOME/bin/jstack" ] && [ -x "$JAVA_HOME/bin/jstack" ]; then export PATH=$PATH:$JAVA_HOME/bin else redEcho "Error: jstack not found on PATH and JAVA_HOME!" From d8729ef1d29ca6630401a2298fd0bae3066c187c Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 4 Aug 2013 17:53:59 +0800 Subject: [PATCH 39/42] local the for variable --- parseOpts.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/parseOpts.sh b/parseOpts.sh index b870ea85..96547ae1 100755 --- a/parseOpts.sh +++ b/parseOpts.sh @@ -63,12 +63,14 @@ _opts_findOptMode() { } local opt="$1" # like a, a-long + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") local mode="${idxNameArray[0]}" + local optName for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "${optName}" ] && { echo "$mode" @@ -98,12 +100,15 @@ _opts_setOptValue() { local opt="$1" # like a, a-long local value="$2" + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") + local optName for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "$optName" ] && { + local optName2 for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$value"' @@ -122,13 +127,16 @@ _opts_setOptArray() { local opt="$1" # like a, a-long shift + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}" ; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") + local optName for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode [ "$opt" = "$optName" ] && { # set _OPT_VALUE + local optName2 for optName2 in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "${optName2}"`" local from='"$@"' @@ -144,10 +152,12 @@ _opts_setOptArray() { } _opts_cleanOptValueInfoList() { + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") + local optName for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" eval "unset $optValueVarName" @@ -218,6 +228,7 @@ parseOpts() { local idxName= local evalOpts= + local o for o in "${optTuple[@]}"; do idxName+="_`_opts_convertToVarName "$o"`" evalOpts+=" $o" @@ -264,6 +275,8 @@ parseOpts() { shift local valueArray=() local foundComma="" + + local value for value in "$@" ; do [ ";" = "$value" ] && { foundComma=true @@ -301,6 +314,7 @@ parseOpts() { _opts_showOptDescInfoList() { echo "===============================================================================" echo "show option desc info list:" + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" echo "$idxName = (${!idxNameArrayPlaceHolder})" @@ -311,12 +325,14 @@ _opts_showOptDescInfoList() { _opts_showOptValueInfoList() { echo "===============================================================================" echo "show option value info list:" + local idxName for idxName in "${_OPT_INFO_LIST_INDEX[@]}"; do local idxNameArrayPlaceHolder="$idxName[@]" local idxNameArray=("${!idxNameArrayPlaceHolder}") local mode=${idxNameArray[0]} + local optName for optName in "${idxNameArray[@]:1:${#idxNameArray[@]}}"; do # index from 1, skip mode local optValueVarName="_OPT_VALUE_`_opts_convertToVarName "$optName"`" case "$mode" in From 2cb08193ab117ee37afd90adf5019f767d38be26 Mon Sep 17 00:00:00 2001 From: oldratlee Date: Sun, 4 Aug 2013 17:57:18 +0800 Subject: [PATCH 40/42] rename file name --- test-cases/{parse-test.sh => parseOpts-test.sh} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test-cases/{parse-test.sh => parseOpts-test.sh} (99%) diff --git a/test-cases/parse-test.sh b/test-cases/parseOpts-test.sh similarity index 99% rename from test-cases/parse-test.sh rename to test-cases/parseOpts-test.sh index b39d8c83..6dc02c6b 100755 --- a/test-cases/parse-test.sh +++ b/test-cases/parseOpts-test.sh @@ -2,7 +2,7 @@ BASE=`dirname $0` -. $BASE/../parse.sh +. $BASE/../parseOpts.sh ################################################# From 61a9d12cad1a157560b70e56bed36e57ef6ed26d Mon Sep 17 00:00:00 2001 From: oldratlee Date: Thu, 8 Aug 2013 21:41:57 +0800 Subject: [PATCH 41/42] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 803b364b..9d902551 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ parseOpts "a,a-long|b,b-long:|c,c-long+" -a -b bv -c c.sh -p pv -q qv arg1 \; aa # _OPT_VALUE_b_long = bv # _OPT_VALUE_c = (c.sh -p pv -q qv arg1) ,数组类型 # _OPT_VALUE_c_long = (c.sh -p pv -q qv arg1) ,数组类型 -# _OPT_ARGS = (aa bb cc) # Array type +# _OPT_ARGS = (aa bb cc) ,数组类型 ``` cp-svn-url.sh From 79947b875516db3669e2b2e90fbf705faff88d9d Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Thu, 26 Sep 2013 17:04:33 +0800 Subject: [PATCH 42/42] ignore output of clean --- show-busy-java-threads.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/show-busy-java-threads.sh b/show-busy-java-threads.sh index 3771232b..1d40a4e3 100755 --- a/show-busy-java-threads.sh +++ b/show-busy-java-threads.sh @@ -25,6 +25,7 @@ EOF } ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"` +echo "$ARGS" [ $? -ne 0 ] && usage 1 eval set -- "${ARGS}" @@ -73,7 +74,7 @@ fi uuid=`date +%s`_${RANDOM}_$$ cleanupWhenExit() { - rm /tmp/${uuid}_* > /dev/null + rm /tmp/${uuid}_* &> /dev/null } trap "cleanupWhenExit" EXIT