In scripts you will often want to take some result produced or calculated by one application, and to pass that information to another. Two techniques are available: piping or output parameters.
If the first task prints the required value, it is possible to use one or more of grep, sed, and awk to locate and isolate the value. For example, to obtain the mean value of a data array you could have the following lines in your script.
The accept
keyword tells stats to use the current values for any parameters for which it would
otherwise prompt you. The back quotes (‘ ‘
are important. They tell the shell to execute the
expression they enclose, in this case stats accept | grep "Pixel mean", and assign the result to
variable dummy
. So the Kappa stats task is run on the current dataset, and the output is passed to grep
which searches for the line containing the string Pixel mean
, and the mean value. When you equate a
variable to a multi-word string not enclosed in quotes—words being separated by spaces—the
variable becomes an array, whose elements are each assigned to a word. So in this case the fourth
word or element is the mean value.
Besides being inelegant, this method demands that the format and text of the output be fixed. So it should be avoided where the first task writes results to output parameters.
There is a more-robust technique for passing information between Starlink applications; it does not rely on the formatting of the output. Where an application writes output parameters (otherwise called results parameters), you may use the parget command of Kappa to write to standard output the value or values associated with a parameter for a named application. In a script we want to assign the value to a shell variable. Let’s see how that compares with obtaining the same mean value as before.
This runs stats, but redirects the output to the null device as the output is not wanted. However, this does not prevent the output parameter called MEAN from being assigned the mean value. parget retrieves this value, which is then assigned to variableaverage
. Note that
parget retrieves parameter values for the last invocation of the task. Thus if you wanted the
standard deviation too, you would only need to issue the stats command once (as shown
below).