Why doesn't this work ?

Maarten Litmaath maart at cs.vu.nl
Thu Jun 15 09:05:43 AEST 1989


jon at fdmetd.uucp (Jon Ivar Tr|stheim) writes:
\Why does not the following command work in a Bourne shell script ?
\
\	USERID=`expr "`id`" : 'uid=\([0-9]*\)(.*'`
               ^      ^  ^                       ^
	       |      |  |                       |
	       +------+  +-----------------------+

1) As shown above, sh doesn't parse the expression the way you intended.
   I consider this a bug.
   Anyhow another bug is revealed: the first command between backquotes
   doesn't generate an error message.
2) The output of `id' is something like:

	uid=0(root) gid=0(wheel)
	           ^
		   This space splits the output into TWO words,
		   which `expr' isn't too happy with.
Solution:

	USERID=`expr "\`id\`" : 'uid=\([0-9]*\)(.*'`
-- 
"I HATE arbitrary limits, especially when |Maarten Litmaath @ VU Amsterdam:
   they're small."  (Stephen Savitzky)    |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list