use of set in a shell script

Geoff Clare gwc at root.co.uk
Wed Jan 17 01:06:32 AEST 1990


In article <5060 at solo9.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
>optc=0
>optv=
>
>for i
>do
>	case $i in
>	-*)
>		optc=`expr $optc + 1`
>		eval optv$optc='"$i"'
>		optv="$optv \"\$optv$optc\""
>		;;
>	*)
>		# you get the idea
>	esac
>done
>
>eval set $optv		# restore the options EXACTLY

A good attempt, Maarten, but there are a couple of big problems here.  

Firstly, the use of "expr" will be extremely slow for shells which don't
have "expr" built in (virtually all Bourne shells, I think).  There's no
need to use a separate variable for each argument, anyway.

Secondly, the final "set" command will not work correctly.  Suppose at
the start of the script $1 contains "-x".  This will end up as a
"set -x" command, which will turn on tracing mode in the shell, not
place "-x" in $1.  With some shells you can use "--" in a "set" command
to mark the end of the options, but a dummy first argument is more
portable.

Try this modified version:

optv=

for i
do
	case $i in
	-*)
		optv="$optv '$i'"
		;;
	*)
		# you get the idea
	esac
done

eval set X "$optv"; shift		# restore the options EXACTLY


-- 
Geoff Clare, UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc at root.co.uk  (Dumb mailers: ...!uunet!root.co.uk!gwc)  Tel: +44-1-315-6600



More information about the Comp.unix.questions mailing list