home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / textfile / faqs / unix_faq / shell / csh_whyn next >
Encoding:
Internet Message Format  |  1992-12-27  |  13.9 KB

  1. Xref: bloom-picayune.mit.edu comp.unix.shell:8091 comp.unix.questions:50556 news.answers:4264
  2. Newsgroups: comp.unix.shell,comp.unix.questions,news.answers
  3. Path: bloom-picayune.mit.edu!enterpoop.mit.edu!usc!wupost!news.utdallas.edu!convex!tchrist
  4. From: Tom Christiansen <tchrist@convex.COM>
  5. Subject: Csh Programming Considered Harmful 
  6. Originator: tchrist@pixel.convex.com
  7. Sender: usenet@news.eng.convex.com (news access account)
  8. Message-ID: <1992Nov30.123449.8118@news.eng.convex.com>
  9. Approved: news-answers-request@MIT.Edu
  10. Date: Mon, 30 Nov 1992 12:34:49 GMT
  11. Expires: Mon, 4 Jan 1993 12:00:00 GMT
  12. Nntp-Posting-Host: pixel.convex.com
  13. Organization: Convex Computer Corporation, Colorado Springs, CO
  14. Followup-To: comp.unix.shell
  15. X-Disclaimer: This message was written by a user at CONVEX Computer
  16.               Corp. The opinions expressed are those of the user and
  17.               not necessarily those of CONVEX.
  18. Lines: 496
  19.  
  20. Archive-name: unix-faq/shell/csh-whynot
  21. Version: $Id: csh-faq,v 1.2 92/11/30 05:26:37 tchrist Exp Locker: tchrist $
  22.  
  23. The following periodic article answers in excruciating detail
  24. the frequently asked question "Why shouldn't I program in csh?".
  25. It is available for anon FTP from convex.com in /pub/csh.whynot .
  26.  
  27.  
  28.            Csh Programming Considered Harmful
  29.  
  30. Resolved: The csh is a tool utterly inadequate for programming, and
  31. its use for such purposes should be strictly banned.
  32.  
  33. I am continually shocked and dismayed to see people write test cases,
  34. install scripts, and other random hackery using the csh.  Lack of
  35. proficiency in the Bourne shell has been known to cause errors in /etc/rc
  36. and .cronrc files, which is a problem, because you *must* write these files
  37. in that language.
  38.  
  39. The csh is seductive because the conditionals are more C-like, so the path
  40. of least resistance if chosen and a csh script is written.  Sadly, this is
  41. a lost cause, and the programmer seldom even realizes it, even when they
  42. find that many simple things they wish to do range from cumbersome to
  43. impossible in the csh.
  44.  
  45.  
  46. FILE DESCRIPTORS
  47.  
  48. The most common problem encountered in csh programming is that
  49. you can't do file-descriptor manipulation.  All you are able to 
  50. do is redirect stdin, or stdout, or dup stderr into stdout. 
  51. Bourne-compatible shells offer you an abundance of more exotic
  52. possibilities.    
  53.  
  54. Writing Files
  55.  
  56. In the Bourne shell, you can open or dup random file descriptors.
  57. For example, 
  58.  
  59.     exec 2>errs.out
  60.  
  61. means that from then on, stderr goes into errs file.
  62.  
  63. Or what if you just want to throw away stderr and leave stdout
  64. alone?    Pretty simple operation, eh?
  65.  
  66.     cmd 2>/dev/null
  67.  
  68. Works in the Bourne shell.  In the csh, you can only make a pitiful 
  69. attempt like this:
  70.  
  71.     (cmd > /dev/tty) >& /dev/null
  72.  
  73. But who said that stdout was my tty?  So it's wrong.  This simple
  74. operation *CANNOT BE DONE* in the csh.
  75.  
  76.  
  77. Along these same lines, you can't direct error messages in csh 
  78. scripts out stderr as is considered proper.  In Bourne shell, you
  79. might say:
  80.  
  81.     echo "$0: cannot find $file" 1>&2
  82.  
  83. but in the csh, you can't redirect stdout out stderr, so you end
  84. up doing something silly like this:
  85.  
  86.     sh -c 'echo "$0: cannot find $file" 1>&2'
  87.  
  88. Reading Files
  89.  
  90. In the csh, all you've got is $<, which reads a line from your tty.  What
  91. if you've redirected stdin?  Tough noogies, you still get your tty, which 
  92. you really can't redirect.  Now, the read statement 
  93. in the Bourne shell allows you to read from stdin, which catches
  94. redirection.  It also means that you can do things like this:
  95.  
  96.     exec 3<file1
  97.     exec 4<file2
  98.  
  99. Now you can read from fd 3 and get lines from file1, or from file2 through
  100. fd 4.   In modern, Bourne-like shells, this suffices: 
  101.  
  102.     read some_var 0<&3
  103.     read another_var 0<&4
  104.  
  105. Although in older ones where read only goes from 0, you trick it:
  106.  
  107.     exec 5<&0  # save old stdin
  108.     exec 0<&3; read some_var
  109.     exec 0<&4; read another_var
  110.     exec 0<&5  # restore it
  111.  
  112.  
  113. Closing FDs
  114.  
  115. In the Bourne shell, you can close file descriptors you don't
  116. want open, like 2>&-, which isn't the same as redirecting it
  117. to /dev/null.
  118.  
  119. More Elaborate Combinations
  120.  
  121. Maybe you want to pipe stderr to a command and leave stdout alone.
  122. Not too hard an idea, right?  You can't do this in the csh as I
  123. mentioned in 1a.  In a Bourne shell, you can do things like this:
  124.  
  125.     exec 3>&1; grep yyy xxx 2>&1 1>&3 3>&- | sed s/file/foobar/ 1>&2 3>&-
  126.     grep: xxx: No such foobar or directory
  127.  
  128. Normal output would be unaffected.  The closes there were in case
  129. something really cared about all its FDs.  We send stderr to sed,
  130. and then put it back out 2.
  131.  
  132. Consider the pipeline:
  133.  
  134.     A | B | C
  135.  
  136. You want to know the status of C, well, that's easy: it's in $?, or
  137. $status in csh.  But if you want it from A, you're out of luck -- if
  138. you're in the csh, that is.  In the Bourne shell, you can get it, although
  139. doing so is a bit tricky.  Here's something I had to do where I ran dd's
  140. stderr into a grep -v pipe to get rid of the records in/out noise, but had
  141. to return the dd's exit status, not the grep's:
  142.  
  143.     device=/dev/rmt8
  144.     dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
  145.     exec 3>&1
  146.     status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) |
  147.         egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1`
  148.     exit $status;
  149.  
  150.  
  151.  
  152. COMMAND ORTHOGONALITY
  153.  
  154. Built-ins
  155.  
  156. The csh is a horrid botch with its built-ins.  You can't put them
  157. together in many reasonable way.   Even simple little things like this:    
  158.  
  159.         % time | echo
  160.  
  161. which while nonsensical, shouldn't give me this message:
  162.  
  163.         Reset tty pgrp from 9341 to 26678
  164.  
  165. Others are more fun:
  166.  
  167.         % sleep 1 | while
  168.         while: Too few arguments.
  169.         [5] 9402
  170.         % jobs
  171.         [5]     9402 Done                 sleep |
  172.  
  173.  
  174. Some can even hang your shell.  Try typing ^Z while you're sourcing 
  175. something, or redirecting a source command.  Just make sure you have
  176. another window handy.  Or try 
  177.  
  178.     % history | more
  179.  
  180. on some systems.
  181.  
  182. Flow control
  183.  
  184. You can't mix flow-control and commands, like this:
  185.     
  186.     who | while read line; do
  187.     echo "gotta $line"
  188.     done
  189.  
  190.  
  191. You can't combine multiline constructs in a csh using semicolons.
  192. There's no easy way to do this
  193.  
  194.     alias cmd 'if (foo) then bar; else snark; endif'
  195.  
  196.  
  197.  
  198.  
  199. Stupid parsing bugs
  200.  
  201. Certain reasonable things just don't work, like this:
  202.  
  203.     % kill -1 `cat foo`
  204.     `cat foo`: Ambiguous.
  205.  
  206. But this is ok:
  207.  
  208.     % /bin/kill -1 `cat foo`
  209.  
  210. If you have a stopped job:
  211.  
  212.     [2]     Stopped              rlogin globhost
  213.  
  214. You should be able to kill it with 
  215.  
  216.     % kill %?glob
  217.     kill: No match
  218.  
  219. but
  220.  
  221.     % fg %?glob
  222.  
  223. works.
  224.  
  225. White space can matter:
  226.  
  227.     if(expr)
  228.  
  229. may fail on some versions of csh, while
  230.  
  231.     if (expr)
  232.  
  233. works!
  234.  
  235.  
  236.  
  237. SIGNALS
  238.  
  239. In the csh, all you can do with signals is trap SIGINT.  In the Bourne
  240. shell, you can trap any signal, or the end-of-program exit.    For example,
  241. to blow away a tempfile on any of a variety of signals:
  242.  
  243.     $ trap 'rm -f /usr/adm/tmp/i$$ ;
  244.         echo "ERROR: abnormal exit";
  245.         exit' 1 2 3 15
  246.  
  247.     $ trap 'rm tmp.$$' 0   # on program exit
  248.  
  249.  
  250.  
  251. 6.  QUOTING
  252.  
  253. You can't quote things reasonably in the csh:
  254.  
  255.     set foo = "Bill asked, \"How's tricks?\""
  256.  
  257. doesn't work.  This makes it really hard to construct strings with
  258. mixed quotes in them.  In the Bourne shell, this works just fine. 
  259. In fact, so does this:
  260.  
  261.      cd /mnt; /usr/ucb/finger -m -s `ls \`u\``
  262.  
  263. Dollar signs cannot be escaped in double quotes in the csh.  Ug.
  264.  
  265.     set foo = "this is a \$dollar quoted and this is $HOME not quoted" 
  266.     dollar: Undefined variable.
  267.  
  268. You have to use backslashes for newlines, and it's just darn hard to
  269. get them into strings sometimes.
  270.  
  271.     set foo = "this \
  272.     and that";
  273.     echo $foo
  274.     this  and that
  275.     echo "$foo"
  276.     Unmatched ".  
  277.  
  278. Say what?  You don't have these problems in the Bourne shell, where it's
  279. just fine to write things like this:
  280.  
  281.     echo     'This is 
  282.          some text that contains
  283.          several newlines.'
  284.  
  285.  
  286. As distributed, quoting history references is a challenge.  Consider:
  287.  
  288.     % mail adec23!alberta!pixel.Convex.COM!tchrist
  289.     alberta!pixel.Convex.COM!tchri: Event not found.
  290.  
  291.  
  292. VARIABLE SYNTAX
  293.  
  294. There's this big difference between global (environment) and local
  295. (shell) variables.  In csh, you use a totally different syntax 
  296. to set one from the other.  
  297.  
  298. In Bourne shell, this 
  299.     VAR=foo cmds args
  300.  is the same as
  301.     (export VAR; VAR=foo; cmd args)
  302. or csh's
  303.     (setenv VAR;  cmd args)
  304.  
  305. You can't use :t, :h, etc on envariables.  Watch:
  306.     echo Try testing with $SHELL:t
  307.  
  308. It's really nice to be able to say
  309.     
  310.     ${PAGER-more}
  311. or
  312.     FOO=${BAR:-${BAZ}}
  313.  
  314. to be able to run the user's PAGER if set, and more otherwise.
  315. You can't do this in the csh.  It takes more verbiage.
  316.  
  317. You can't get the process number of the last background command from the
  318. csh, something you might like to do if you're starting up several jobs in
  319. the background.  In the Bourne shell, the pid of the last command put in
  320. the background is available in $!.
  321.  
  322. The csh is also flaky about what it does when it imports an 
  323. environment variable into a local shell variable, as it does
  324. with HOME, USER, PATH, and TERM.  Consider this:
  325.  
  326.     % setenv TERM '`/bin/ls -l / > /dev/tty`'
  327.     % csh -f
  328.  
  329. And watch the fun!
  330.  
  331.  
  332. EXPRESSION EVALUATION
  333.  
  334. Consider this statement in the csh:
  335.  
  336.  
  337.     if ($?MANPAGER) setenv PAGER $MANPAGER
  338.  
  339.  
  340. Despite your attempts to only set PAGER when you want
  341. to, the csh aborts:
  342.  
  343.     MANPAGER: Undefined variable.
  344.  
  345. That's because it parses the whole line anyway AND EVALUATES IT!
  346. You have to write this:
  347.  
  348.     if ($?MANPAGER) then
  349.     setenv PAGER $MANPAGER
  350.     endif
  351.  
  352. That's the same problem you have here:
  353.  
  354.     if ($?X && $X == 'foo') echo ok
  355.     X: Undefined variable
  356.  
  357. This forces you to write a couple nested if statements.  This is highly
  358. undesirable because it renders short-circuit booleans useless in
  359. situations like these.  If the csh were the really C-like, you would
  360. expect to be able to safely employ this kind of logic.  Consider the
  361. common C construct:
  362.  
  363.     if (p && p->member) 
  364.  
  365. Undefined variables are not fatal errors in the Bourne shell, so 
  366. this issue does not arise there.
  367.  
  368. While the csh does have built-in expression handling, it's not
  369. what you might think.  In fact, it's space sensitive.  This is an
  370. error
  371.  
  372.    @ a = 4/2
  373.  
  374. but this is ok
  375.  
  376.    @ a = 4 / 2
  377.  
  378.  
  379. The ad hoc parsing csh employs fouls you up in other places 
  380. as well.  Consider:
  381.  
  382.     % alias foo 'echo hi' ; foo
  383.     foo: Command not found.
  384.     % foo
  385.     hi
  386.  
  387.  
  388. ERROR HANDLING
  389.  
  390. Wouldn't it be nice to know you had an error in your script before
  391. you ran it?   That's what the -n flag is for: just check the syntax.
  392. This is especially good to make sure seldom taken segments of code
  393. code are correct.  Alas, the csh implementation of this doesn't work.
  394. Consider this statement:
  395.  
  396.     exit (i)
  397.  
  398. Of course, they really meant
  399.  
  400.     exit (1)
  401.  
  402. or just
  403.  
  404.     exit 1
  405.  
  406. Either shell will complain about this.  But if you hide this in an if
  407. clause, like so:
  408.  
  409.     #!/bin/csh -fn
  410.     if (1) then
  411.     exit (i)
  412.     endif
  413.  
  414. The csh tells you there's nothing wrong with this script.  The equivalent
  415. construct in the Bourne shell, on the other hand, tells you this:
  416.  
  417.  
  418.     #!/bin/sh -n
  419.     if (1) then
  420.     exit (i)
  421.     endif
  422.  
  423.     /tmp/x: syntax error at line 3: `(' unexpected
  424.  
  425.  
  426.  
  427. RANDOM BUGS
  428.  
  429. Here's one:
  430.  
  431.     fg %?string
  432.     ^Z
  433.     kill  %?string
  434.     No match.
  435.  
  436. Huh? Here's another
  437.  
  438.     !%s%x%s
  439.  
  440. Coredump, or garbage.
  441.  
  442. If you have an alias with backquotes, and use that in backquotes in 
  443. another one, you get a coredump.
  444.  
  445. Try this:
  446.     % repeat 3 echo "/vmu*"
  447.     /vmu*
  448.     /vmunix
  449.     /vmunix
  450. What???
  451.  
  452.  
  453. Here's another one:
  454.  
  455.     % mkdir tst
  456.     % cd tst
  457.     % touch '[foo]bar'
  458.     % foreach var ( * )
  459.     > echo "File named $var"
  460.     > end
  461.     foreach: No match.
  462.  
  463.  
  464. SUMMARY
  465.  
  466.  
  467. While some vendors have fixed some of the csh's bugs (the tcsh also does
  468. much better here), many have added new ones.  Most of its problems can
  469. never be solved because they're not actually bugs per se, but rather the
  470. direct consequences of braindead design decisions.  It's inherently flawed.
  471.  
  472. Do yourself a favor, and if you *have* to write a shell script, do it in the 
  473. Bourne shell.  It's on every UNIX system out there.  However, behavior 
  474. can vary.
  475.  
  476. There are other possibilities.
  477.  
  478. The Korn shell is the preferred programming shell by many sh addicts,
  479. but it still suffers from inherent problems in the Bourne shell's design,
  480. such as parsing and evaluation horrors.  The Korn shell or its
  481. public-domain clones and supersets (like bash) aren't quite so ubiquitous
  482. as sh, so it probably wouldn't be wise to write a sharchive in them that
  483. you post to the net.  When 1003.2 becomes a real standard that companies
  484. are forced to adhere to, then we'll be in much better shape.  Until
  485. then, we'll be stuck with bug-incompatible versions of the sh lying about.
  486.  
  487. The Plan 9 shell, rc, is much cleaner in its parsing and evaluation; it is
  488. not widely available, so you'd be sacrificing portability.  No vendor
  489. is shipping it yet.
  490.  
  491. If you don't have to use a shell, but just want an interpreted language,
  492. many other free possibilities present themselves, like Perl, REXX, TCL,
  493. Scheme, or Python.  Of these, Perl is probably the most widely available
  494. on UNIX (and many other) systems and certainly comes with the most
  495. extensive UNIX interface.  Some vendors even ship it standard.
  496.  
  497. If you have a problem that would ordinarily use sed or awk or sh, but it
  498. exceeds their capabilities or must run a little faster, and you don't want
  499. to write the silly thing in C, then Perl may be for you.  You can get
  500. at networking functions, binary data, and most ofthe C library. There
  501. are also translators to turn your sed and awk scripts into Perl scripts,
  502. as well as a symbolic debugger.  Tchrist's personal rule of thumb is
  503. that if it's the size that fits in a Makefile, it gets written in the
  504. Bourne shell, but anything bigger gets written in Perl.
  505.  
  506. See the comp.lang.{perl,rexx,tcl} newsgroups for details about these
  507. languages (including FAQs), or David Muir Sharnoff's comparison of 
  508. freely available languages and tools in comp.lang.misc and news.answers.
  509.  
  510.  
  511. -- 
  512.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  513.  
  514.     "UNIX was not designed to stop you from doing stupid things, because
  515.      that would also stop you from doing clever things." -- Doug Gwyn
  516.