sh(1) question from a "quoting paranoiac" :-)
Maarten Litmaath
maart at cs.vu.nl
Fri Mar 9 05:29:41 AEST 1990
In article <2387 at rodan.acs.syr.edu>,
jdpeek at rodan.acs.syr.edu (Jerry Peek) writes:
)...
) last="`expr \"$*\" : '.* \(.*\)'`" # LAST ARGUMENT
) first="`expr \"$*\" : '\(.*\) .*'`" # ALL BUT LAST ARG
)
)Seems like, years ago, I needed to put doublequotes around the backquotes
)to protect any spaces from the shell. [...]
Indeed.
Let's assume the IFS characters are space, tab and newline, and no argument
contains such characters; then you could remove the outer double quotes.
Example:
$ set a b c
$ last=`expr "$*" : '.* \(.*\)'`
$ first=`expr "$*" : '\(.*\) .*'`
$ echo "=$first=$last="
=a b=c=
This should work too (more consistent, IMHO):
$ last=`expr "$*" : '.* \\(.*\\)'`
Right, let's try some funny arguments:
$ set a ' ' b
$ last=`expr "$*" : '.* \(.*\)'`
$ first=`expr "$*" : '\(.*\) .*'`
$ echo "=$first=$last="
=a =b=
$ /bin/echo `expr "$*" : '\(.*\) .*'` | od -a
0000000 a nl
0000002
Huh?! In an assignment the results of command substitution are NOT scanned
for IFS characters! (At least on SunOS 4.0.3c and 4.3BSD.) All the SunOS
manual has to say about assignments:
Keyword parameters (also known as variables) may be assigned
values by writing:
name=value [ name=value ] ...
Pattern-matching is not performed on value.
So:
$ a=*
$ echo "$a"
*
Anyway, here's an example showing the effect of an embedded space in the last
argument:
$ set a b c' 'd
$ last=`expr "$*" : '.* \(.*\)'`
$ first=`expr "$*" : '\(.*\) .*'`
$ echo "=$first=$last="
=a b c=d=
--
"Belfast: a sentimental journey to the Dark Ages - Crusades & Witchburning
- Europe's Lebanon - Book Now!" | maart at cs.vu.nl, uunet!mcsun!botter!maart
More information about the Comp.unix.questions
mailing list