Showing posts with label tcsh. Show all posts
Showing posts with label tcsh. Show all posts

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 http://www-uxsup.csx.cam.ac.uk/misc/csh.html 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'

Monday, November 5, 2012

CSH Scripting Basics, basic control syntax and file/dir tests

I heartly dislike csh syntax, but still need reminders of how it works from time to time to help other people, so here 'tis.
Connie "Necessary Evils" O'Dell
P.S. Why not switch over to bash today, and while you're at it, emacs "The One True Editor (TM)"?
You will thank me later.  ;-)
-------------------------------------------------
Contents:


Introduction
Rather than going into a "how to program" tutorial, we'll simply show a few examples of scripts and what they do, how to modify them, etc. In general, a shell script is a program just like any other and thus may have bugs and "features" that you did not expect. Try to keep them simple, put the "real" programming into your executable, not the script. Use the script to set up data files, pre- and post-process data, and sequence operations. We'll show csh syntax, although any other shell can be used - the conditionals and loop constructs may be slightly different.


#!/bin/csh 
# 
cd data 
./runme
Note that lines beginning with a # (pound) character are comments. The first line specifies what shell is to be used, csh in this example. While it is technically inside a comment line, the shell always looks at the first line for this type of construct, then it knows how to interpret or run the script.As with the csh shell itself, variables may be referenced with a dollar sign. So after we "set p = 2", we can refer to variable p by "echo $p", for example. Note that variables are assumed to be strings unless explicitly operated on with a mathematical operation using the @ functionality (at-sign, see below).



Loops
Here's a simple loop example:


#!/bin/csh 
# 
cd data 
foreach l ( a b c 123 456 red blue green ) 
  runprog < datafile.$l.in > datafile.$l.out 
end
The foreach line loops through all entries in the paren list, setting the variable l to each one and then executing the loop body. In this case, the loop will be executed 8 times. During the first trip through the loop, variable l will have the value a. The second trip will have l equal to b, etc. When the loop executes with l equal to 123, remember that it is the string "123" and not a number.
Note that we are using input and output redirection, that is, we are using a file to simulate "keyboard input" to the program and we are capturing any screen output to a second file.



If..Then 
#!/bin/csh 
# 
cd data 
foreach l ( a b c 123 456 red blue green ) 
  if( ! -e datafile.$l.out ) then 
    runprog < datafile.$l.in > datafile.$l.out 
  endif 
end
Inside the loop, we now test a conditional. The '!' means logical-negation and the '-e' means "does the file exist". So the end results is: if a file called datafile.$l.out does not exist, then we run the programrunprog (and any other statements until we hit the endif). There are a number of tests you can do in the 'if' clause. Logical and file-test operations are described in the next sections.Finally, the script executes the program, using input and output redirection with the angle brackets (greater-than and less-than signs). Note that if you have done "set noclobber" then you cannot redirect output to an existing file - an error will occur. You can use >> to append the current output to an existing file. If you want to force the overwritting of an existing file, you can also use >!. To redirect the "standard error" output, use >& or >>&.
There is an option for an 'else' section, which will execute if the test condition is false. For several 'If..Then' clauses in a row, you may use 'else if' statements in between, then close the whole group with a single 'endif'.
Note the idea of the above script is that the exact same script can be submitted over and over again and it will ONLY run jobs that have not been completed yet. Once a job has run, it will have produced an output file called "datafile.$l.out" and thus the "if" clause will stop it from being executed again.



Logical Operators
The exclamation point signfies logical NOT - ie. "! -e file" is true if the file does NOT exist. Logical AND is performed with && (double ampersands), logical OR is done with || (double bar).

You can compare strings using '==' (equal) and '!=' (not equal), as well as '=~' and '!~' which allow for wildcard characters ('*' and '?') on the right-hand side of the test. The '*' wildcard will match any number of any character, the '?' will match exactly one character. You can also use square brackets to allow a match to one-of-N choices, e.g. '[ABC]' will match only one character of 'A', 'B', or 'C'.
For numeric values, you can use '>', '>=', '<=', '<'


File-test Operators
There are a number of operators you can use to test different attributes of a file:

-e file
true if file exists
-o file
true if file exists and is owned by the user
-r file
true if file exists and is readable
-w file
true if file exists and is writable
-x file
true if file exists and is executable
-z file
true if file exists and is zero size
-d dir
true if dir exists and is a directory



Doing Math
To illustrate some math in a script, we'll use the following example:


#!/bin/csh 
# 
cd data 
# 
foreach p ( 1 2 4 8 16 ) 
  @ s = $p * 25 
  foreach l ( a b c ) 
    prog -p=$p -size=$s
  end 
end
Again, recall that all variables are assumed to be strings. The '@' (at sign) indicates that we want to interpret the string as a number and then do some math with it.So in this script, the variable "$p" is a string, but we temporarily interpret it as a number, multiply it by 25, and then set that result (as a string) into variable "$s".

CSH Scripting Basics

'via Blog this'