Linux Shell Scripting : Conditions

Linux Shell Scripting : Conditions

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.

What are Operators?

Operators in Linux shell scripting are symbols or characters used to perform operations on variables, strings, or conditional statements. They are used to manipulate and control the flow of the script. Here are some commonly used operators in Linux shell scripting:

  1. Arithmetic Operators:

    • Addition (+)

    • Subtraction (-)

    • Multiplication (*)

    • Division (/)

    • Modulo (%)

    • Increment (++)

    • Decrement (--)

  2. Comparison Operators:

    • Equal to (==)

    • Not equal to (!=)

    • Greater than (>)

    • Less than (<)

    • Greater than or equal to (>=)

    • Less than or equal to (<=)

  3. String Operators:

    • Concatenation (+)

    • Equality (==)

    • Inequality (!=)

    • Length (#)

  4. Assignment Operators:

    • Assign (=)

    • Add and assign (+=)

    • Subtract and assign (-=)

    • Multiply and assign (*=)

    • Divide and assign (/=)

  5. Logical Operators:

    • AND (&&)

    • OR (||)

    • NOT (!)

  6. Bitwise Operators:

    • Bitwise AND (&)

    • Bitwise OR (|)

    • Bitwise XOR (^)

    • Bitwise left shift (<<)

    • Bitwise right shift (>>)

    • Bitwise complement (~)

  7. Other Operators:

    • File test operators (e.g., -e, -f, -d) to check file properties.

    • Conditional operators (e.g., ?:) for shorthand conditional statements.

These operators allow you to perform calculations, compare values, manipulate strings, assign values, and control the flow of your shell script based on conditions.

Basic if-else Condition

  • Create a file using the touch command.

  • Use the extension (.sh).

  • Use the vi command with the file name to write the script in the file.

  • Write the shebang on the top of the script.

  • Then write the if command, Here we have used the special variable ($1) for input.

  • We are checking that the input is equal to 2 or not.

  • If the input is equal to the 2.

  • we will use then and echo to print the message.

  • If the input is not equal to 2.

  • Then we will use the else with echo to print the message.

  • To end the script we will type fi.

  • To run the Script we will use the bash command with the file name.

  • You can see the results.

Basic if-elif Condition

  • Create a new file using touch command.

  • Use vi command to edit the script.

  • It is same as if-else just a slight change is applied here.

  • Here, the conditions are more than 1.

  • We have 1st condition in if.

  • Then the other conditions are in elif.

  • Use the bash command to Run the Script.

  • In the above screenshot you can see the results.