Shell Scripting Conditions and if Command

The if command allows you to execute a block of code only if a specified condition is true. The syntax of the if command is as follows:

if CONDITION then
   # Code to execute if the condition is true
fi

The condition can be any command that returns a exit status of 0 (true) or 1 (false). The then keyword indicates the start of the code block that will be executed if the condition is true. The fi keyword indicates the end of the code block.

The CONDITION can be any test or comparison that returns a boolean value (true or false). For example, you can use the test command or the [ command to evaluate a condition, like this:

if test -e /path/to/file
then
   # Code to execute if the file exists
fi
if [ -z "$STRING" ] then
   # Code to execute if the string is empty
fi

Here is another example of how to use the if command:

#!/bin/bash
# Declare a variable and assign it a value
A=5
# Test the value of the variable
if [ $A -gt 3 ] then
   # Execute this block of code if the test is true
   echo "A is greater than 3"
fi

 

This script will print the following output:
A is greater than 3

You can also use the if command in conjunction with the else and elif (else-if) keywords to specify multiple conditions and code blocks. For example:

#!/bin/bash
# Declare a variable and assign it a value
A=5
# Test the value of the variable
if [ $A -gt 10 ]
then
# Execute this block of code if the first test is true
echo "A is greater than 10"
elif [ $A -gt 5 ]
then
# Execute this block of code if the second test is true
echo "A is greater than 5 but not greater than 10"
else
# Execute this block of code if all tests are false
echo "A is not greater than 5"
fi

This script will print the following output:

A is greater than 5 but not greater than 10

You can also use the && operator to chain multiple conditions together, like this:

if [ -f /path/to/file ] && [ -w /path/to/file ]
then
   # Code to execute if the file exists and is writable
fi

You can use the || operator to chain multiple conditions together in an if statement, such that the code block is executed if any one of the conditions is true.

Here is an example of how to use the || operator in an if statement:

#!/bin/bash
# Declare a variable and assign it a value
A=5
# Test the value of the variable
if [ $A -gt 10 ] || [ $A -lt 3 ] then
   # Execute this block of code if either condition is true
   echo "A is either greater than 10 or less than 3"
else
   # Execute this block of code if both conditions are false
   echo "A is between 3 and 10"
fi

This script will print the following output:

A is between 3 and 10

Note that the || operator is short-circuiting, which means that the second condition will not be evaluated if the first condition is true. This can be useful for optimizing your scripts by avoiding unnecessary tests.

if condition to check numerical values

you can use the [ command to perform numerical comparisons in an if statement. Here are some examples of the different numerical comparison operators that you can use:

  1. -eq: Check if the two numbers are equal.
  2. -ne: Check if the two numbers are not equal.
  3. -lt: Check if the first number is less than the second number.
  4. -le: Check if the first number is less than or equal to the second number.
  5. -gt: Check if the first number is greater than the second number.
  6. -ge: Check if the first number is greater than or equal to the second number.

Here is an example of how to use the [ command with one of these numerical comparison operators:

# Check if the variable $num is greater than 10
if [ $num -gt 10 ]
then
   # Execute this block of code if the condition is true
   echo "The number is greater than 10"
else
   # Execute this block of code if the condition is false
   echo "The number is not greater than 10"
fi

 

if condition to check regular expressions

you can use the =~ operator to check if a string matches a regular expression pattern. The =~ operator is part of the [[ command, which is used to evaluate a condition in an if statement.

Here is an example of how to use the =~ operator to check a regular expression pattern in an if statement:

# Declare a variable and assign it a value
string="Hello, world!"
# Check if the string matches the pattern "Hello,*"
if [[ $string =~ "Hello,*" ]] then
   # Execute this block of code if the string matches the pattern
   echo "The string matches the pattern"
else
   # Execute this block of code if the string does not match the pattern
   echo "The string does not match the pattern"
fi

This script will print the following output:

The string matches the pattern

Note that you must use double brackets ([[ and ]]) to use the =~ operator. Single brackets ([ and ]) do not support the =~ operator.

the [ and [[ commands are used to evaluate a condition. They are similar in many ways, but there are a few key differences that you should be aware of:

  1. Syntax: The [ command uses a single set of brackets, whereas the [[ command uses a double set of brackets. This can be a source of confusion for some users, as the double brackets can look similar to the test command.
  2. Regular expression support: The [[ command supports the =~ operator, which allows you to use regular expressions to match a pattern in a string. The [ command does not support this operator.
  3. Word splitting: The [ command performs word splitting on the arguments, which means that it can treat multiple words as separate arguments. The [[ command does not perform word splitting, which can be more predictable and easier to use in some cases.
  4. Double quotes: The [ command requires double quotes around the expression if it contains variables or other special characters. The [[ command does not require quotes in most cases, but they can be used if needed.

if condition for files and directories check

you can use the [ command to perform tests on files and directories in an if statement. Here are some examples of the different file and directory tests that you can use:

  1. -d dir: Check if the file is a directory.
  2. -e file: Check if the file exists.
  3. -f file: Check if the file is a regular file.
  4. -L file: Check if the file is a symbolic link.
  5. -r file: Check if the file is readable.
  6. -s file: Check if the file has a size greater than zero.
  7. -w file: Check if the file is writable.
  8. -x file: Check if the file is executable.
  9. -h file: Check if the file is a symbolic link.

Here is an example of how to use the [ command with one of these file and directory tests:

# Check if the file "/path/to/file" is a directory
if [ -d /path/to/file ] then

   # Execute this block of code if the file is a directory
   echo "The file is a directory"
else
   # Execute this block of code if the file is not a directory
   echo "The file is not a directory"
fi

Summary:
The if command in the Unix shell allows you to execute a block of code only if a specified condition is true. The condition can be any command that returns a exit status of 0 (true) or 1 (false), and can be any test or comparison that returns a boolean value. The then keyword indicates the start of the code block that will be executed if the condition is true, and the fi keyword indicates the end of the block. You can use the else and elif (else-if) keywords to specify multiple conditions and code blocks. The && operator can be used to chain multiple conditions together in an if statement, such that the code block is executed if all conditions are true, and the || operator can be used to chain multiple conditions together such that the code block is executed if any one of the conditions is true. The =~ operator can be used with the [[ command to check if a string matches a regular expression pattern in an if statement. The [ and [[ commands are used to evaluate a condition in an if statement, with [[ being a more advanced version that supports additional features such as the =~ operator.

Questions for review:

  1. What is the syntax of the if command in Bash?
  2. What is the purpose of the then keyword in an if command in Bash?
  3. How can you specify multiple conditions in an if command using the else-if keyword in Bash?
  4. How can you chain multiple conditions together in an if command using the && operator in Bash?
  5. How can you chain multiple conditions together in an if command using the || operator in Bash?
  6. How can you perform numerical comparisons in an if command using the [ command in Bash?
  7. How can you use regular expressions in an if command in Bash?
  8. Can you provide an example of using the if command in Bash to check if a file exists?

 

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book