| 1 |
! $Id: if,v 1.3 2002-02-19 19:43:28 f Exp $ |
| 2 |
! |
| 3 |
! Copyright (c) 1990-2001 Michael Sandrof, Troy Rollo, Matthew Green, |
| 4 |
! and other ircII contributors. |
| 5 |
! |
| 6 |
! All rights reserved. See the HELP SCROLLZ COPYRIGHT file for more |
| 7 |
! information. |
| 8 |
! |
| 9 |
Usage: IF (<variable-expression>) {<true-command/s>} [{<false-command/s>}] |
| 10 |
IF tests the value of the variable expression for truth. An |
| 11 |
expression is true if it is either a non empty string or a non |
| 12 |
zero number. If the expression is true, the true-commands are |
| 13 |
executed. If it is false, and the false-commands are given, they |
| 14 |
are executed. Usually the expressions will be formed by using |
| 15 |
combinations of the following: |
| 16 |
( exp ) ( exp1 ^^ exp2 ) |
| 17 |
( exp1 == exp2 ) ( exp1 >= exp2 ) |
| 18 |
( exp1 != exp2 ) ( exp1 <= exp2 ) |
| 19 |
( exp1 > exp2 ) ( exp1 || exp2 ) |
| 20 |
( exp1 < exp2 ) ( exp1 && exp2 ) |
| 21 |
|
| 22 |
Here exp is evaluated as though it were in ${} which means that |
| 23 |
if (blah) expands the contents of $blah. Numbers are treated |
| 24 |
as constants and thus to expand $0 it must be passed to the |
| 25 |
regular parser with []. eg. IF ([$0]) ... |
| 26 |
|
| 27 |
IF (!exp) is also possible which will negate a number or a string. |
| 28 |
In the other expressions, if both exp1 and exp2 expand to numbers, |
| 29 |
then the following cause true-command to be executed: |
| 30 |
== if exp1 is equal to exp2 |
| 31 |
!= if exp1 is not equal to exp2 |
| 32 |
> if exp1 is greater than exp2 |
| 33 |
< if exp1 is less than exp2 |
| 34 |
>= if exp1 is greater than or equal to exp2 |
| 35 |
<= if exp1 is less than or equal to exp2 |
| 36 |
^^ Logical XOR of exp1 and exp2 (1 ^^ 1) == 0 |
| 37 |
(1 ^^ 0) or (0 ^^ 1) == 1 |
| 38 |
|| Logical OR of exp1 and exp2 |
| 39 |
&& Logical AND or exp1 and exp2 |
| 40 |
|
| 41 |
Otherwise, false-command (if present) is executed. |
| 42 |
If both exp1 and exp2 are non-numeric, then the above operations |
| 43 |
are string value comparisons. And for the logical operations |
| 44 |
A NULL string is treated as 0 with the possible exception of the ^^ |
| 45 |
operator. |
| 46 |
|
| 47 |
To put a string in an expression it must be passed to the |
| 48 |
regular parser with []. eg IF ([string1] == [string2]) .. |
| 49 |
All string comparisons are case-insensitive: so ([Hello]==[HELLO]) |
| 50 |
is true. |
| 51 |
|
| 52 |
Examples: |
| 53 |
IF (C) { set input_prompt $C } { set -input_prompt } |
| 54 |
IF (C!=0) { set input_prompt $C } { set -input_prompt } |
| 55 |
|
| 56 |
Both of the above evaluate the same way, since $C will either expand |
| 57 |
to your current channel or 0 if none. Notice the (C) format which |
| 58 |
means that it will expand exactly like ${C}. |
| 59 |
|
| 60 |
$ expansion is always performed on the contents of an the true-commands |
| 61 |
and false commands, regardless of the setting of the INPUT_ALIASES |
| 62 |
variable. |
| 63 |
|
| 64 |
IF (USER==[ms5n]) { echo Hello Mike! } { echo Who the hell are you? } |
| 65 |
|
| 66 |
This example will look for an ASSIGN'd variable named USER, and if found |
| 67 |
replace it and compare to ms5n. If no ASSIGN'd variable exists, it |
| 68 |
will check the environment variables for USER and return that. |
| 69 |
|
| 70 |
IF (index > max*2) { echo Limit reached } |
| 71 |
This checks the ASSIGN'd variable against the ASSIGN'd variable |
| 72 |
max times 2 and displays "Limit reached" if the first is greater than |
| 73 |
the second. It does nothing if this is not true. |
| 74 |
|
| 75 |
IF's can now be imbedded within each other. It is inconvient to write it |
| 76 |
out on a single line, so I will show the format needed in a script file. |
| 77 |
|
| 78 |
if (time() - lasttime > 600) |
| 79 |
{ |
| 80 |
if (thud == [gone home]) |
| 81 |
{ |
| 82 |
echo Bill went home over ten minutes ago |
| 83 |
echo $stime($time()) |
| 84 |
} |
| 85 |
{ |
| 86 |
echo Bill is still here. |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
This will first expand the function time() and the ASSIGN'd var 'lasttime' |
| 91 |
to their current values, subtract (seconds) and see if its more than |
| 92 |
600 seconds, (10 minutes). If that is true, then it checks to see if the |
| 93 |
ASSIGN'd var 'thud' equals the string "gone home". If that is ALSO true |
| 94 |
then it will echo "Bill went home over ten minutes ago" and the current |
| 95 |
date and time. If the second IF is not true then it will display |
| 96 |
"Bill is still here.". Note there is no 'false' clause for the first |
| 97 |
IF. |
| 98 |
|
| 99 |
See Also: |
| 100 |
@ |
| 101 |
FOREACH |
| 102 |
WHILE |
| 103 |
expressions |