Friday, January 4, 2013

Csh (tcsh) Scripting Basics, setenv, bargepoles, &c for csh haters

A very nice resource for csh scripting details, setenv, etc, when it is absolutely necessary.

"The shell you type your commands in, tcsh, is unfortunately rather brain-dead at scripting (see for the gory details). If we were not astronomers I'd advise everyone never to touch tcsh/csh with a bargepole (indeed I still do...), but I'm afraid we're stuck with it for historical reasons. If you understand what you're doing you might want to look at bash, but otherwise stick with tcsh, and use a better scripting language (like Perl or Python if you need to do more).
The possible options for writing a script or programming are:
  1. We write the script in tcsh. We have to step around all the horrible problems with csh/tcsh. You can do this for simple scripts, but doing anything slightly more complex feels like your brain is sucked out through your nose, but fortunately we live to tell our tale to the others in the sanatorium between the nightmares.
  2. We write the script in a good shell like bash. This has a lot going for it, but unfortunately we don't get access to all the aliases that setup scripts for the astronomical packages provide. Aliases are also a brain-dead way of providing commands in the shells without doing properly. That's what you get when you ask astronomers to write software packages without giving them training. I'm told to advise you against bash, as few other astronomers know what you're talking about.
  3. We write in a much more featureful scripting language like Python or Perl. We also don't get the the aliases that the setup scripts provide, but we can write much more complex programs that can do proper maths. I'll cover scripting languages briefly elsewhere.
  4. We can write a script in a proprietary language like IDL or Mathematica. Can be simple and powerful but be aware that people you might want to give your code to won't have the cash to buy these expensive programs to run your simple script (don't laugh - I know one astronomer who writes all his simple scripts in IDL, so no one else uses them). People won't always have IDL or Mathematica on their laptops.
  5. We can write in a fully-fledged programming language (not a scripting language) like C, Fortran or C++. Typically it is harder to write a program in these languages but it will run more quickly than the above options. This is necessary if you need to do lots of calculations. I'll cover programming elsewhere.
  6. If we are a hardcore masochistic nut, we can code in raw binary by flipping bits on a disk with a magnet. We would also be able to tell our exciting life story to those guys who are putting us in that nice soft room. 
I'll leave it others more qualified to cover this. I'm in two minds what to give you here, so I'll give you examples of writing scripts in tcsh and bash, and strongly advise those who are doing more than running the same ten commands in a row to use Python or Perl (maybe bash if you feel up to understanding what's going on).

I'm not going to give you a fully fledged introduction to shell scripting, but leave that to the resources I'll give you on the links page. "
The rest of this great article can be reads here: Shell Scripting

'via Blog this'

No comments:

Post a Comment