The Unix "if then else fi" command

iKnow-IT banner

Test and jump

if [ test_condition ] then
…. 
….
fi
if [ test_condition ] then
…. 
….
else
…. 
….
fi
Test and jump (test_conditions)
-r filename => Is the file readable 
-w filename => Is the file writable 
-f filename => The file exists and is not a directory 
-d filename => The file exists and is a directory 
-s filename => The file exists and is greater than 0 
-z string1 => Is "string1" the null string, length = 0 
-n string1 => Is "string1" not "null string", length <> 0 
string1 = string2 => Is "string1" equals "string2" 
string1 != string2 => Is “string1” not equals "string2” 
… -a … => The "logical AND" operator 
… -o … => The "logical OR" operator 
! … => The “NOT” operator 
var1 -comparing var2 comparing can be: eq, ne, gt, ge, lt, le

Suppose is "/home/jasper" a directory AND I may in writing

if [ -d /home/jasper -a -w /home/jasper ] then
….
fi

Suppose: shell variable NUM is a number greater than 0 and less than or equal to 10

if [ $NUM -gt 0 -a $NUM -le 10 ] then
fi