Bash script to show conditional else-if (elif) statement.



  • Take into account the main readme for this section. README first.

    Code.

    $ cat learnbash.sh 
    #!/bin/bash
    
    Message="Hello There!"
    read -p "What is your name? " Name
    
    if [ "$Name" == "Josh" ]
    then
      echo "$Message $Name, you can go home early today."
    elif [ "$Name" == "Burt" ]
    then
      echo "$Message $Name, sorry to hear you're unwell, get some rest."
    else
      echo "$Message $Name, you should not be at work today."
    fi
    

    Run the script, with different names.

    $ ./learnbash.sh 
    What is your name? Josh
    Hello There! Josh, you can go home early today.
    $ ./learnbash.sh 
    What is your name? Burt
    Hello There! Burt, sorry to hear you're unwell, get some rest.
    $ ./learnbash.sh 
    What is your name? Jill
    Hello There! Jill, you should not be at work today.
    


© Lightnetics 2024