Another Reason Not to Write csh Scripts
In case you haven’t read Tom Christiansen’s
Csh Programming Considered Harmful,
here’s another reason not to write csh/tcsh scripts
if you can avoid it.
Unlike the Bourne Shell, the C shell exits with “Undefined variable”
if you reference an undefined variable, instead of expanding that
variable to the empty string, the way the Bourne shell and Perl do.
But there’s the $?VAR construct, which allows you to
tell whether a variable is set.
I needed to check this in a script that, for reasons I won’t go into
here, needed to be written in Csh. So I had
if ( !$?FOO ) then # Stuff to do if $FOO isn't set
and got an error. It turns out that csh started by evaluating
$?FOO. In this case, $FOO was set, so $?FOO
was set to 0. Csh then tried to evaluate !0 and parsed it as
“event number zero”, which failed. Grrr.
Putting a space between the bang and the dollar sign fixed that.
I’ve become a spoiled ruby user over the past few years. I know it’s not installed on most machines, but most of my work is done on private workstations (or servers I admin), so I can get away with forgetting my bourne shell syntax.
My big complaints about ruby stem from little annoyances like the “parentheses and readable code are optional” construct and the lack of increment/decrement operators. Of course, my wife prefers perl, so there are certain subjects that we simply can’t bring up at Thanksgiving.
But yes, csh scripts are an abomination.
Out of curiosity, what’s so great about Ruby? I’ve grown quite happy with Perl, and I might have to delve into Python for some stuff I want to do. I’ve seen that Ruby seems to have a contingent of enthusiasts, but so far I haven’t run into a good reason to learn it — or, indeed, a list of features that its adherents like.
To me, Ruby is just perl, but easier to read. I don’t do web programming, and I was looking for a general scripting language that would complement my skills as a C++ programmer. Python’s whitespace dependence was just offensive to me, so I took a quick look at Ruby and found some pretty cool stuff. A few things I liked:
1) It’s easy and clean to add useful iterators to a class.
2) You can easily extend built-in classes. For example, adding a .factorial method to the Integer class and then using that .factorial method to implement a .choose method, you can do useful stuff like 6.choose(3) in your main code.
3) The .freeze method that makes an object constant after you’re finished messing with it.
4) The concept of “tainted” data based on whether the data originates “safely” inside the program or from some dangerous outside source. Having the interpreter keep track of that stuff and prevent you from accidentally passing unchecked data to dangerous calls is pretty cool.
5) I do a lot of image processing code in C and C++ and have found that writing Ruby wrappers is quick and easy.
All these things are available to a greater or lesser extent in other languages, but Ruby packaged them up in a clean and easy to learn interface. I’ve found that when I don’t know the syntax for something, my first guess is usually correct. It’s extremely intuitive.
I like to joke with my wife that perl is great because you can write a powerful program in no time, but the feature is necessary because most programmers need to rewrite it when they come back and can’t figure out what it does. I bust on perl from time to time, but it’s only because I love it.