Conditionals

Two main types of conditionals:

  • if
    • note, terminated by fi
  • case
    • note, terminated by esac

Also a pseudo Ternary using && and ||

  • command && next command || or this command

 

if conditionals

Basic if test construction:

if [ condition ]
then
    command
fi

if, then, else construction

if [ condition ]
then
    command
else
    command
fi

if, then, elif construction

if [ condition ]
then
    command
elif [ condition ]
then
    command

else

    command
fi

 

 

 

Case conditionals

 

Case tests several conditions at a time, carrying out the command of the first condition met. If no condition is met, the default case will be carried out as indicated by the final *)

 

 

Pseudo Ternary conditionals

The ternary conditional can be emulated using a one liner with && and || replacing normal ? and :