Skip to main content
Engineering LibreTexts

13-D.10: Shell Control Statements

  • Page ID
    43210
  • EXAM OBJECTIVES COVERED
    5.1 Given a scenario, deploy and execute basic Bash scripts.

    There are three components that we need to understand in any structured programming methodology:

    1. Sequential execution
    2. Conditional execution
    3. Looping constructs

    Each of these will be discussed as we learn how to implement control structures in a shell script.

    Sequential Execution

    Sequential execution means that each command in a program script executes in the order in which it is listed in the program. The first command in the sequence executes first and when it is complete, the second command executes, and so on. The presence of functions in the code does not negate sequential execution; we can still follow the sequential flow of the instructions. As technology advances there is more parallel programming being run on smaller computers, but how that impacts sequential execution is beyond the scope of this course.

    Conditional Execution

    Conditional Statements: There are a total of five conditional statements which can be used in bash programming.

    1. if statement
    2. if-else statement
    3. if..elif..else..fi statement (Else If ladder)
    4. if..then..else..if..then..fi..fi..(Nested if)
    5. switch statement

    Their description with syntax is as follows:

    if statement

    This block will process if specified condition is true.
    Syntax:

    if [ expression ]
    then
       statement
    fi 

    An example of the code:

    #Initializing two variables 
    a=20 
    b=20 
    
    if [ $a == $b ] 
    then 
        #If they are equal then print this 
        echo "a is equal to b"
    else
        #else print this 
        echo "a is not equal to b"
    fi 
    

    if-else statement

    If specified condition is not true in if part then else part will be executed.
    Syntax:

    if [ expression ]
    then
       statement1
    else
       statement2
    fi 

    if..elif..else..fi statement (Else If ladder)

    To use multiple conditions in one if-else block, then the elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the conditions is true then it processes else part.
    Syntax:

    if [ expression1 ]
    then
       statement1
       statement2
       .
       .
    elif [ expression2 ]
    then
       statement3
       statement4
       .
       .
    else
       statement5
    fi 

    Switch Statement

    A case statement works as a switch statement, if specified values match with the pattern then it will execute a block of that particular pattern. When a match is found all of the associated statements until the double semicolon (;;) are executed. A case will be terminated when the last command is executed. If there is no match, the exit status of the case is zero.

    Syntax:

    case in
    Pattern 1) Statement 1;;
    Pattern n) Statement n;;
    esac 

    An example of a switch statement is shown below. Lines beginning with a hash tag are comments.

    CARS="bmw"
    
    #Pass the variable in string 
    case "$CARS" in 
        #case 1 
        "mercedes") echo "Headquarters - Affalterbach, Germany" ;; 
        
        #case 2 
        "audi") echo "Headquarters - Ingolstadt, Germany" ;; 
        
        #case 3 
        "bmw") echo "Headquarters - Chennai, Tamil Nadu, India" ;; 
    esac 
    

    Test Statement

    These operators are used to test a particular property of a file.

    • -b operator: This operator checks whether a file is a block special file or not. It returns true if the file is a block special file otherwise returns false.
    • -c operator: This operator checks whether a file is a character special file or not. It returns true if it is a character special file otherwise returns false.
    • -d operator: This operator checks if the given directory exists or not. If it exists then operators returns true otherwise returns false.
    • -e operator: This operator checks whether the given file exists or not. If it exists this operator returns true otherwise returns false.
    • -r operator: This operator checks whether the given file has read access or not. If it has read access then it returns true otherwise returns false.
    • -w operator: This operator checks whether the given file has write access or not. If it has write then it returns true otherwise returns false.
    • -x operator: This operator checks whether the given file has execute access or not. If it has execute access then it returns true otherwise returns false.
    • -s operator: This operator checks the size of the given file. If the size of given file is greater than 0 then it returns true otherwise it returns false.
    #!/bin/bash 
    
    #reading data from the user 
    FileName=/home/pbmac
    
    if [ -d $FileName ] 
    then 
        echo "The directory $FileName exists"
    else
        echo "The directory $FileName does NOT exists"
    fi 
    

    Adapted from:
    "Conditional Statements | Shell Script" by bilal-hungund, Geeks for Geeks is licensed under CC BY-SA 4.0
    "Basic Operators in Shell Scripting" by DrRoot_, Geeks for Geeks is licensed under CC BY-SA 4.0

    • Was this article helpful?