[comp.unix.questions] Macro facility in csh?

charleen@zodiac.ADS.COM (Charleen Bunjiovianna) (09/02/89)

I may be asking for too much, but I write a lot of csh scripts and
it sure would be handy to have macro capability.  I don't find any
mention of it in the documentation, though.

Responses on the order of "Well, shoot, if you want that much
flexibility you should be using ______" welcome as well.

Charleen

If you put garbage in a computer nothing comes out but garbage.  But
this garbage, having passed through a very expensive machine, is
somehow enobled and none dare criticize it.

maart@cs.vu.nl (Maarten Litmaath) (09/02/89)

charleen@zodiac.ADS.COM (Charleen Bunjiovianna) writes:
\I may be asking for too much, but I write a lot of csh scripts and
\it sure would be handy to have macro capability.  I don't find any
\mention of it in the documentation, though.

There always has been a macro facility (!):

	set	foo = bar
	... $foo ... $foo ... $foo ...

Allright, you want macros with PARAMETERS: well, why don't you filter your
script through `/lib/cpp -P' before passing it to the shell?

If you would use *Bourne* shell scripts, you could even define shell FUNCTIONS:

	foo()
	{
		...
	}
	... foo ...

If you're stuck with an old version of the Bourne shell, try the following
trick:
	foo='
		case $1 in
		bar)
			date
			;;
		pipo)
			who
		esac
	'
	...
	set bar; eval "$foo"
	set pipo; eval "$foo"

In fact, this is the way functions were implemented at first: the shell would
set the arguments you gave and eval the function shell variable.
The problem: the (global) positional parameters get overwritten on each call,
making nested functions somewhat `tricky'...
-- 
C, the programming language that's the same |Maarten Litmaath @ VU Amsterdam:
         in all reference frames.           |maart@cs.vu.nl, mcvax!botter!maart

jonas@his.UUCP (Jonas Mellin) (09/04/89)

In article <8998@zodiac.ADS.COM> charleen@ADS.COM () writes:
>I may be asking for too much, but I write a lot of csh scripts and
>it sure would be handy to have macro capability.  I don't find any
>mention of it in the documentation, though.
>
>Responses on the order of "Well, shoot, if you want that much
>flexibility you should be using ______" welcome as well.
>
>Charleen

It is possible to use aliases for the purpose.

alias cd 'set old=$cwd; chdir \!*; pwd'
alias back 'set back=$old; cd $back; unset back'

Here is an example using it:
...> cd /tmp
/tmp
...> back
(whereever you were before)
...>

or

set pdl=
alias pd 'chdir \!:1;set pdl=($pdl $cwd); pwd'
alias ccd 'echo $pdl; echo -n index: ; set pdi=$<; chdir $pdl[$pdi]; pwd'
alias cdn 'chdir $pdl[\!:1]; pwd'
alias ud 'echo $pdl; echo -n index:; set pdi=$<; @ pdii=$pdi + 1;'\
         '@ pdi=$pdi - 1; set pdl=($pdl[-$pdi] $pdl[$pdii-]);'\
	 'echo $pdl; unset pdi pdii'
alias pdreset set pdl=

where

pd	registers a directory in the pdl
ccd	displays pdl first and prompts the user for an index. If an
	correct index is used it changes to the directory
cdn	change to the nth directory in the indexlist.
ud	removes a directory from pdl
pdreset	resets pdl to an empty list

Both of these examples are from the book 'An Introduction To Berkeley UNIX'.

I am more inclined to use Bourne Shell for shellscript programming.
    _____________________________________________
   /      /  /    /   /\  /    /      /| /|	|E-mail: jonas@his.se
  /      /--/	 /    \  /    /	     / |/ | 	|UUCP:   ...!sunic!his!jonas
 /      /  /	/    \/ / /  /      /     |	|Phone:  +46 500 77646
/______________________/  \_/ onas /      |ellin|Fax:    +46 500 16325
Snailmail: Jonas Mellin,Hogskolan i Skovde, Box 408, 541 28 Skovde, Sweden