tcsh, csh, sh scripts

tcsh, csh, sh scripts

Post by Luis Bernard » Fri, 06 Nov 1998 04:00:00



Hello.
I would appreciate some help with my script. I spent a great part of
yesterday looking for old posts on dejanews and although I found people
with similar problems I didn't find an answer. The FAQs were of no help
either.

I read that csh scripting is bad. What about tcsh scripting? My problem is
that my script only works with tcsh. It doesn't work with either sh or
csh. So if you know the reason why they don't work I would like to hear
from you.

Below I show a session with the three scripts. Only the first line (#!...)
is different between them. Also, I think that if the first line of the
script has only a # then the script runs with the shell I am using. Is
that correct? That's my conclusion after I switch between shells and run
the script with just # on the first line.

Note: the password was edited for obvious reasons.
The name of the script is well.(shell).
-----
rmds03 % cat well.tcsh
#! /usr/local/bin/tcsh
switch($1)
case "1":
    ftp  -vn  <<END > $5
    open   $2 2121
    user $3 $4
    bye
    END
    breaksw
case "2":
        echo Case 2
        breaksw
default:
        echo Done.
endsw
rmds03 % well.tcsh 1 rmds01 starreco XXXXXXXXX file
rmds03 % cat file
Connected to rmds01.rhic.bnl.gov.
220 rmds01 FTP server (Version PFTPD.1 Fri Jul 24 10:27:52 EDT 1998)
ready.
331 Password required for starreco.
230 User starreco logged in.
221 Goodbye.
rmds03 % cat well.csh
#! /bin/csh
switch($1)
case "1":
    ftp  -vn  <<END > $5
    open   $2 2121
    user $3 $4
    bye
    END
    breaksw
case "2":
        echo Case 2
        breaksw
default:
        echo Done.
endsw
rmds03 % well.csh 1 rmds01 starreco XXXXXXXXX file
END: << terminator not found
rmds03 % cat well.sh
#! /bin/sh
switch($1)
case "1":
    ftp  -vn  <<END > $5
    open   $2 2121
    user $3 $4
    bye
    END
    breaksw
case "2":
        echo Case 2
        breaksw
default:
        echo Done.
endsw
rmds03 % well.sh 1 rmds01 starreco XXXXXXXXX file
well.sh: syntax error at line 2: `switch' unexpected
rmds03 % cat well
#
switch($1)
case "1":
    ftp  -vn  <<END > $5
    open   $2 2121
    user $3 $4
    bye
    END
    breaksw
case "2":
        echo Case 2
        breaksw
default:
        echo Done.
endsw
rmds03 % csh
%m %% well 1 rmds01 starreco XXXXXXXX file
END: << terminator not found
%m %% sh
$ well 1 rmds01 starreco XXXXXXXXX file
well: syntax error at line 2: `switch' unexpected
$ tcsh
rmds03 % well 1 rmds01 starreco XXXXXXXXX file
rmds03 % cat file
Connected to rmds01.rhic.bnl.gov.
220 rmds01 FTP server (Version PFTPD.1 Fri Jul 24 10:27:52 EDT 1998)
ready.
331 Password required for starreco.
230 User starreco logged in.
221 Goodbye.
-----------------------------

Thanks,
Luis

P.S. I think that spaces around the token END are not the source of the
problem because, first they are not there (I think...) and second
well.tcsh runs without problems.

 
 
 

tcsh, csh, sh scripts

Post by Icarus Spar » Sat, 07 Nov 1998 04:00:00



Quote:>I read that csh scripting is bad. What about tcsh scripting? My problem is
>that my script only works with tcsh. It doesn't work with either sh or
>csh. So if you know the reason why they don't work I would like to hear
>from you.

'tsch' scripting is slightly better than 'csh' scripting, as some of the
bugs in csh have been removed. However it still lacks power and facilities
which most experienced shell scriptors consider essential.

Quote:>Below I show a session with the three scripts. Only the first line (#!...)
>is different between them. Also, I think that if the first line of the
>script has only a # then the script runs with the shell I am using. Is
>that correct? That's my conclusion after I switch between shells and run
>the script with just # on the first line.

It depends on the shell you are currently using, so the general answer is 'no'.

Quote:>rmds03 % cat well.csh
>#! /bin/csh
>switch($1)
>case "1":
>    ftp  -vn  <<END > $5
>    open   $2 2121
>    user $3 $4
>    bye
>    END

The word 'END' here has to be at the beginning of the line. One could argue
that it is a bug in tcsh that it works there. Our 'tcsh' manual says
     << word Read the shell input up to a line which is identical
             to word.
and '   END' is not identical to 'END'.

Quote:>    breaksw
>case "2":
>        echo Case 2
>        breaksw
>default:
>        echo Done.
>endsw
>rmds03 % cat well.sh
>#! /bin/sh
>switch($1)

'sh' and 'csh' have VERY different commands. A correct version of your program
would look like

#!/bin/sh
case $1 in
        1)
                ftp  -vn  <<-END > $5
                open   $2 2121
                user $3 $4
                bye
                END
        ;;
        2) echo Case 2 ;;
        *) echo Done. ;;
esac

Icarus

 
 
 

tcsh, csh, sh scripts

Post by Bruce Barnet » Sat, 07 Nov 1998 04:00:00



> I read that csh scripting is bad. What about tcsh scripting? My problem is
> that my script only works with tcsh. It doesn't work with either sh or
> csh. So if you know the reason why they don't work I would like to hear
> from you.

TCsh has fixed some of the ad hoc parsing bugs, and some other bugs,
but many of the flaws in csh/tcsh are in the language itself.

Here is my short list of the major flaws with csh/tcsh

        1) Ad Hoc Parser - full of surprises
        2) Multiple-line quotes difficult - hard to include awk/sed scripts
        3) Quoting can be confusing, inconsistent
        4) if/while/for loops cannot use <
        5) Limited file I/O redirection
        6) Difficult to read a line at a time, and to choose `tty` vs. stdin
        7) Signal management are not flexible, difficult to manage subprocesses
        8) Fewer ways to test for missing variables
        9) Can't pass spaces in arguments, spaces can disappear
        10) No way to disable the field separator
        11) Bigger and slower that sh
        12) No functions - aliases are hard to make multiple-lines

Can anyone add any other major flaws to this list? There are lots of
nit-picky things caused by #1, and that tcsh (and some vendors) have
fixed. But I tried to list all of the major problems...

Quote:> Below I show a session with the three scripts.
> P.S. I think that spaces around the token END are not the source of the
> problem because, first they are not there (I think...) and second
> well.tcsh runs without problems.

Well, I just tried it on a version of csh I have here. Any extra
spaces before the second END caused the script to fail with
"terminator not found."

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)