use of set in a shell script
Maarten Litmaath
maart at cs.vu.nl
Thu Jan 18 10:15:03 AEST 1990
In article <1197 at root44.co.uk>,
gwc at root.co.uk (Geoff Clare) writes:
\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.
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 at cs.vu.nl, uunet!mcsun!botter!maart
More information about the Comp.unix.questions
mailing list