www.faqs.org/faqs/unix-faq/shell/csh-whynot -> www.faqs.org/faqs/unix-faq/shell/csh-whynot/
I am continually shocked and dismayed to see people write test cases, install scripts, and other random hackery using the csh. The csh is seductive because the conditionals are more C-like, so the path of least resistance is chosen and a csh script is written. Sadly, this is a lost cause, and the programmer seldom even realizes it, even when they find that many simple things they wish to do range from cumbersome to impossible in the csh. FILE DESCRIPTORS The most common problem encountered in csh programming is that you can't do file-descriptor manipulation. All you are able to do is redirect stdin, or stdout, or dup stderr into stdout. Bourne-compatible shells offer you an abundance of more exotic possibilities. Writing Files In the Bourne shell, you can open or dup arbitrary file descriptors. Or what if you just want to throw away stderr and leave stdout alone? In the csh, you can only make a pitiful attempt like this: (cmd > /dev/tty) >& /dev/null But who said that stdout was my tty? Along these same lines, you can't direct error messages in csh scripts out stderr as is considered proper. In the Bourne shell, you might say: echo "$0: cannot find $file" 1>&2 but in the csh, you can't redirect stdout out stderr, so you end up doing something silly like this: sh -c 'echo "$0: cannot find $file" 1>&2' 1b. Reading Files In the csh, all you've got is $<, which reads a line from your tty. Tough noogies, you still get your tty, which you really can't redirect. Now, the read statement in the Bourne shell allows you to read from stdin, which catches redirection. It also means that you can do things like this: exec 3<file1 exec 4<file2 Now you can read from fd 3 and get lines from file1, or from file2 through fd 4. In modern, Bourne-like shells, this suffices: read some_var 0<&3 read another_var 0<&4 Although in older ones where read only goes from 0, you trick it: exec 5<&0 # save old stdin exec 0<&3; Closing FDs In the Bourne shell, you can close file descriptors you don't want open, like 2>&-, which isn't the same as redirecting it to /dev/null. More Elaborate Combinations Maybe you want to pipe stderr to a command and leave stdout alone. In a Bourne shell, you can do things like this: exec 3>&1; The closes there were in case something really cared about all its FDs. But if you want it from A, you're out of luck -- if you're in the csh, that is. In the Bourne shell, you can get it, although doing so is a bit tricky. Here's something I had to do where I ran dd's stderr into a grep -v pipe to get rid of the records in/out noise, but had to return the dd's exit status, not the grep's: device=/dev/rmt8 dd_noise='^ 0-9 +\+ 0-9 + records (in|out)$' exec 3>&1 status=((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; Even simple little things like this: % time | echo which while nonsensical, shouldn't give me this message: Reset tty pgrp from 9341 to 26678 Others are more fun: % sleep 1 | while while: Too few arguments.
White space can matter: if(expr) may fail on some versions of csh, while if (expr) works! Your vendor may have attempted to fix this bug, but odds are good that their csh still won't be able to handle if then if then echo A: got here else echo B: got here endif echo We should never execute this statement endif 3. SIGNALS In the csh, all you can do with signals is trap SIGINT. In the Bourne shell, you can trap any signal, or the end-of-program exit. For example, to blow away a tempfile on any of a variety of signals: $ trap 'rm -f /usr/adm/tmp/i$$ ; This makes it really hard to construct strings with mixed quotes in them. You have to use backslashes for newlines, and it's just darn hard to get them into strings sometimes. VARIABLE SYNTAX There's this big difference between global (environment) and local (shell) variables. In csh, you use a totally different syntax to set one from the other. In the Bourne shell, this VAR=foo cmds args is the same as (export VAR; Watch: echo Try testing with $SHELL:t It's really nice to be able to say ${PAGER-more} or FOO=${BAR:-${BAZ}} to be able to run the user's PAGER if set, and more otherwise. You can't get the process number of the last background command from the csh, something you might like to do if you're starting up several jobs in the background. The csh is also flaky about what it does when it imports an environment variable into a local shell variable, as it does with HOME, USER, PATH, and TERM. Consider this: % setenv TERM '/bin/ls -l / > /dev/tty' % csh -f And watch the fun! MANPAGER) setenv PAGER $MANPAGER Despite your attempts to only set PAGER when you want to, the csh aborts: MANPAGER: Undefined variable. That's because it parses the whole line anyway AND EVALUATES IT! X && $X == 'foo') echo ok X: Undefined variable This forces you to write a couple nested if statements. This is highly undesirable because it renders short-circuit booleans useless in situations like these. If the csh were the really C-like, you would expect to be able to safely employ this kind of logic. Consider the common C construct: if (p && p->member) Undefined variables are not fatal errors in the Bourne shell, so this issue does not arise there. While the csh does have built-in expression handling, it's not what you might think. This is an error @ a = 4/2 but this is ok @ a = 4 / 2 The ad hoc parsing csh employs fouls you up in other places as well. ERROR HANDLING Wouldn't it be nice to know you had an error in your script before you ran it? This is especially good to make sure seldom taken segments of code code are correct. Consider this statement: exit Of course, they really meant exit or just exit 1 Either shell will complain about this. If you have an alias with backquotes, and use that in backquotes in another one, you get a coredump. Here's another one: % mkdir tst % cd tst % touch ' foo bar' % foreach var ( * ) > echo "File named $var" > end foreach: No match. SUMMARY While some vendors have fixed some of the csh's bugs (the tcsh also does much better here), many have added new ones. Most of its problems can never be solved because they're not actually bugs per se, but rather the direct consequences of braindead design decisions. Do yourself a favor, and if you *have* to write a shell script, do it in the Bourne shell. The Korn shell is the preferred programming shell by many sh addicts, but it still suffers from inherent problems in the Bourne shell's design, such as parsing and evaluation horrors. The Korn shell or its public-domain clones and supersets (like bash) aren't quite so ubiquitous as sh, so it probably wouldn't be wise to write a sharchive in them that you post to the net. Until then, we'll be stuck with bug-incompatible versions of the sh lying about. The Plan 9 shell, rc, is much cleaner in its parsing and evaluation; If you don't have to use a shell, but just want an interpreted language, many other free possibilities present themselves, like Perl, REXX, TCL, Scheme, or Python. Of these, Perl is probably the most widely available on UNIX (and many other) systems and certainly comes with the most extensive UNIX interface. Increasing numbers vendors ship Perl with their standard systems. You can get at networking functions, binary data, and most of the C library. There are also translators to turn your sed and awk scripts into Perl scripts, as well as a symbolic debugger. Tchrist's personal rule of thumb is that if it's the size that fits in a Makefile, it gets written in the Bourne shell, but anything bigger gets written in Perl. He calls it 'csh' or the 'hamilton csh', but it's not a csh as it's neither bug nor feature compatible with the real csh. Actually, he's fixed a great deal, but in doing so, has created a totally different shell. Vote 16 Current Top-Rated FAQs Are you an expert in this area? Share your knowledge and earn expert points by giving answers or rating people's questions and answers! ORG is not sanctioned in any way by FAQ authors or maintainers. Questions strongly related to this FAQ: * 17 i want to get some exmaples of shell programming i mean the problems by Puneet (2/14/2004) * 18 hi all, i need to write a csh script in which i run a perl script.
|