[Tcsh] here-document in if-then-else-endif

Christos Zoulas christos at zoulas.com
Sat Mar 21 18:48:47 UTC 2020


It is a bug, but known behavior. csh/tcsh don't use an LALR parser but scan for keywords.
This can lead to the behavior you encounter below. I.e. when tcsh finds that an if statement
that will not be executed, enters "skip mode" i.e. it ignores tokens until the next "else" or "endif"
keyword. Fixing this would require to completely rewrite the parser for the shell at which point
might as well, fix a host of other language misfeatures... Which will end up rewriting most of
the shell.

Best,

christos

> On Mar 20, 2020, at 8:07 AM, Michal Szymanski <msz at astrouw.edu.pl> wrote:
> 
> Hi
> 
> I have encountered strange behavior in here-docs placed inside "if"
> command. Consider:
> 
> #!/bin/sh
> echo DEFANGED.0
> exit
> #!/bin/tcsh -f
> set M=0
> if ( $M ) then
> cat <<EOF >! /tmp/here.out
> if (x>0)
>  y=1;
> else
>  y=2;
> EOF
> else
> cat <<EOF >! /tmp/here.out
> if (x<0)
>  y=1;
> else
>  y=2;
> EOF
> endif
> 
> Executing the above script results in:
> 
> y=2: Command not found.
> EOF: Command not found.
> 
> Seems that if $M is 0, tcsh does not recognize "cat << EOM" construct
> in the "if" part of the conditional and treats the "else" line inside
> the here-doc as a part of the script syntax.
> 
> Is that an expected behavior?
> 
> regards, Michal
> 
> PS. I have not noticed that TCSH mailing list moved to a new address
> (from mx.gw.com) until today. Apparently the old list subscribers were
> not transferred automatically.
> 
> --
>  Michal Szymanski (msz at astrouw dot edu dot pl)
>  Warsaw University Observatory, Warszawa, POLAND
> --
> Tcsh mailing list
> Tcsh at astron.com
> https://mailman.astron.com/mailman/listinfo/tcsh
> 
> 
> --
> This message has been 'sanitized'.  This means that potentially
> dangerous content has been rewritten or removed.  The following
> log describes which actions were taken.
> 
> Sanitizer (start="1584706495"):
>  SanitizeFile (filename="unnamed.txt", mimetype="text/plain"):
>    Match (names="unnamed.txt", rule="9"):
>      Enforced policy: accept
> 
>  Defanged UNIX shell script(s).
>  Total modifications so far: 1
> 
> 
> Anomy 0.0.0 : Sanitizer.pm
> $Id: Sanitizer.pm,v 1.94 2006/01/02 16:43:10 bre Exp $

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 235 bytes
Desc: Message signed with OpenPGP
URL: <https://mailman.astron.com/pipermail/tcsh/attachments/20200321/ebce7adb/attachment.asc>


More information about the Tcsh mailing list