SC/4.4

Starlink Project
Starlink Cookbook 4.4

Malcolm J. Currie

2006 November 26


C-shell Cookbook

Version 1.3



Abstract

This cookbook describes the fundamentals of writing scripts using the UNIX C shell. It shows how to combine Starlink and private applications with shell commands and constructs to create powerful and time-saving tools for performing repetitive jobs, creating data-processing pipelines, and encapsulating useful recipes. The cookbook aims to give practical and reassuring examples to at least get you started without having to consult a UNIX manual. However, it does not offer a comprehensive description of C-shell syntax to prevent you from being overwhelmed or intimidated. The topics covered are: how to run a script, defining shell variables, prompting, arithmetic and string processing, passing information between Starlink applications, obtaining dataset attributes and FITS header information, processing multiple files and filename modification, command-line arguments and options, and loops. There is also a glossary.

Contents

1 Introduction
2 Further Reading
3 How to use this manual
4 Conventions
5 Running a script
 5.1 Shell selection
 5.2 Making a script executable
 5.3 Executing a script by name
 5.4 Using aliases to run a script
 5.5 Removing aliases
 5.6 Executing a script in the current process
 5.7 Package aliases
6 Some simple examples
7 Shell Variables
 7.1 Assigning scalar values
 7.2 Assigning arrays
 7.3 Using the values of variables
 7.4 Special characters
 7.5 Prompting
 7.6 Script arguments and other special variables
 7.7 Predefined variables
8 Executing a Starlink Application
 8.1 Parameter files and the graphics database
 8.2 How to test whether or not a Starlink task has failed
9 Passing information between Starlink applications
 9.1 Parsing text output
 9.2 Via Parameters
10 Arithmetic
 10.1 Integer
 10.2 Logical
 10.3 Floating Point
 10.4 Intrinsic Functions
11 String Processing
 11.1 String concatenation
 11.2 Obtain the length of a string
 11.3 Find the position of a substring
 11.4 Extracting a substring
 11.5 Split a string into an array
 11.6 Changing case
 11.7 String substitution
 11.8 Formatted Printing
12 Dealing with Files
 12.1 Extracting parts of filenames
 12.2 Process a Series of Files
  12.2.1 NDFs
  12.2.2 Wildcarded lists of files
  12.2.3 Exclude the .sdf for NDFs
  12.2.4 Examine a series of NDFs
 12.3 Filename modification
  12.3.1 Appending to the input filename
  12.3.2 Appending a counter to the input filename
  12.3.3 Appending to the input filename
 12.4 File operators
 12.5 Creating text files
  12.5.1 Writing a script within a script
 12.6 Reading lines from a text file
 12.7 Reading tabular data
  12.7.1 Finding the number of fields
  12.7.2 Extracting columns
  12.7.3 Selecting a range
  12.7.4 Choosing columns by name
 12.8 Reading from dynamic text files
 12.9 Discarding text output
 12.10 Obtaining dataset attributes
  12.10.1 Obtaining dataset shape
  12.10.2 Available attributes
  12.10.3 Does the dataset have variance/quality/axis/history information?
  12.10.4 Testing for bad pixels
  12.10.5 Testing for a spectral dataset
 12.11 FITS Headers
  12.11.1 Testing for the existence of a FITS header value
  12.11.2 Reading a FITS header value
  12.11.3 Writing or modifying a FITS header value
 12.12 Accessing other objects
 12.13 Defining NDF sections with variables
13 Loop a specified number of times
14 UNIX-style options
15 Debugging scripts
16 Breaking-in
17 Longer recipes
 17.1 Recipe for masking and background-fitting
18 Glossary