Help needed with conditional statement for alias in csh
Barton E. Schaefer
schaefer at ogicse.ogi.edu
Wed Sep 19 02:47:47 AEST 1990
In article <6929.26ed0b53 at uwovax.uwo.ca> J G Miller <miller at ria.ccs.uwo.CA> writes:
}
} I have been having great problems in setting up an alias within csh.
}
} if ( { test -s /usr/spool/mail/miller } ) then; echo "" ; echo "New mail" ;\
} echo "" ; endif
}
} Could somebody please tell me what I am doing wrong and how to do
} this in a one line command that can be aliased?
In article <6932.26ed237f at uwovax.uwo.ca> he goes on:
} In article <6929.26ed0b53 at uwovax.uwo.ca>, miller at uwovax.uwo.ca (Greg Miller) writes:
}
} Further to my original post, I have since found that the problem is
} not associated with the brackets around the expression but is
} related to the execution of test.
Chris Torek has already explained that it is csh brain-damage, not a
problem with "the execution of test", that causes this.
} And ideas on why this bizarre behavior and how to get it to
} work properly?
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.
First problem: You want three lines of output, but you can't use three
echo commands. Solution (`%' is the csh prompt):
__________
% setenv NEW_MAIL_MESSAGE '\
New mail\
'
% printenv NEW_MAIL_MESSAGE
New mail
%
__________
So in the alias, you use:
if { test -s /usr/spool/mail/miller } printenv NEW_MAIL_MESSAGE
Note that the ( ) around the { } were redundant.
In general, the way to put conditionals into csh aliases is to use a
series of simple-if statements like the one above. Here's an example
from my .cshrc. The backslashes are for readability in this posting
only -- if you actually want to use this, you have to remove the
backslash-newline-tab sequences and make the pp alias into one long line.
Ah, the vagaries of csh ....
# Default pushd to home directory (like cd)
# if there is no top-of-stack to swap with.
alias pp 'set pd=(\!*:q);\
if ($#pd == 0) set pd=(`dirs` ~);\
if ($#pd > 1) shift pd;\
pushd $pd[1] >& /dev/null;\
if ($#pd > 1) popd +2 >& /dev/null;\
unset pd;\
dirs'
--
Bart Schaefer schaefer at cse.ogi.edu
More information about the Comp.unix.shell
mailing list