Revision [1601]
This is an old revision of HowToConditional made by RaymondYee on 2010-12-01 06:58:34.
How To test a condition
Conditions can be tested with either the boolean operations || and "&&" or with the if/elif/else/then clauses.
The conditional test the return status of commands. If the command returns 0 then that is considered "true", otherwise the condition is "false". A common command used for conditionals is the "test" command, usually using the "[ test ]" syntax.
Boolean Operations
If you only need execute a single command or a small block then boolean operations are the easiest way
for example
if "command" returns successfully then print "is success"
command && echo is success
If the file "myfile.txt" exists then read the first line from the file and print it.
[ -e myfile.txt ] && { read VAR < myfile.txt ; echo $VAR ; }
Structured Conditionals (if / elif / else / fi )
If the condition or conditional operations are more complicated then the structured conditionals are more useful.
This is the if / elif / else / fi construct.
The general form is
if condition1 ; then condition1 commands elif condition2 ; then condition2 commands else otherwise commands fi
The "elif" and "else" parts are optional.
For example, if the file "myfile.xml" exists then read it into a variable otherwise print an error message
if [ -f myfile.xml ] ; then xread var < myfile.xml else echo File myfile.xml does not exist fi
XML Expressions can also be used as conditons HowToXMLCondition
HowTo
CategoryHowTo