[net.unix-wizards] Help! Csh is eating my brain....

flory@zaphod.UUCP (Trevor Flory) (10/16/85)

Hello All;
	I'm trying to debug a csh script written by someone who
	knew what he was doing I'm sure.  Below is a fragment of
	the script which I find rather difficult to understand:
		...
		alias readandset 'echo -n \!:1 ; set \!:2 = $< ' 
		...
		readandset "Choice? " chvar
		...
		if("$chvar" == "quit") ....

	In particular I'd like to know what \!:1 or \!:2 means/does.

	Please note that I know the basics of csh but I'm no whiz so
	a full, step-by-step parse of how the above fragment is
	interpreted would not be out of order.  Thanks very much for
	your help.  Post or e-mail as you see fit.

-- 
Trevor K. Flory           UUCP: ...!ihnp4{!alberta}!sask!zaphod!flory
Develcon Electronics Ltd.             Saskatoon, Saskatchewan, CANADA

"... the play is the tragedy, `Man',
	And its hero the Conqueror Worm."
			Poe, c.1838

flory@zaphod.UUCP (Trevor Flory) (10/17/85)

In article <366@zaphod.UUCP> flory@zaphod.UUCP (that's me) writes:
>Hello All;
>	I'm trying to debug a csh script ....


Thanks to all who answered my Csh question.  Also thanks to all who
scorched my buns off with RTFM flames.
-- 
Trevor K. Flory           UUCP: ...!ihnp4{!alberta}!sask!zaphod!flory
Develcon Electronics Ltd.             Saskatoon, Saskatchewan, CANADA

"... the play is the tragedy, `Man',
	And its hero the Conqueror Worm."
			Poe, c.1838

karl@osu-eddie.UUCP (Karl Kleinpaste) (10/18/85)

> 	I'm trying to debug a csh script...
> 		...
> 		alias readandset 'echo -n \!:1 ; set \!:2 = $< ' 
> 		...
> 		readandset "Choice? " chvar
> 		...
> 		if("$chvar" == "quit") ....
> 	In particular I'd like to know what \!:1 or \!:2 means/does.

In csh, the alias command sets the aliased name to the value of the
other words in the line.  In this example, that's one long quoted
word, but that's OK.

Within the alias command, one can use standard csh history expansions
to parse the arguments to be given to the alias itself.  That is, the
alias, once expanded, is considered momentarily to be the previous com-
mand, which makes it available for history expansion.

In history references, the ! sequences introduce pieces of that event.
Specifically, anything of the form <history reference>:<number> refers
to the <number>th argument of the command specified by the <history
reference>.  The usage of ! by itself refers to the immediately
preceding event, i.e., the aliased command itself.  Thus, in the call
	readandset "Choice?" chvar
the string "Choice?" is the 1st argument, and chvar is the 2nd.  Hence,
when the entire command using the readandset alias is used, it creates
the following command line:
	echo -n "Choice?" ; set chvar = $<
which lets the user input a choice as variable chvar.

The reason that the ! was escaped as \! in the alias command itself was
to keep the alias command itself from referring to history events; it
introduced the literal character ! into the alias declaration, so that
it would be available during alias expansion to parse arguments.
-- 
Karl Kleinpaste