if ... then ... else ... fi ( en )

Last modification : 2013/01/26 19:16

The main conditional block structure is the if ... then ...fi block.

The two syntaxes are
if [ expr ]
then
      command
fi
and with an else expression
if [ expr ]
then
      command 1
else
      command 2
fi

Never forget spaces characters before and after [ and ].

example
msh> set A=100
msh> if [ $A -gt 0 ] ; then
1>       echo \"A is a positive number\"
1> else
1>       echo \"A is a negative or null number\"
1> fi
A is a positive number

To get information about test expressions see test command.

Without else expression the syntax can be shorten as follow [[ ... ]] { ... }.
msh> [[ -f filename ]] print Filename exists
Filename exists