# Schell Scripting Operators and Programming Constructs

In shell scripting, you can use various comparison operators to compare values. Here are some commonly used ones:

1. **Numeric Comparison:**
    
    \-eq : Equal to
    
    \-ne : Not equal to
    
    \-lt : Less than
    
    \-le : Less than or equal to
    
    \-gt : Greater than
    
    \-ge : Greater than or equal to
    
2. **String Comparison**:
    
    \= : Equal to
    
    != : Not equal to
    
    &lt; : Less than (alphabetical order)
    
    \&gt;: Greater than (alphabetical order)
    
3. **Logical Operators:**
    
    && : Logical AND
    
    || : Logical OR
    
    ! : Logical NOT
    
4. **File Tests:**
    
    \-e : File exists
    
    \-f : File is a regular file
    
    \-d : File is a directory
    
    \-r : File is readable
    
    \-w : File is writable
    
    \-x : File is executable
    

Shell scripting is a powerful tool for automating tasks and managing system operations efficiently. Understanding the basics of shell scripting, including if conditions, for loops, while loops, and switch cases, is essential for any aspiring system administrator or developer. In this comprehensive guide, we will delve into the fundamentals of these operations and provide practical examples to help understand the shell scripting automation.

1. **Command line argument variables**
    
    Command line argument variables are placeholders that allow scripts or programs to receive input values from the command line when they are executed. These variables store information passed as arguments and enable dynamic behavior, enhancing the flexibility and interactivity of command line applications. In Unix/Linux Bash, `$1`, `$2`, etc.
    
    ```bash
    #!/bin/bash
    
    <<COMMENT1
            Code to showcase command line argument variable
    COMMENT1
    
    # Number of arguments
    echo '$#: ' $#
    
    # Process No. of current process
    echo '$$: ' $$
    
    # Third argument on command line
    echo '$3: ' $3
    
    # Display the name of current shell or program
    echo '$0: ' $0
    
    # Display all the argument in the command line
    echo '$*: ' $*
    echo '$@: ' $@
    ```
    
    Output :
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705345288371/6b581f2a-fed4-4029-8ebd-ec71a1afdc43.png align="center")
    
2. **If Condition**
    
    * The `if` statement is a fundamental control structure in programming. It allows you to execute a block of code if a specified condition is true.
        
        ```bash
        #!/bin/bash
        
        <<COMMENT1
        
                This is a code to check the greatest number amoung the 3 numbers pass as a command line argument.
        
        COMMENT1
        
        echo "Starting the program"
        
        if [ $# -ne 3 ];then
                echo "ERROR :$0 - (number1, number2, number3) are the required command line parameters." >&2
                exit 1
        fi
        
        if [ "$1" -gt "$2" ] && [ "$1" -gt "$3" ]; then
                echo "$1 is the greatest number."
        
        elif [ "$2" -gt "$1" ] && [ "$2" -gt "$3" ]; then
                echo "$2 is the greatest number."
        
        elif [ "$3" -gt "$1" ] && [ "$3" -gt "$2" ]; then
                echo "$3 is the greatest number."
        
        else
                echo "All the 3 numbers are equal."
        fi
        ```
        
        Output :
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705344742418/4d20550f-a0fe-4506-8a22-ec0704c65fe6.png align="left")
        
3. **File managment**
    
    ```bash
    #!/bin/bash
    
    <<COMMENT1
            Code to showcase use of if condition in shell scripting
    COMMENT1
    
    echo "Enter the file name."
    read filename
    
    if [ -f $filename ]; then # file is present
            if [ -w $filename ]; then # is file writable
    
                    echo "Enter the content you want to add in the file. To quit type Ctrl + d"
                    cat >> $filename
    
            else
                    echo "$filename doesn't have write permission."
            fi
    else
            echo "$filename doesn't exists.\n"
    fi
    ```
    
    Output :
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705345844707/3797c73a-e9cd-4b53-8fc3-49411eb357fe.png align="left")
    
4. **For Loop**
    
    The `for` loop in shell scripting is used to iterate through a list of values. The basic syntax is as follows:
    
    ```bash
    #!/bin/bash
    
    <<COMMENT1
            Code to showcase use of for loop in shell scripting
    COMMENT1
    
    # C-style for loop
    for ((i=1; i<5; i++));do
           echo "Iteration : $i"
    done
    
    echo ""
    
    # Define a list of items
    numbers=("1" "2" "3" "4" 5 6 7)
    
    # foreach loop
    for number in "${numbers[@]}"; do # @ reference all elements in an array
            echo "Number : $number"
    done
    ```
    
    Output :
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705346633203/135d56c8-6d8b-40a4-a496-8bbfdb682d50.png align="left")
    
5. **While Loop**
    
    The `while` loop is used to execute a block of code repeatedly as long as a specified condition is true. The syntax is as follows:
    
    ```bash
    #!/bin/bash
    
    <<COMMENT1
            Code to showcase use of while loop in shell scripting - sum of n natural no.
    COMMENT1
    
    count=0
    sum=0
    printf "Enter the value of n :"
    read number
    
    while (( count  <= number )); do  # double parenthesies is use to perform arithmetic operations
            sum=$(expr $sum + $count) # performing arithmetic operations using expr
            ((count++))
    done
    
    echo "Sum is $sum."
    ```
    
    Output :
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705346975821/4e6a139c-99ab-4933-b20d-f74f3e61b942.png align="left")
    
6. **Switch Case**
    
    While shell scripting doesn't have a native `switch` statement, you can achieve similar functionality using a combination of `case` and `esac`. The syntax is as follows:
    
    ```bash
    #!/bin/bash
    
    <<COMMENT1
            Code to showcase use of switch case in shell scripting - Arithmetic Operations.
    COMMENT1
    
    # Display a menu to the user
    echo "Choose an operation:"
    echo "1. Addition"
    echo "2. Subtraction"
    echo "3. Multiplication"
    echo "4. Division"
    echo "5. Quit"
    
    read choice
    
    # Perform calculations based on user choice using case statement
    case $choice in
        1)
            echo "Enter two numbers for addition:"
            printf "Enter number 1 : "
            read num1
            printf "Enter number 2 : "
            read num2
            result=$(expr $num1 + $num2)
            echo "Result of addition: $result"
            ;;
        2)
            echo "Enter two numbers for subtraction:"
            printf "Enter number 1 : "
            read num1
            printf "Enter number 2 : "
            read num2
            result=$(expr $num1 - $num2)
            echo "Result of subtraction: $result"
            ;;
        3)
            echo "Enter two numbers for multiplication:"
            printf "Enter number 1 : "
            read num1
            printf "Enter number 2 : "
            read num2
            result=$(expr $num1 \* $num2)  # Note: \* is used to represent multiplication
            echo "Result of multiplication: $result"
            ;;
        4)
            echo "Enter two numbers for division:"
            printf "Enter number 1 ( Numerator ) : "
            read num1
            printf "Enter number 2 ( Denominator ) : "
            read num2
            result=$(expr $num1 / $num2)
            echo "Result of division: $result"
            ;;
        5)
            echo "Exiting the program."
            exit 0
            ;;
        *)
            echo "Invalid choice. Please choose a number from 1 to 5."
            ;;
    esac
    ```
    
    Output :
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705347218681/a9e7556c-f343-4210-b3c7-67b281454313.png align="left")
