🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
6 messages
6 total messages Started by Greg Miller Tue, 11 Sep 1990 20:05
Help needed with conditional statement for alias in csh
#1031
Author: Greg Miller
Date: Tue, 11 Sep 1990 20:05
27 lines
1488 bytes
Hi!

I have been having great problems in setting up an alias within csh.

I want to test for the presence of a file, and if it exists then
to execute some statements e.g.


if ( { test -s /usr/spool/mail/miller } ) then; echo "" ; echo "New mail" ;\
 echo "" ; endif


The above does not work because when executed the endif statement is not
reached.  The problem appears to be related to the brackets associated
with the expression in the condition.

Could somebody please tell me what I am doing wrong and how to do
this in a one line command that can be aliased?

Thanks!

  J G Miller                         telephone:  (519) 679-2111 extension 6325
  Room 024, Chemistry Building        InterNet:  <a4346@uwocc1.uwo.CA>
  University of Western Ontario       NetNorth:  <A4346@UWOCC1.BITNET>
  LONDON, Ontario, N6A 5B7            mailpath:  {uunet!watmath,utai}!ria!a4346

-------------------------------------------------------------------------------
Re: Help needed with conditional statement for alias in csh
#1034
Author: Greg Miller
Date: Tue, 11 Sep 1990 21:48
28 lines
1301 bytes
In article <6929.26ed0b53@uwovax.uwo.ca>, miller@uwovax.uwo.ca (Greg Miller) writes:
> Hi!
> 
> I have been having great problems in setting up an alias within csh.
> 
> I want to test for the presence of a file, and if it exists then
> to execute some statements e.g.
> 
> 
> if ( { test -s /usr/spool/mail/miller } ) then; echo "" ; echo "New mail" ;\
>  echo "" ; endif
> 

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.

If the file is present, the condition is met and the statements are
executed.

If the file is zero length, the condition is not true, then
the sequence of commands are not executed but the if statement
is not terminated.

And ideas on why this bizarre behavior and how to get it to
work properly?

J G Miller
Re: Help needed with conditional statement for alias in csh
#1140
Author: Chris Torek
Date: Fri, 14 Sep 1990 15:24
60 lines
2233 bytes
In article <6932.26ed237f@uwovax.uwo.ca> miller@uwovax.uwo.ca
(Greg Miller) writes:
>Followup-To: j g miller <miller@ria.ccs.uwo.ca>

(No wonder there have been no followups.  None of `j', `g', `miller', or
`<miller@ria.ccs.uwo.ca>' are valid newsgroup names....)

>if ( { test -s /usr/spool/mail/miller } ) then; echo "" ; echo "New mail" ;\
> echo "" ; endif

>If the file is present, the condition is met and the statements are
>executed.
>
>If the file is zero length, the condition is not true, then
>the sequence of commands are not executed but the if statement
>is not terminated.

Correct.

>Any ideas on why this bizarre behavior

This is the C shell we are talking about.  Do not expect anything
resembling sanity.

>and how to get it to work properly?

Do not use an alias.

The C shell's `parser' looks for a *line* whose first word is `else' or
`endif' after the C shell executes a false `if (expr) then'.  While
scanning for such a line it notes lines that have the form

	if<whitespace>.*<whitespace>then

and increments a `false if' counter.  You can thus stick all sorts of
syntactic trash between a false `if' and its corresponding `endif'; only
when the test in the `if' becomes true will the C shell actually notice
that the trash is indeed invalid.  For instance:

	if (0) then
		if (look, unclosed { and !funny characters then
		endif
		echo this does not get echoed
	endif
	echo but this does

It *is* possible to jam a newline into an alias, but if you do you will
quickly run afoul of other C shell bugs.  For instance:

	% alias t 'if (0) then\
		echo foo\
	endif\
	echo bar'
	% t
	? ? %

Nothing gets echoed.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
Re: Help needed with conditional statement for alias in csh
#1168
Author: Barton E. Schaefer
Date: Tue, 18 Sep 1990 16:47
69 lines
2754 bytes
In article <6929.26ed0b53@uwovax.uwo.ca> J G Miller <miller@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@uwovax.uwo.ca> he goes on:
} In article <6929.26ed0b53@uwovax.uwo.ca>, miller@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@cse.ogi.edu
Re: Help needed with conditional statement for alias in csh
#1172
Author: Per Hedeland
Date: Tue, 18 Sep 1990 22:36
29 lines
1832 bytes
In article <26553@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
>In article <6932.26ed237f@uwovax.uwo.ca> miller@uwovax.uwo.ca
>>if ( { test -s /usr/spool/mail/miller } ) then; echo "" ; echo "New mail" ;\
>> echo "" ; endif

>This is the C shell we are talking about.  Do not expect anything
>resembling sanity.

Quite. Following the eternal wisdom of the net, I have learned not only
"if you want to do a non-trivial csh script, use sh" but also "if you want
to do a non-trivial csh command, use sh". Thus, a working one-liner could be:

sh -c 'if test -s /usr/spool/mail/miller; then echo ""; echo "New mail"; echo ""; fi'

However, turning this into an alias with csh's idea of quoting is awkward if
at all possible, and you might just as well make it a script. Actually,
IMHO the best solution is to use the other conditional construct, that
happens to be common to sh and csh, and apparently unbroken in the latter:

test -s /usr/spool/mail/miller && (echo ""; echo "New mail"; echo "")

or as an alias:

alias mailtest 'test -s /usr/spool/mail/miller && (echo ""; echo "New mail"; echo "")'

--Per Hedeland
per@erix.ericsson.se  or
per%erix.ericsson.se@uunet.uu.net  or
...uunet!erix.ericsson.se!per
Re: Help needed with conditional statement for alias in csh
#1221
Author: Joseph Wells
Date: Sun, 23 Sep 1990 00:44
50 lines
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>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads