if Command Examples in Linux

In most languages, including Bash, the primary conditional statement is the if statement. An if statement contains a condition to be evaluated and one or more actions to be performed, if the condition is satisfied. If the condition is not satisfied, the actions are skipped and the next statement in the script is executed. In Bash, the end of the set of instructions is indicated by the fi statement.

The following is an example of a simple if statement: var=5

if [ $var -gt 1 ]
then
    echo "$var is greater than 1!"
fi

The if statement includes, between two square brackets, a condition to be evaluated. In this case, it’s whether or not the $var variable is greater than 1. On the next line is the then statement, within which is the code that will be executed if the prior condition is true. Lastly, the fi statement indicates the end of the entire if statement. Because 5 is greater than 1, the message will echo to the screen. If it were not true, then nothing would happen.

The basic syntax of an if statement is as follows:

if [ <condition to be evaluated> ]
then
    <code to execute if condition is true>
fi

The if…else statement

The if…else statement enables a choice between two actions based on the evaluation of a condition. If the condition is satisfied, the first action is performed; otherwise, the action following the else segment is performed. If there are more than two sets of instructions, one or more elif statements may be used to specify alternative sequences of action.

The following is an example of a simple if…else statement: var=1

if [ $var -gt 1 ]
then
    echo "$var is greater than 1!"
else
    echo "$var is less than or equal to 1!"
fi

The value of $var has changed since the previous example, which means that the first echo command won’t execute. Rather than nothing happening, the else statement specifies what will happen if the condition is false: in this case, it is to print a different message to the screen.

Syntax

The basic syntax of an if…else statement is as follows:

if [ <condition to be evaluated> ]
then
    <code to execute if condition is true>
else
    <code to execute if condition is false>
fi

The basic syntax of an if…elif statement is as follows:

if [ <condition to be evaluated> ]
then
    <code to execute if condition is true>
elif [ <other condition to be evaluated> ]
then
    <code to execute if other condition is true>
fi

if Command Examples

1. Execute the specified commands if the condition command’s exit status is zero:

if condition_command
then
  echo "Condition is true"
fi

2. Execute the specified commands if the condition command’s exit status is not zero:

if ! condition_command
then 
  echo "Condition is true"
fi

3. Execute the first specified commands if the condition command’s exit status is zero otherwise execute the second specified commands:

if condition_command
then 
  echo "Condition is true"
else 
  echo "Condition is false"
fi

4. Check whether a [f]ile exists:

if [[ -f path/to/file ]]
then 
  echo "Condition is true"
fi

5. Check whether a [d]irectory exists:

if [[ -d path/to/directory ]]
then 
  echo "Condition is true"
fi

6. Check whether a file or directory [e]xists:

if [[ -e path/to/file_or_directory ]]
then 
  echo "Condition is true"
fi

7. Check whether a variable is defined:

if [[ -n "$variable" ]]
then 
  echo "Condition is true"
fi
Related Post