Linux Shell Scripting : Logical Operator

Linux Shell Scripting : Logical Operator

What is Shell Scripting?

Shell scripting refers to writing scripts or programs that are interpreted and executed by a shell, which is a command-line interpreter for operating systems like Unix, Linux, and macOS. The shell is an interface between the user and the operating system, allowing users to execute commands and perform various tasks.

A shell script is a text file containing a series of commands written in a scripting language specific to the shell being used, such as Bash (Bourne Again SHell) or C Shell (csh). These scripts can automate repetitive tasks, perform system administration tasks, or combine multiple commands into a single script for more complex operations.

Logical Operators

Logical operators in Linux shell scripting are used to perform logical operations on conditions or expressions. They are typically used in conditional statements and control flow constructs to determine the outcome of the conditions. Here are the logical operators commonly used in Linux shell scripting:

  1. AND operator (&&):

    • Syntax: command1 && command2

    • Description: The AND operator returns true (exit status 0) if both command1 and command2 are successful (exit status 0). If either of them fails, the AND operator returns false (non-zero exit status).

  2. OR operator (||):

    • Syntax: command1 || command2

    • Description: The OR operator returns true (exit status 0) if either command1 or command2 is successful (exit status 0). If both commands fail, the OR operator returns false (non-zero exit status).

  3. NOT operator (!):

    • Syntax: ! command

    • Description: The NOT operator negates the exit status of the command. If the command succeeds (exit status 0), the NOT operator returns false (non-zero exit status). If the command fails (non-zero exit status), the NOT operator returns true (exit status 0).

Logical operators are commonly used in if statements, while loops and other control structures make decisions based on the outcome of conditions. They allow you to combine multiple conditions and control the flow of your shell script accordingly.

AND Operator

In the below example, we will learn to create a shell script that will show us how an AND Operator works.

Creating Shell Script

  • Create a file using the touch command.

  • Use the extension (.sh)

  • Use the vi command to edit the script.

  • Write the shebang on the top. (#!/bin/bash).

  • We will use the read -p command to take the input with the prompt.

  • The if condition is used to check whether the input is equal to 0 or not.

  • Now we will write the AND condition in the elif.

  • Now you can use the bash command with the file name to Run the Script.

  • Here, you can see the results with different arguments.

OR Operator

In the below example, we will learn to create a shell script that will show us how an AND Operator works.

Creating Shell Script

  • Create a file using the touch command.

  • Use the extension (.sh)

  • Use the vi command to edit the script.

  • Write the shebang on the top. (#!/bin/bash).

  • We will use the read -p command to take the input with the prompt.

  • The if condition is used to check whether the input is equal to 0 or not.

  • Now we will write the OR condition in the elif. If one of the situations is true the echo will print the provided statement.

  • The else is written if the input doesn't match any condition.

  • We will type fi to end the function.

  • Now you can use the bash command with the file name to Run the Script.

  • Here, you can see the results with different arguments.