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[3]
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
expression.
To perform arithmetic the expression is prefixed with the special @ character like this.
Both of these add one to the value ofcount
. Note that the space following the @
and around the =
and
+
operator are needed. Likewise the examples below both subtract one from the value of
count
.
There are several other operators, the most important ones are illustrated below.
The first divides the value ofcount
by 5, rounding down, so if count
was 11, ratio
would be 2. The
second assigns the remainder of the second element of the array called data
after division by 8. So if
data[2]
is 30, octrem
would be set to 6. Finally numelement
is equated to the product of variables i
and j
.
The precedence order is * / %
followed by + -
. If you are in doubt, use parentheses to achieve the
desired order.
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 "true"
and "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.
TRUE
and true
are 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
variablex
is 0
(false) when n
lies between 5 and 19
inclusive, but is 1
(true) otherwise. Variable y
is the
negation of x
. Note the parentheses surrounding
the logical expressions; they are needed when an
expression contains >
, <, |
, or &
. A list of the
comparison operators is given in the table to the
right.
Here are a few more examples.
Comparison operators
| |
Operator | Meaning |
== | 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
innermost parentheses.
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.
The "
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 >
and <
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
…then block.
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.
calc
for 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 Kappa 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.
The variablec
takes the value of adding variables a
and 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
details.
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
.
So
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.
3
and assigns it to variable c
. The
second expression sums two positive differences: 0
and 1
respectively.
mag
is the magnitude derived from the flux stored in variable counts
. separation
is
assigned the inverse tangent of 35.3 divided by the value of variable dist
measured between
180 and
180 degrees.
Function | Number of arguments | Description |
SQRT | 1 | square root: |
LOG | 1 | natural logarithm: |
LOG10 | 1 | common logarithm: |
EXP | 1 | exponential: |
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 |
SIN* | 1 | sine function: |
COS* | 1 | cosine function: |
TAN* | 1 | tangent 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: |
SIND* | 1 | sine function: |
COSD* | 1 | cosine function: |
TAND* | 1 | 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.
| ||