[comp.unix.shell] quoting variables in csh - bug in certain implementation

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (03/01/91)

I have run into a bug with one implementation of csh that appears to be
related to variable quoting, or the lack thereof, but with a peculiar
side effect as well.  I would like comments, especially from those who
understand the internals of csh (I don't).

[text pasted in from other windows is indented with "> "]

On system 1 (the OK one) I get these expected results:

> phil@ux1:/cso/staff/phil 7>
> phil@ux1:/cso/staff/phil 7> set awk = 'BEGIN{w=h=d="?"}\
> /Width:/{w=$3}\
> /Height:/{h=$3}\
> /Depth:/{d=$3}\
> END{print "set width =",w,";set height =",h,";set depth =",d}'
> phil@ux1:/cso/staff/phil 8>
> phil@ux1:/cso/staff/phil 8> echo $awk
> Missing }.
> phil@ux1:/cso/staff/phil 9>
> phil@ux1:/cso/staff/phil 9> echo $awk:q
> BEGIN{w=h=d="?"}
> /Width:/{w=$3}
> /Height:/{h=$3}
> /Depth:/{d=$3}
> END{print "set width =",w,";set height =",h,";set depth =",d}
> phil@ux1:/cso/staff/phil 10>
> phil@ux1:/cso/staff/phil 10> echo "${awk:q}"
> BEGIN{w=h=d="?"
> /Width:/{w=$3
> /Height:/{h=$3
> /Depth:/{d=$3
> END{print "set width =",w,";set height =",h,";set depth =",d}
> phil@ux1:/cso/staff/phil 11>
> phil@ux1:/cso/staff/phil 11>


However on system 2 (the bad one) I get some strange results:

> pdh@spiff:/home/fsg/pdh 44>
> pdh@spiff:/home/fsg/pdh 44> set awk = 'BEGIN{w=h=d="?"}\
> /Width:/{w=$3}\
> /Height:/{h=$3}\
> /Depth:/{d=$3}\
> END{print "set width =",w,";set height =",h,";set depth =",d}'
> pdh@spiff:/home/fsg/pdh 45>
> pdh@spiff:/home/fsg/pdh 45> echo $awk
> Missing }.
> pdh@spiff:/home/fsg/pdh 46>
> pdh@spiff:/home/fsg/pdh 46> echo $awk:q
> Missing }.
> pdh@spiff:/home/fsg/pdh 47>
> pdh@spiff:/home/fsg/pdh 47> echo "${awk:q}"
> Unmatched ".
> pdh@spiff:/home/fsg/pdh 48>
> /Width:/{w=}?
> /Height:/{h=}?
> /Depth:/{d=}?
> END{print? ?"set? width? =?",w,?"?;set? height? =?",h,?"?;set? depth? =?",d}: Ambiguous.
> pdh@spiff:/home/fsg/pdh 49>
> pdh@spiff:/home/fsg/pdh 49>

Note that at prompt number 48 I simply pressed carriage return.  It appears to
me that it had for some reason (because newline characters are in the
variable?) it placed data in a buffer, or left it in a buffer incorrectly.

The portion of the output of the "set" command showing the contents of the
variable "awk" are:  [indented with "> "]

> awk     BEGIN{w=h=d="?"}
> /Width:/{w=$3}
> /Height:/{h=$3}
> /Depth:/{d=$3}
> END{print "set width =",w,";set height =",h,";set depth =",d}

So their are definitely some newlines in there.

I have been experimenting with another problem on the same system and noted
some effects related to the above that indicate a problem in the quoting.
In this other problem, backquotes work on "set" and fail on "setenv":

> pdh@spiff:/home/fsg/pdh 55> set date = `date`
> pdh@spiff:/home/fsg/pdh 56>
> pdh@spiff:/home/fsg/pdh 56> echo $date
> Thu Feb 28 14:53:44 EST 1991
> pdh@spiff:/home/fsg/pdh 57>
> pdh@spiff:/home/fsg/pdh 57> setenv DATE `date`
> `date`: Ambiguous.
> pdh@spiff:/home/fsg/pdh 58>
> pdh@spiff:/home/fsg/pdh 58> echo $DATE
> DATE: Undefined variable.
> pdh@spiff:/home/fsg/pdh 59>
> pdh@spiff:/home/fsg/pdh 59> setenv DATE $date:q
> setenv: Too many arguments.
> pdh@spiff:/home/fsg/pdh 60>
> pdh@spiff:/home/fsg/pdh 60> setenv DATE "${date:q}"
> pdh@spiff:/home/fsg/pdh 61>
> pdh@spiff:/home/fsg/pdh 61> echo $DATE
> 28 Thu Feb EST EST 1991
> pdh@spiff:/home/fsg/pdh 62>
> pdh@spiff:/home/fsg/pdh 62>

Note the result of the command at prompt 59.

Comments? Ideas? Suggestions? This is not a problem critical to me because
I use that system for nothing more than testing for just such portability
problems.  But of course finding ways to make programs work on more machines
by getting broken ones fixed, fixing my own programs, or just finding a
good workaround, is always desireable.

Thanks!
phil-howard@uiuc.edu
-- 

--Phil Howard, KA9WGN-- | Individual CHOICE is fundamental to a free society
<phil@ux1.cso.uiuc.edu> | no matter what the particular issue is all about.