Help needed with conditional statement for alias in csh
Joseph Wells
jbw at bucsf.bu.edu
Sun Sep 23 10:44:05 AEST 1990
In <12211 at ogicse.ogi.edu> schaefer at ogicse.ogi.edu (Barton E. Schaefer) writes:
Don't use any "then", "else" or "endif". The csh "if" syntax also allows
if condition simple-command
where simple-command can't be a parenthesized subshell or control
statement (while, foreach) of any kind, and can't contain separators
e.g. `|' or `;' are out.
[stuff deleted]
In general, the way to put conditionals into csh aliases is to use a
series of simple-if statements like the one above.
Actually, you can get the complete power of the if-then-else-endif
statement in a csh alias by using the "simple if" in combination with the
|| and && operators. You can translate this pseudo-code (not csh)
statement:
if "condition" then "statement1" else "statement2" endif
into this csh statement which will work inside an alias:
if "condition" set status=1 && "statement2" || "statement1"
or this one:
if (! "condition") set status=1 && "statement1" || "statement2"
As an example, here is a simple alias for listing the command history in
different ways:
alias h \
" if ('\!*' != '') set status=1 && hi \\
|| if ('\!*' !~ [1-9]*) set status=1 && hi -\!* \\
|| history | grep -i '\!*' | tail"
alias hi 'history | grep ............ | tail'
If you just type "h", it lists the last ten non-trivial history entries.
If you type "h 3", it lists the last three such entries. If you type
"h ls", it lists the last 10 history entries that contain the string "ls".
This technique won't work correctly if the "condition" has a side effect
that sets "status" to be non-zero.
Enjoy,
--
Joe Wells <jbw at bu.edu>
More information about the Comp.unix.shell
mailing list