You may include integer arithmetic in your scripts. First you must assign a value to each variable used with the set command.
Notice that arrays are defined as spaced-separated values in parentheses. So
$data is 30. You can
also set a variable to a null value, such as
list above, so that the variable is ready to be assigned to an
To perform arithmetic the expression is prefixed with the special @ character like this.
count. Note that the space following the
@and around the
+operator are needed. Likewise the examples below both subtract one from the value of
There are several other operators, the most important ones are illustrated below.
countby 5, rounding down, so if
ratiowould be 2. The second assigns the remainder of the second element of the array called
dataafter division by 8. So if
octremwould be set to 6. Finally
numelementis equated to the product of variables
The precedence order is
* / % followed by
+ -. If you are in doubt, use parentheses to achieve the
As variables are either integers or strings, you have to find an alternative route to have logical
variables in your script. One way is to define string values
"false" (or uppercase
equivalents) and test for these. For instance, this checks the value of variable
smooth; when it is true
the current dataset is Gaussian smoothed.
trueare not the same.
Another popular way is to use an integer, which is assigned to 0 for a false value, and 1 for
true. Indeed this is the way logical expressions are evaluated by the shell. For instance in the following logical expression
nlies between 5 and 19 inclusive, but is
1(true) otherwise. Variable
yis the negation of
x. Note the parentheses surrounding the logical expressions; they are needed when an expression contains
&. A list of the comparison operators is given in the table to the right.
Here are a few more examples.
| ||Equal to|
| ||Boolean NOT|
| ||Not equal|
| ||Boolean AND|
| ||Boolean OR|
| ||Greater than|
| ||Less than|
| ||Greater or equal to|
| ||Less than or equal to|
The additional parentheses in the expression for
finish indicate the evaluation order starting from the
Observant readers will have noticed there are no comparisons involving floating-point numbers. Although you can do some limited string comparison, another means is needed. One such is to use the bc calculator.
" quoted if statement involving the desired conditional statements is passed to bc. If bc evaluates
the statement true, it returns the 1, which is equivalent to true in C-shell. Note since the piping into bc
is a command, the whole assignment is enclosed in backquotes. In the first example above, the
parentheses are not required, as the
< are part of a string, not operators, as far as the shell is
concerned. The other two examples show this technique in the context of a loop control and an if
Note bc uses an arbitrary and controllable precision (cf. the length and scale attributes). Be aware it also uses a non-standard syntax for scientific notation, as seen defining variable current above.
The C-shell also offers some operators that test the characteristics of a file, such as file existence. See Section 12.4 for details.
The shell cannot perform non-integer arithmetic. Thus you need to employ some other utility. The standard UNIX bc arbitrary-precision calculator is one. You can also define an algebraic calculator with awk or gawk as shown below.
calcfor reasons that will soon be evident. Note that in expressions you don’t escape the multiplication sign and the expression is not in
"quotes. The small set of about ten mathemetical functions available in awk, including arctangent shown above that evaluates , limits what you can achieve. Another option is the (SUN/95) calc command. calc has most of the Fortran intrinsic functions available. For the remainder of this section, we shall just deal with calc.
The calc tool evaluates Fortran-like expressions, sending the result to standard output. So suppose that we wish to add two real values.
ctakes the value of adding variables
b. Note the back quotes that cause calc to be executed.
This is fine if you know that the values will always be positive. However, calc will object if there are
adjacent mathematical operators, such as
+- as would happen if
b were negative. So surround
variables in parentheses, remembering that the
( ) are metacharacters. See Section 7.4 for more
Let’s redo the first example along with a few more to illustrate the recommended syntax. This time we’ll specify the expression by parameter name rather than position.
" "are needed because we want the dollar to retain its variable-substitution meaning. If the expression contains embedded spaces (usually for clarity) it should be enclosed in single quotes as shown in the assignment to variable
f. So in general the recipe for using calc is
set variable = ‘calc exp="’expression’"‘
Don’t try to escape all the metacharacters individually because it can soon become very messy, error prone, and hard to follow.
The default precision is single precision. If you need more significant figures then append
prec=_double to the calc command line. The special symbol
pi has the value of
arc) to degrees using double-precision arithmetic.
It is sometimes easier to assemble the expression in another variable. Here’s another recipe which demonstrates this approach. Suppose that you want to find the average median value for a series of NDFs.
expr, but otherwise each new value is appended to the expression. Once all the medians are evaluated, the remainder of the expression, including division by the number of NDFs is appended. Finally, the expression is evaluated using calc. If you want to learn more about the
set median = ‘parget median histat‘command see Section 9.2.
If you want to include intrinsic functions, such as logarithms and trigonometric functions in your calculations, or perhaps you need some function for an integer expression that is not supplied by the C-shell, such as finding the maximum or the absolute value, calc may be the solution. It offers the 28 functions tabulated below.
Here are a few examples.
3and assigns it to variable
c. The second expression sums two positive differences:
magis the magnitude derived from the flux stored in variable
separationis assigned the inverse tangent of 35.3 divided by the value of variable
distmeasured between 180 and 180 degrees.
|Function||Number of arguments||Description|
|ABS||1||absolute (positive) value:|
|NINT||1||nearest integer value to arg|
|MAX||2 or more||maximum of arguments|
|MIN||2 or more||minimum of arguments|
|DIM||2||Fortran DIM (positive difference) function|
|MOD||2||Fortran MOD (remainder) function|
|SIGN||2||Fortran SIGN (transfer of sign) function|
|ASIN*||1||inverse sine function:|
|ACOS*||1||inverse cosine function:|
|ATAN*||1||inverse tangent function:|
|ATAN2*||2||inverse tangent function:|
|SINH*||1||hyperbolic sine function:|
|COSH*||1||hyperbolic cosine function:|
|TANH*||1||hyperbolic tangent function:|
|ASIND*||1||inverse sine function:|
|ACOSD*||1||inverse cosine function:|
|ATAND*||1||inverse tangent function:|
|ATAN2D*||2||inverse tangent function:|
*Function does not support integer arithmetic.