Wiki source for CommandTest

Show raw source

======Command test======

**test** expr
**[** expr **]**

The test command evaluates an expression and returns true or false (0,1) as its exit value.
The test command is aliased as "[" which is the typical use.

An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true
or false and sets exit status. It is one of:



both EXPRESSION1 and EXPRESSION2 are true

either EXPRESSION1 or EXPRESSION2 is true

**-n STRING**
the length of STRING is nonzero

equivalent to -n STRING

**-z STRING**
the length of STRING is zero

**-D name**
true if the environment variable "name" is defined

**-S value**
true if the value/argument is a string //type //(not an xml type)

**-X value**
true if the value/argument is an xml //type//

the strings are equal

the strings are not equal

INTEGER1 is equal to INTEGER2

INTEGER1 is greater than or equal to INTEGER2

INTEGER1 is greater than INTEGER2

INTEGER1 is less than or equal to INTEGER2

INTEGER1 is less than INTEGER2

INTEGER1 is not equal to INTEGER2

**FILE1 -ef FILE2**
FILE1 and FILE2 have the same canonical path

**FILE1 -nt FILE2**
FILE1 is newer (modification date) than FILE2

**FILE1 -ot FILE2**
FILE1 is older than FILE2

**-d FILE**
FILE exists and is a directory

**-e FILE**
FILE exists

**-f FILE**
FILE exists and is a regular file

**-r FILE**
FILE exists and read permission is granted

**-s FILE**
FILE exists and has a size greater than zero

**-w FILE**
FILE exists and write permission is granted

**-x FILE**
FILE exists and execute (or search) permission is granted

**-u string**
TRUE if string is formated as an absolute URI (starts with <scheme>: )

Beware that parentheses need to be escaped (e.g., by back-
slashes) for shells. INTEGER may also be -l STRING, which evaluates to
the length of STRING.

====XML Expressions====

An XML Expression within the test command is evaluated to boolean and the result used for the expression.
A simplistic example

[ <[ fn:true() ]> ] && echo true

A more interesting example

a=<[ <foo attr="bar"/> ]>
if [ <[ $a/@attr eq 'bar' ]> ] ; then
echo attr is bar

Note the spaces needed before and after the [ and ]


XDM Sequences are evaluated as "Effective Boolean" but only if they appear as a single value. This can cause runtime
errors if you dont know ahead of time if a sequence is 0,1 or more.

The following is evalutated according to the [ Effective Boolean] rule of XPath.
In this case a sequence of non atomic elements of 1 or more is "true"
x=<[ <foo/> ]> # XML sequence of 1
[ $x ] && echo true


One might expect this also to work
x=<[ <foo/> , <bar/> ]>
[ $x ] && echo true
Results in
**{{color fg="#FF0000" text="[:Unexpected xml value operator"}} **

The problem is that $x is expanded in command mode and evaluates to 2 arguments not one. The resulting expression is equivalent to

[ <foo/> <bar/> ] && echo true

Which the ""["" command doesnt expect. To get the desired results either wrap the expression in an XQuery expression and XPath expression that evaluates to an atomic, like exists()
or quote the variable with {} which prevents expansion into multple words, preserving the value as a single arugment of type sequence.

Either of these will work


x=<[ <foo/> , <bar/> ]>
[ <[ exists( $x ) ]> ] && echo true

x=<[ <foo/> , <bar/> ]>
[ {$x} ] && echo true


Be aware that the former expression only returns true if $x is not empty, wheras the later uses the [ Effective Boolean] rules.
For example the following test evaluates false even though the list is not empty
x=<[ fn:false() ]>
[ {$x} ] && echo This will not echo

if you really want to tell if a list is empty use the former version or equivilent (such as ""count($x) gt 0)"" ) or use the variable sequence count expression

[ ${#x} -ne 0 ] && echo The list is not empty

You can also convert the result to a string and compare for empty using -n or -z. For XML expressions the string serialization rules will be used which
does not always produce a non empty string for sequences (such as attributes) and can also produce very large strings which are inefficient to serialize.


x=<[ <foo a=""/>/@a ]>
echo \$x is "$x" count ${#x}
[ -z "$x" ] && echo true || echo false
y=<[ $x/string() ]>
echo \$y is "$y" count ${#y}
[ -z "$y" ] && echo true || echo false

$x is a="" count 1
$y is count 1

This is because $x is an attribute node that when implicitly serialized produces a="" but when explicitly converted to a string using the string() function produces "" (an empty string)

[[ CategoryCommands]]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki