Article View: comp.unix.shell
Article #1221Re: Help needed with conditional statement for alias in csh
From: Joseph Wells
Date: Sun, 23 Sep 1990 00:44
Date: Sun, 23 Sep 1990 00:44
50 lines
2272 bytes
2272 bytes
In <12211@ogicse.ogi.edu> schaefer@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@bu.edu>
Technical Details
Message-ID:
<JBW.90Sep22204405@bucsf.bu.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uunet!bu.edu!nntp-read!jbwReferences:
<6929.26ed0b53@uwovax.uwo.ca> <6932.26ed237f@uwovax.uwo.ca> <12211@ogicse.ogi.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uunet!bu.edu!nntp-read!jbw From: jbw@bucsf.bu.edu (Joseph Wells) Newsgroups: comp.unix.shell Subject: Re: Help needed with conditional statement for alias in csh Message-ID: <JBW.90Sep22204405@bucsf.bu.edu> Date: 23 Sep 90 00:44:05 GMT References: <6929.26ed0b53@uwovax.uwo.ca> <6932.26ed237f@uwovax.uwo.ca> <12211@ogicse.ogi.edu> Sender: news@bu.edu.bu.edu Organization: Boston University Computer Science Department Lines: 50 In-reply-to: schaefer@ogicse.ogi.edu's message of 18 Sep 90 16:47:47 GMT