How to use if-else statement in Linux shell environment.

Introduction:

While using the Linux OS, the shell script is one of the most useful ways to automate our day-to-day tasks. In such scripts, we require logic building, using if-else statement in a Linux shell script that executes tasks on the basis of conditions. In this session we will understand how we can build a basic logic using if-else statements in a Linux shell environment.

Syntax of if-else statement in Linux:

Here is a basic and most commonly used syntax:

if condition:
if [ 1 -gt 0 ] ; then
 echo "1 is greater than 0"
fi

Here is a breakdown:

  • if: Starts the condition.
  • [ 1 -gt 0 ]: Checks if 1 is greater than 0.
    • -gt means greater than.
  • then If the condition is true, run the next command.
  • echo "1 is greater than 0": This message will be shown.
  • fi: Ends the if block.
if and else conditions:
if [ $NUM -gt 0 ] ; then
 echo "$NUM is greater than 0"
else
 echo "$NUM is lower than 0"
fi

Here is a breakdown:

  • if [ $NUM -gt 0 ]:
    • Checks if the variable $NUM is greater than 0.
    • -gt means “greater than“.
  • then:
    • If the condition is true, the next command runs.
  • echo "$NUM is greater than 0":
    • Prints the value of NUM with the message.
  • else:
    • If the condition is not true (i.e., 0 or negative), this part runs.
  • echo "$NUM is lower than 0":
    • Shows a message for non-positive numbers.
  • fi:
    • Marks the end of the if-else block.
if, elif, and else conditions:
if [ "$num" -gt 0 ]; then
  echo "The number is positive"
elif [ "$num" -lt 0 ]; then
  echo "The number is negative"
else
  echo "The number is zero"
fi

Here is a breakdown:

  • if [ "$num" -gt 0 ]; then
    • Checks if the value of the variable num is greater than 0.
    • -gt means “greater than”.
  • echo "The number is positive"
    • This line runs only if the above condition is true.
    • It prints that the number is positive.
  • elif [ "$num" -lt 0 ]; then
    • If the first condition is false, this checks if num is less than 0.
    • -lt means “less than”.
  • echo "The number is negative"
    • Executes if the elif condition is true.
    • It prints that the number is negative.
  • else
    • This runs if none of the previous conditions are true.
    • That means the number is exactly 0.
  • echo "The number is zero"
    • Prints that the number is zero.
  • fi
    • Ends the entire if-elif-else block.

File Checks with if-else in Linux

File checks are common when using if-else statement in Linux shell scripts. Here are few of them which are most useful while using shell scripts.

-a file or -e file to check if a file exists:
if [ -e /etc/passwd ]; then
  echo "File exists"
fi

OR

if [ -a /etc/passwd ]; then
  echo "File exists"
fi
-d file to check if it is a directory.
if [ -d /home ]; then
  echo "/home is a directory"
fi
-f file to check if it is file type.
if [ -f /etc/passwd ]; then
  echo "/etc/passwd is a regular file"
fi 
-s file to check if the file is not empty.
if [ -s /etc/passwd ]; then
  echo "/etc/passwd is not empty"
fi

Using ! in if-else statement:

While using any condition like file operations, calculations, etc. the ! helps us to get false statement to execute script for an example:

if [ ! -f /etc/sudoers ] ; then 
   echo "/etc/sudoers does not exist the user permissions got corrupted"
else
   echo "/etc/sudoers exists, everything is working fine"
fi
  • [ ! -f /etc/sudoers ]: This checks if the file does not exist.
  • If the file is missing, the script prints a warning about user permissions.
  • If the file exists, it confirms everything is working as expected.

NOTE: If the condition is true, the ! makes it false.

Using && and || in if-else conditions:

As we know, the if condition allows us to control the flow of a script based on certain criteria. We can combine multiple conditions using the && (AND) and || (OR) operators.

The key difference between these two symbols is that

  1. && ensures all conditions must be true.
  2. || executes the next command if any one condition is true (often used when a condition fails).

Here’s an example script:

if [ -f /etc/passwd ] && [ -d /etc/sudoers.d ] ; then
    echo "System is configured correctly."
else
    echo "System configuration is incorrect."
fi

In the above script condition, it is checking whether both /etc/passwd file and /etc/sudoers.d directory is present or not. if both conditions are true, then only it will execute echo "System is configured correctly." else it will execute echo "System configuration is incorrect."

Just like the && operator requires all conditions to be true, the || operator proceeds if at least one condition is true. If all conditions are false, the command after || will not execute.

Example using ||:

if [ -f /etc/passwd ] || [ -f /etc/shadow ]; then
    echo "At least one of the critical system files exists."
else
    echo "Neither file exists. System may be misconfigured."
fi

In the above script, it will check if at least one condition must be true. After Validating it will show the output.

If /etc/passwd exists, the first condition becomes true, and it will execute. At least one of the critical system files exists. Similarly, if only one /etc/shadow exists, the second condition becomes true and it prints the same message, but if neither file exists, both conditions are false, and the script prints “Neither file exists. System may be misconfigured.

Conclusion:

In conclusion, understanding how to use if-else statement in Linux is essential for anyone learning shell scripting or managing Linux systems. By mastering conditional logic with if, elif, and else, as well as operators like &&, ||, and !, you can create powerful scripts to automate tasks, perform file checks, and make real-time decisions. These foundational skills are vital for improving workflow efficiency and writing robust Linux shell scripts that behave intelligently under different conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *