set command will not allow parameters with leading minus "-option"

  • Follow


Referencing the two scripts below, if you take all the command line
arguments and run them through a case statement from one script, how
can you leave the unused arguments intact for any remaining scritps to
use?  I've taken a stab at this as shown below, but the best I came up
with left me escaping options which started with a minus.  Is there any
way to put the unused options back in $* as they originally appeared?

Regards,

-Inet




###################### Begin server-selector.sh
#########################


#!/usr/bin/ksh

Svrs=~/scripts/servers

DoUsage(){
clear
cat << EOF
This paragraph pertains exclusively to server-selector.sh which was
sourced by the calling script: $0.
It is meant to serve as a plugin to other scripts to abstract the
process of selecting servers based on
servername, tier, or all contained in the servers file. These options:
<-t|-a|-s> will be consumed by
server-selector.sh and cannot be used by the calling script.  Any other
options remain in the '$*' variable.
However those options will have any minus signs (-) escaped. If you
passed the following options to your
script:  "-t dev -test1 one -test2 two", the value of the remaining
options in "$*" are:  "\-test1 one \-test2 two".

EOF
}

while [ "$1" != "" ]; do
    case $1 in
        -t | --tier )     shift
                          TierFilter=$1
                          set -A ServerArray `egrep -e
".*\,.*\,$TierFilter" $Svrs | awk -F, '{print $1}'`
                          ;;
        -a | --all  )     TierFilter='.'
                          set -A ServerArray `egrep -e
".*\,.*\,$TierFilter" $Svrs | awk -F, '{print $1}'`
                          ;;
        -s | --server     ) shift
                          set -A ServerArray $1
                          ;;
        * )               Leftover="$Leftover $1"
	esac
	shift
done

#the only way i've found to get parameters back into $* is if they are
escaped.
#explicitly doing something like:  "set -a aaa -b bbb" does not work.
Is there
#a way to put parameters back so that they are not escaped?

Leftover=`echo "$Leftover" | sed s/-/\\\\\\\-/g`
set $Leftover > /dev/null


###################### End server-selector.sh
###########################

################## Begin server-selector-example.sh
#####################

#!/usr/bin/ksh

.. server-selector.sh


while [ "$1" != "" ]; do
    case $1 in
        \\-x | \\-\\-xtra ) shift
               				x="$1"
               				;;
        * 	 			  ) DoUsage
			   				echo "Error processing option: $1\n"
			   				exit 1
    esac
    shift
done

DoStuff(){ echo "$SERVER" }

echo "Processing ${#ServerArray[@]} servers...\n"

for SERVER in ${ServerArray[@]};
do
        if [[ ${#ServerArray[@]} -le 1 ]]; then
                DoStuff
        else
                DoStuff &
        fi
done

################### End server-selector-example.sh
######################

0
Reply inetquestion (72) 3/9/2006 3:34:24 PM

In article <1141918464.722989.117230@u72g2000cwu.googlegroups.com>,
	"inetquestion" <inetquestion@hotmail.com> writes:
[...]


I'm not even going to attempt to untangle what you're trying to do, but
in response to the subject line:


$ print -r -- $*

$ set -- -bogus
$ print -r -- $*
-bogus
$ set --
$ print -r -- $*

$ 

So, yes you can set anything you want into the current args; if it begins
with a dash, you just have to give "set" a -- first to indicate the end of
options to "set" itself.

Many other commands behave this way too, although some folks always go and
write their own option parsing without implementing such a feature.

-- 
mailto:rlhamil@smart.net  http://www.smart.net/~rlhamil

Lasik/PRK theme music:
    "In the Hall of the Mountain King", from "Peer Gynt"
0
Reply Richard 3/10/2006 12:32:56 AM


haha... I can't say I blame you for not trying to figure out all the
crap in there.  However I do thank you for answering my question.  That
cleared everything up!

0
Reply inetquestion 3/10/2006 6:42:37 AM

2 Replies
249 Views

(page loaded in 0.068 seconds)

Similiar Articles:













7/24/2012 11:24:01 AM


Reply: