[net.games] csh problem answers

sibley (05/16/82)

I got only two attempted solutions to the csh problems about
prompts with embedded newlines.  One, from Walt Morris,
BTL-MH, ...mhtsa!mh3bs!wsm, was actually a solution for the
older Bourne shell.  Thus, only one answer for the C-shell.
Since his explanations are concise and to the point, I'll just
duplicate them rather than write my own.  My comments appear
in [...]'s.  I have done a little editing on the answer, also.

dave
psuvax!sibley
---------------------------------------------------------------
Here's a good question about csh.  Suppose you want to set your
prompt to something like 'you found a cshell' instead of the
mundane '% '.  You decide this is a pretty long prompt so you
want the text followed by a newline and 2 spaces.

	1.  How do you do this directly?

% set prompt="you found a cshell\
  "
[The double quotes " could be single ' ]

	2.  How do you write an alias to do it? (A script won't work.
	    Why?)

% alias al 'set prompt="you found a cshell\\
  "'
[The double and single quotes could be in the other order.]

A script is executed in a subshell.  The subshell can have no effect
on the parent shell. [Actually, this can be done in a script
if it is invoked by 'source foo'.]

	3.  Suppose you have a file full of interesting prompt lines.
	    You also have an executable file 'newprompt' which picks
	    one of them at random and writes it to stdout.  Find an
	    alias that invokes 'newprompt' and sets your prompt to the
	    resulting line followed, as above, by a newline and 2
	    spaces.

% alias al 'set prompt="`newprompt`\\
  "'

	4.  Explain why your answers work, preferably with references
	    to the Berkeley manuals.  (I am mostly interested in how
	    the quotes and/or escapes you use work and why they are
	    necessary.)

The double quotes in the answer to problem one preserve the embedded
spaces.  The backslash in the same answer preserves the embedded newline.

In answer 2, the single quotes quote all the inside characters, including
the later-used double quotes.  The only characters that the single quote
does not quote is the backslash in front of a newline or history character
(like !), so that backslash must be quoted too.  (The first time I tried
that one, I only put one backslash there, and it botched.)
[Same here.  But my second try was with 3 backslashes, thinking
that it would be like % in printf formats.  But 2 is correct.]

In answer 3, the open quotes invoke the "newprompt" program each time the
alias is called in.  Otherwise, it is the same as answer 2.

I'm curious as to what your solutions were that were so "messy".  If
they were the same as mine, then I guess we just have a different opinion
of what is "messy".  I've kinda taken it for granted that quoting is
just a hassle to be accepted for what it is.
[Mine have the quotes in the other order.  Otherwise identical.
I just think 3 kinds of quotes and and 2 escapes make a mess.
I accept the hassle, but I don't like it.]

[Above answer from ]
Randal L. Schwartz
Tektronix Microcomputer Development Products
Beaverton, Oregon
[Unfortunately, I lost his net address.  Sorry.]