csh: if-then-else-endif in csh alias?

csh: if-then-else-endif in csh alias?

Post by Dave Bro » Fri, 20 Oct 1995 04:00:00




: I am converting some Korn Shell scripts to Csh.

Leave them in ksh.  Especially if they work.

: The Korn Shell scripts have some simple shell functions that use
: if-then-else-fi control structures in them. Is there no way to do
: this with a Csh alias? I know that you can do plain one-line if
: constructs in a Csh alias, but the bug in the pudding comes when
: I try to do the then-else-endif.

That's because you can't do one-line if-then-else-endif constructs in
csh.  You just can't.  It's impossible.  Same goes for foreaches.

Don't write shell scripts in csh.  It's broken.  Really.  It should've
never gotten out in the first place.  There's a detailed FAQ.

--Dave

 
 
 

csh: if-then-else-endif in csh alias?

Post by Daniel Zepe » Fri, 20 Oct 1995 04:00:00


Quote:>Leave them in ksh.  Especially if they work.

Believe me I would if I could. What they are are scripts that set up a fancy
developer's environment. I originally wrote them for myself in Korn Shell, and
then was asked to re-write them in Csh so that the rest of the department could
benefit from them. I'd like to * the person that started this
department on the Csh... Because now they don't want to change.

Quote:>: The Korn Shell scripts have some simple shell functions that use
>: if-then-else-fi control structures in them. Is there no way to do
>: this with a Csh alias? I know that you can do plain one-line if
>: constructs in a Csh alias, but the bug in the pudding comes when
>: I try to do the then-else-endif.
>That's because you can't do one-line if-then-else-endif constructs in
>csh.  You just can't.  It's impossible.  Same goes for foreaches.

I had a feeling that was the case, but didn't know for sure. That's why I
asked. I wanted someone to tell me for sure, "hey forget it, can't happen"
before I blew it off. I have implemented what I wanted the alias to do in a
truly ugly fashion by using a if clause to check the condition for each of the
command statements.

Quote:>Don't write shell scripts in csh.  It's broken.  Really.  It should've
>never gotten out in the first place.  There's a detailed FAQ.

I don't. I hate the Csh. I convert people and scripts to Korn Shell (bash
outside of work) whenever possible. I know about the deficiences of the Csh and
use the FAQ to convert people whenever possible. I haven't written a Csh script
in years.

Quote:>--Dave

Thanks for your help.

Dan

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Daniel Paul Zepeda
Western Geophysical
Applied Technology Software - Testing


 
 
 

csh: if-then-else-endif in csh alias?

Post by Dave Bro » Mon, 23 Oct 1995 04:00:00




: >Don't write shell scripts in csh.  It's broken.  Really.  It should've
: >never gotten out in the first place.  There's a detailed FAQ.
:
: This is surprising news. Having written many successful shell scripts in
: csh, and certainly preferring the use of csh over the Bourne shell
: (although the Korn Shell does do many of the things that the csh does),
: it amazes me that the scripts that I wrote shouldn't have worked.

To turn the question around, why do you prefer the csh over the bourne
shells?

: What are your specific objections to csh? What do you mean by broken?

It's inconsistent, for one thing.  It doesn't deal properly with quoted
strings, and it doesn't give you control over file descripters--you
can't (say) keep stdout and send stderr to /dev/null.

I could go on for ages, but somebody already went to that trouble for
me.  There are (of course) things he missed, but he covered the biggest
things.

: Don't you mean that you gained expertise on another shell first, and are
: now loath to abandon it?

I gained expertise on the C shell, then moved to the tcsh, and now use
bash and zsh in about equal parts.

: Remember, too, that a person can very well elect to use csh for
: interactive use, but use something else (ksh) for scripts.

Csh also sucks for interactive use.  The only halfway nice service it
provides is bang history, and both zsh and bash have that.

: An itemized list of objections to csh would be interesting.

How about the frequent posting I mentioned warning people away from
using it?  Look at the "Csh programming considered harmful" FAQ at
ftp://news.uu.net/usenet/news.answers/unix-faq/shell/csh-whynot.

