C-shell alias: is it possible to have optional arguments with default

Gerald Justice justice at dao.nrc.ca
Fri Aug 4 06:08:40 AEST 1989


Here is a non-urgent problem that I am having in trying to extend my C-shell
knowledge.  I have two aliases that allow me to do editing of the previous
command or commands.  One is really a special case of the other but in order
to write a generalized alias I need to know how to allow for an optional
argument to an alias--if there is no argument I want to use the value 1.

I am also interested in having suggestions for improvement, after all this
is a simple concept but my implementation invokes a long list of programs.

----cut here-----REMEMBER TO REJOIN THE ALIAS DEFINITION LINES-----cut here----

# history editing aliases:
# cedit takes no arguments, it places the last command into two files and
# starts ex (open mode) on one of them.  If the two files are the same after
# ex exits then do nothing otherwise read the edited file back into the
# history list then execute the commands in the file.
# mcedit is similar, but works on the last N commands (specified by a single
# argument) and uses vi rather than ex.
# N.B. each of the alias definitions must occur on a single line.

alias cedit 'history -h 2 | head -1 | tee ~/ed-in >! ~/ed-out; ex +o ~/ed-out; 
cmp ~/ed-{in,out} >/dev/null || source -h ~/ed-out && cat ~/ed-out && 
source ~/ed-out'

alias mcedit '@ count=\!:1; @ hcount=$count + 1; history -h $hcount | 
head -$count | tee ~/ed-in >! ~/ed-out; unset count hcount; vi ~/ed-out; 
cmp ~/ed-{in,out} >/dev/null || source -h ~/ed-out && cat ~/ed-out && 
source ~/ed-out'
 =-=-=-=-SOFTCOPY-=-=-=-=-=-=-=-|-=-=-HARDCOPY-=-=-=-=-=-=-=
Phone:     (604) 388-0055       | Fax:    (604) 388-0045
Internet:  justice at dao.nrc.ca   | Telex:  049-7295
      or:  justice at 134.87.150.7	| Mail:   Dominion Astrophysical Observatory
BITNET:    justice at nrcdao       |         5071 W. Saanich Road
VAX PSI:   68100434::justice    |         Victoria, B.C.
SPAN,UUCP: (not yet available)  |         CANADA  V8X 4M6



More information about the Comp.unix.wizards mailing list