Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!mcsun!hp4nl!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re: use of set in a shell script Message-ID: <5119@solo5.cs.vu.nl> Date: 17 Jan 90 23:15:03 GMT References: <472@cpsolv.UUCP> <5060@solo9.cs.vu.nl> <1197@root44.co.uk> Reply-To: maart@cs.vu.nl (Maarten Litmaath) Organization: VU Informatika, Amsterdam, the Netherlands Lines: 77 In article <1197@root44.co.uk>, gwc@root.co.uk (Geoff Clare) writes: \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. A good attempt, Geoff, but there ... :-) \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. Wrong. I didn't do that for nothing, you know. See below. \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. [...] You're right on this one. In fact I meant to include a `-': eval set - $optv \ case $i in \ -*) \ optv="$optv '$i'" \... What if $i were -' (sic)? Ridiculous, I know, but we're talking foolproof here. The extra level of variables is one way to deal with nasty arguments, here's another: case $i in -*\'*) tmp=`echo x"$i" | sed -e 's/.//' -e "s/'/'\\\\\\\\''/g"` optv="$optv '$tmp'" ;; -*) optv="$optv '$i'" ;; esac BTW, I tested this under SunOS 4.0.3c. But wait a minute! There's another problem: what if $i contains C escape sequences and one is using that terrible System-V echo...? :-( Hmm, one could always use the portable echo hack: ----------8<----------8<----------8<----------8<----------8<---------- : 'portable echo hack: do not interpret backslash escape sequences' cat << EOF $* EOF -- What do the following have in common: access(2), SysV echo, O_NONDELAY? | Maarten Litmaath @ VU Amsterdam: maart@cs.vu.nl, uunet!mcsun!botter!maart