--Dave

 
 
 

csh: if-then-else-endif in csh alias?

Post by Arved Sandstr » Mon, 23 Oct 1995 04:00:00





>: I am converting some Korn Shell scripts to Csh.

>Leave them in ksh.  Especially if they work.

>: The Korn Shell scripts have some simple shell functions that use
>: if-then-else-fi control structures in them. Is there no way to do
>: this with a Csh alias? I know that you can do plain one-line if
>: constructs in a Csh alias, but the bug in the pudding comes when
>: I try to do the then-else-endif.

>That's because you can't do one-line if-then-else-endif constructs in
>csh.  You just can't.  It's impossible.  Same goes for foreaches.

>Don't write shell scripts in csh.  It's broken.  Really.  It should've
>never gotten out in the first place.  There's a detailed FAQ.

>--Dave

This is surprising news. Having written many successful shell scripts in
csh, and certainly preferring the use of csh over the Bourne shell
(although the Korn Shell does do many of the things that the csh does),
it amazes me that the scripts that I wrote shouldn't have worked.

Incidentally, I don't try to do _all_ my programming in a shell. I use a
shell script as a wrapper for perl or n/awk scripts, or other commands,
which do actual data processing. In some cases, the shell scripts
themselves are embedded in other scripts written in other languages.
When I look at all the shell/UNIX tool/language newsgroups, it's
interesting to see how many people try to do everything with one thing.

What are your specific objections to csh? What do you mean by broken?
Don't you mean that you gained expertise on another shell first, and are
now loath to abandon it? Remember, too, that a person can very well
elect to use csh for interactive use, but use something else (ksh) for
scripts.

An itemized list of objections to csh would be interesting.
--
Arved H. Sandstro"m                       |     YISDER ZOMENIMOR
Physical Oceanography Group               |     ORZIZZAZIZ
Dept.of Physics, Memorial Univ. of NFLD   |     ZANZERIZ

 
 
 

csh: if-then-else-endif in csh alias?

Post by Daniel Zepe » Tue, 24 Oct 1995 04:00:00



>: >Don't write shell scripts in csh.  It's broken.  Really.  It should've
>: >never gotten out in the first place.  There's a detailed FAQ.
>:
>: This is surprising news. Having written many successful shell scripts in
>: csh, and certainly preferring the use of csh over the Bourne shell
>: (although the Korn Shell does do many of the things that the csh does),
>: it amazes me that the scripts that I wrote shouldn't have worked.

Csh is great for simple wrappers. That's about it.

Quote:>To turn the question around, why do you prefer the csh over the bourne
>shells?
>: What are your specific objections to csh? What do you mean by broken?
>It's inconsistent, for one thing.  It doesn't deal properly with quoted
>strings, and it doesn't give you control over file descripters--you
>can't (say) keep stdout and send stderr to /dev/null.
>I could go on for ages, but somebody already went to that trouble for
>me.  There are (of course) things he missed, but he covered the biggest
>things.

One of my biggest peeves is the variable substition/editing stinks.

Quote:>: Don't you mean that you gained expertise on another shell first, and are
>: now loath to abandon it?
>I gained expertise on the C shell, then moved to the tcsh, and now use
>bash and zsh in about equal parts.

You are quite wrong. Csh was my first shell. I became proficient at it, found
there were many short-comings, switche tdo Korn Shell, and haven't looked back
(Unless forced to.) I Use Korn and Bash exclusively.

Quote:>: Remember, too, that a person can very well elect to use csh for
>: interactive use, but use something else (ksh) for scripts.
>Csh also sucks for interactive use.  The only halfway nice service it
>provides is bang history, and both zsh and bash have that.

I second Dave's comment.

Quote:>: An itemized list of objections to csh would be interesting.
>How about the frequent posting I mentioned warning people away from
>using it?  Look at the "Csh programming considered harmful" FAQ at
>ftp://news.uu.net/usenet/news.answers/unix-faq/shell/csh-whynot.

