On 06/29/2010 08:52 AM, Gary V. Vaughan wrote:
Well, really the problem is this:
while $# -gt 0; do
opt=$1; shift
case $opt in
-p) opt_p="$1"; shift ;;
-q) opt_q="$1"; shift ;;
-x) opt_x=: ;;
-y) opt_y=: ;;
-p*|-q*) # option args
func_split_short_arg $opt
set dummy $arg "$rest" ${1+"$@"}; shift ;;
-x*|-y*) # non-option args
func_split_short_arg $opt
set dummy $arg -$rest ${1+"$@"}; shift ;;
esac
done
So, we know there will always be at least 3 characters available in $1
by the time we reach func_split_short_arg. Also we don't really discard
the leading '-'. Which means that this works correctly:
func_split_short_arg ()
{
arg="$1"; while test ${#arg} -gt 2; do arg="${arg%?}"; done
rest=${1%??}
}
What about
func_split_short_arg () {
rest=${1#??};
arg=${1%"$rest"};
}
The quoting ensures that it works even if a star is present in $1:
$ func_split_short_arg -X*YZ ; echo $arg; echo $rest
-X
*YZ
$ func_split_short_arg -X????YZ ; echo $arg; echo $rest
-X
????YZ
Paolo