GNU Rush – a restricted user shell (split by node):   Section:   Chapter:FastBack: Configuration File   Up: Matching Conditions   FastForward: Default Configuration   Contents: Table of ContentsIndex: Concept Index

4.4.2.1 Comparisons

A comparison expression is:

lhs op rhs

here, lhs (left-hand side) is a string (quoted or unquoted), or a variable reference (see Lexical Structure), rhs (right-hand side) is a string or number, and op is one of the following binary operators:

==Equality (string or numeric)
!=Inequality (string or numeric)
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to
~Regexp matching
!~Negated regexp matching

Table 4.4: Comparison Operators

Prior to evaluating simple expression, its left-hand side undergoes variable expansion and backreference interpretation. In contrast, the right-hand side is always treated verbatim.

For example the following rule will match any request with 2 or more arguments (recall, that the command name itself is counted as one of the arguments):

rule
  match $# >= 2

The ‘==’ and ‘!=’ can operate both on strings and on numbers. When applied to strings the ‘==’ means byte-to-byte equality, e.g.

  match $0 == "/bin/ls"

will match requests with ‘/bin/ls’ as the command name.

The ‘~’ and ‘!~’ operators implement regular expression matching.

The expression ‘lhs ~ rx’ yields ‘true’ if lhs matches regular expression rx. E.g.

  match $command ~ "^scp (-v )?-t /incoming/(alpha|ftp)"

The ‘!~’ evaluates to ‘true’ if lhs does not match the regular expression in the rhs.

If the regular expression contains parenthesized groups, subsequent commands can refer to the strings that matched the groups using the backreference notation%n’, where n is 1-based index ordinal number of the group in the regular expression (see backreference). The reference ‘%0’ expands to the entire matched string. For example:

rule chdir
  match $command "^cd (.+) && (.+)"
  chdir %1
  set command = %2
  fall-through

It splits the compound command into the working directory and the command itself. Then it remembers the name of the working directory (first parenthesized group – ‘%1’) for changing to it later (see chdir) and resets the command line to the part of the string that follows the ‘&&’ token. Finally, it passes control to another rules (see Fall-through).

GNU Rush – a restricted user shell (split by node):   Section:   Chapter:FastBack: Configuration File   Up: Matching Conditions   FastForward: Default Configuration   Contents: Table of ContentsIndex: Concept Index