Combining tests

You can combine multiple tests and check them using one if statement.

This is done using the AND (&&) and OR (||) commands:

#!/bin/bash 
mydir=/home/mydir 
name="mokhtar" 
if [ -d $mydir ] && [ -n $name ]; then 
   echo "The name is not zero length and the directory exists." 
else 
echo "One of the tests failed." 
fi 

The if statement performs two checks, it checks if the directory exists and that the name is not of zero length.

The two tests must return success (zero) to evaluate the next echo command.

If one of them fails, the if statement goes to the else clause.

Unlike the OR (||) command, if any of the tests returns success (zero), the if statement succeeds.

#!/bin/bash 
mydir=/home/mydir 
name="mokhtar" 
if [ -d $mydir ] || [ -n $name ]; then 
   echo "One of tests or both successes" 
else 
echo "Both failed" 
fi  

It is clear enough that if one of the tests returns true, the if statement returns true for the combined tests.