Bash IF statements

In computer programs sometimes you need something to happen if a condition is fulfilled. Other times if the condition is not fulfilled the program has to do something. In programming IF statements is one of the ways that is used to have a computer do multiple tasks based on a variable outcome. Here’s an example using a small bash script I wrote.

This script shows the user three options and prompts the user for a response. Either 1, 2, or 3.

If 1 is chosen then the computer will display the word “Yes”

If 2 is chosen the the word “No” will display.

If 3 is chosen then the program will quit.

Here's the first part of the script.
Here’s the first part of the script.
Here's the second part of the script.
Here’s the second part of the script.

Here’s the breakdown of the script:

  • shebang (#!/bin/bash)
    • This is telling the OS which programming language I’m using. In this case it’s the built in bash scripting language.
  • The text in blue is just comments for the programmers reference
  • I have the program echo or display the options to choose from to the user
  • The read command take user’s input and puts it into a variable. In this case the variable name is option1
  • Next are my IF statements. These statements use the option1 variable as a condition to check and see what code to execute. For example if the user selects 1 then the first IF statement will execute because option1 (which is the user’s choice) equals 1.
    • The IF statements require several things in order to work right:
      • The condition
      • Brackets to encase the condition [[ ]]
      • Spaces between the brackets and the conditional
      • A semicolon at the end of the brackets
      • A then statement
      • Code to execute if the condition is met
      • A fi statement to end the IF statement

You will notice that all of the IF statements in this script have all of these.

Here’s the script in action using all of the options available to choose from.

option 3 part 1

option 3 part 2

option1 part 1

option1 part 2

option2 part 1

option2 part 2

Depending on what option I give the program it will either display Yes, No, or quit the program with a termination message. This is the power of IF statements. A programmer can have the computer test a large amount of possible options all within one program. Great stuff!

Another option is to use ELIFs and the ELSE statement with the IF statement. With ELIF (Else If) I can combine multiple conditionals into one statement rather then using multiple IF statements. The ELSE  statement will execute if the conditional for the IF statement is not met. To show this I’m going to modify the top script with ELIFs and an ELSE statement.

mod script part 1
Not only did I modify the script but I’m also using a different text editor. The editor is called vim which stands for vi improved. Vi is included in every distro of Linux.
mod script part 2
Instead of having multiple separate IF statements I now only have one huge statement that houses all of the conditions I want the program to evaluate. If any text other the the numbers 1, 2, and 3 are chosen the program will quit with a termination message. I also modified the user prompt using the -p (prompt) option with the read command. 

invaild input part 1

invalid input part 2

For more information on bash IF statements see:

Thanks for reading!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s