Shell Programming 157 There is another big problem as well, one that we’ve been glossing over from the beginning. The Unix file program doesn’t work. Date: Sat, 25 Apr 92 17:33:12 EDT From: Alan Bawden Alan@lcs.mit.edu Subject: Simple Shell Programming To: UNIX-HATERS WHOA! Hold on a second. Back up. You're actually proposing to use the ‘file’ program? Everybody who wants a good laugh should pause right now, find a Unix machine, and try typing “file *” in a directory full of miscellaneous files. For example, I just ran ‘file’ over a directory full of C source code— here is a selection of the results: arith.c: c program text binshow.c: c program text bintxt.c: c program text So far, so good. But then: crc.c: ascii text See, ‘file’ isn’t looking at the “.c” in the filename, it’s applying some heuristics based on an examination of the contents of the file. Appar- ently crc.c didn’t look enough like C code—although to me it couldn’t possibly be anything else. gencrc.c.~4~: ascii text gencrc.c: c program text I guess I changed something after version 4 that made gencrc.c look more like C… tcfs.h.~1~: c program text tcfs.h: ascii text while tcfs.h looked less like C after version 1. time.h: English text That’s right, time.h apparently looks like English, rather than just ascii. I wonder if ‘file’ has recognition rules for Spanish or French?
158 csh, pipes, and find (BTW, your typical TeX source file gets classified as “ascii text” rather than “English text,” but I digress…) words.h.~1~: ascii text words.h: English text Perhaps I added some comments to words.h after version 1? But I saved the best for last: arc.h: shell commands Makefile: [nt]roff, tbl, or eqn input text Both wildly wrong. I wonder what would happen if I tried to use them as if they were the kinds of program that the ‘file’ program assigns them? —Alan Shell Variables Won’t Things could be worse for Alan. He could, for instance, be trying to use shell variables. As we’ve mentioned before, sh and csh implement shell variables slightly differently. This wouldn’t be so bad, except that semantics of shell vari- ables—when they get defined, the atomicity of change operations, and other behaviors—are largely undocumented and ill-defined. Frequently, shell variables behave in strange, counter-intuitive ways that can only be comprehended after extensive experimentation. Date: Thu, 14 Nov 1991 11:46:21 PST From: Stanley’s Tool Works lanning@parc.xerox.com Subject: You learn something new every day To: UNIX-HATERS Running this script: #!/bin/csh unset foo if ( ! $?foo ) then echo foo was unset else if ( "$foo" = "You lose" ) then echo $foo
Previous Page Next Page