Here Here, Well said Dave.

Quote:>--Dave

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Daniel Paul Zepeda
Western Geophysical
Applied Technology Software - Testing

 
 
 

csh: if-then-else-endif in csh alias?

Post by Michael Sternbe » Wed, 25 Oct 1995 04:00:00




>>Don't write shell scripts in csh.  It's broken.  Really.  It should've
>>never gotten out in the first place.  There's a detailed FAQ.
>Don't you mean that you gained expertise on another shell first, and are
>now loath to abandon it?

Careful with your tone here.  There are people out there who may have
done more [complicated] scripting on the edge and know the business.

Quote:>An itemized list of objections to csh would be interesting.

The FAQ Dave refers to is Tom Christiansen's "Csh Programming Considered
Harmful" periodic posting in this and other groups.  It says:

| The following periodic article answers in excruciating detail
| the frequently asked question "Why shouldn't I program in csh?".
| It is available for anon FTP from perl.com in /pub/perl/versus/csh-whynot.gz

Regards,
--

Case Western Reserve University         Lab phone: (216) 368 4034
"Who disturrrbs me at this time?"     << Zaphod Beeblebrox IV >>

 
 
 

csh: if-then-else-endif in csh alias?

Post by Brian S Hile » Wed, 25 Oct 1995 04:00:00


I agree with the expressed opinions regarding csh above; it *is* a brain-
damaged shell which, worst of all, poorly implements even its faulty design
decisions. However... the answer to your question is to be found in the
O'Reilly and Associates book "Unix Power Tools" (J. Peek, T. O'Reilly, M.
Loukides) pp183-4. I partially quote:

"The C shell's brain damage (sic!) keeps you from using an if with an else
in an alias.... Or that's what I thought until I saw... a trick: use enough
backslashes (\) and the eval command.

"Follow this example and you'll be fine:

# COMPILE AND CHMOD C PROGRAMS; DON'T USE .c ON END OF FILENAME.
alias C 'eval "if (\!* =~ *.c) then \\
        echo "C quitting: no .c on end of \!* please." \\
else \\
        if (-e \!*) mv \!* \!*.old \\
        echo \!*.c SENT TO cc \\
        cc -s \!*.c -o \!* \\
        if (-e \!*) chmod 311 \!* \\
endif"'

In addition, you may be interested to know an alias shown in the same
book on page 185, which itself turns quoted text into appropriately
quoted aliases:

alias quote "/bin/sed -e 's/\\!/\\\\\!/g' \\
        -e 's/'\\''/'\\'\\\\\\'\\''/g' \\
        -e 's/^/'''/' -e 's/"\$"/'''/'"

alias makealias "quote | /bin/sed 's/^alias \!:1 /' \!:2*"

The above aliases and other arcane csh triviata can be obtained elect-
ronically by anonymous ftp to ftp.ora.com:/published/oreilly/power_tools/
unix/upt.oct93.tar.Z
extract file "csh_init".

Oh, and by the way, if you ever purchase the book, be sure to read the
article therein in chapter 49.02 entitled "C Shell Programming Considered
Harmful" by Tom Christiansen.

Hope this helps.... (let me know by e-mail?)

 
 
 

csh: if-then-else-endif in csh alias?

Post by Randal L. Schwar » Fri, 27 Oct 1995 04:00:00


Michel> You can use && and || instead, too achieve the same result:

Michel> kafka[510]$ csh
Michel> kafka:~[1]%alias truefalse '\!* && echo True || echo False'
Michel> kafka:~[2]%truefalse true
Michel> True
Michel> kafka:~[3]%truefalse false
Michel> False
Michel> kafka:~[4]%

No no no.  I keep seeing this solution come up.  It *breaks* if the
middle "true-part" exits with a non-zero status.  It's starting to get
around the net faster than Craig Shergold.

Watch:

        % true && (echo true; false) || echo false
        true
        false

Don't use this.  Forget you've ever seen it.  Stop.  Cease.  Desist. :-)

Just another Unix hacker,--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me