Command test


Synopsis

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:

( EXPRESSION )
EXPRESSION is true

! EXPRESSION
EXPRESSION is false

EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true

EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true

-n STRING
the length of STRING is nonzero

STRING
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


STRING1 = STRING2
the strings are equal

STRING1 != STRING2
the strings are not equal

INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2

INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2

INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2

INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2

INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2

INTEGER1 -ne 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
fi


Note the spaces needed before and after the [ and ]

Sequences


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.

Example:
The following is evalutated according to the [http://www.w3.org/TR/xpath20/#id-ebv 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
[: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 [http://www.w3.org/TR/xpath20/#id-ebv 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.

Examples:

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 


Result
$x is a="" count 1
false
$y is  count 1
true


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)









Commands
CategoryCommands
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki