Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!ukc!stc!root44!gwc From: gwc@root.co.uk (Geoff Clare) Newsgroups: comp.unix.questions Subject: Re: use of set in a shell script Message-ID: <1197@root44.co.uk> Date: 16 Jan 90 14:06:32 GMT References: <472@cpsolv.UUCP> <5060@solo9.cs.vu.nl> Reply-To: gwc@root.co.uk (Geoff Clare) Organization: UniSoft Ltd, London, England Lines: 53 In article <5060@solo9.cs.vu.nl> maart@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@root.co.uk (Dumb mailers: ...!uunet!root.co.uk!gwc) Tel: +44-1-315-6600