Welcome to the fourth chapter of BASH scripting series in which we will discuss the topic, decisions. Like a human being making decisions throughout the day, computers too need to decide on the best possible route when executing stuff from time to time. It has to consider whether a certain line of statements is necessary to be executed or not, based on a condition. In computer programming, the condition is usually tested on boolean values; either true or false.
What is a decision
A decision is making do with one of the lists of possible options.
Suppose you are driving to a store and is finding a parking spot for your car.
Not able to find one since all of them are currently occupied, you can decide to either:
Decision in BASH
In BASH there are three decision statements to control the flow of your program, namely: the if statement, the else statement, and the else-if statement.
The if statement first test whether something is true before executing some line of code. A typical dirty code for the if statement is shown below:
if [[ test == true ]] ; then some code fi
Suppose, you are in a hurry and have not enough patience to drive around the store until someone's car pulls out. In such a situation, it is ideal to drive to another store because there is no guarantee that someone would pull out very soon. And waiting might take hours!
Thus, the pseudocode for driving to another store would look like:
if [[ no_parking_spot == true ]] ; then drive_to_another_store fi
The else statement is used to execute some other line of code in case the if statement part fails.
if [[ test == true ]] ; then some code else other code fi
Considering the same above parking spot scenario, this time you are free and have all the time to yourself. You have the flexibility to choose to either drive around or switch to another store. But to add some realistic decision, let's assume the next store is far away and you'd have to drive to another town. Plus, you dislike driving so far away.
Instead, this time you analyze the location of the next store then decide whether to switch to it or drive around while waiting for someone to pull out. Below is the pseudocode implemented using nested if (an if within an if) statement.
if [[ no_parking_spot == true ]] ; then if [[ next_store_is_not_far == true ]] ; then drive_to_next_store fi else drive_around fi
This is the else-if statement in BASH. This statement is used when there will be multiple options (more than two) to be tested against.
if [[ test1 == true ]] ; then some code elif [[ test2 == true ]] ; then other code else another code fi
The best use case of the else-if statement usually explained on the most computer science textbook is grading the student's performance according to his/her percentage score.
if [[ percentage -gt 90 ]] ; then echo Grade A elif [[ percentage -gt 75 ]] ; then echo Grade B elif [[ percentage -gt 55 ]] ; then echo Grade C else echo Need improvement fi
We will experiment the conditional statements based on a real-life scenario; voting eligibility. The program will ask for the user's age and then evaluate if he/she is eligible to vote.
#!/bin/bash echo Enter your age: read age if [[ $age == 18 || $age -gt 18 ]] ; then echo You are eligible for voting.
In the above example, if statement the || is called the logical OR operator. This statement first checks if the user's age is equal to 18 and if it yields false, it'll check if his/her age is greater than 18. And if either one of them yields true, then the user is informed that he/she is eligible to vote.
#!/bin/bash echo Enter your age: read age if [[ $age == 18 || $age -gt 18 ]] ; then echo You are eligible for voting. else echo You are not eligible for voting. fi
This time we have the else part in our voting eligibility program which informs the user is not currently eligible to vote in case the if part fails.
#!/bin/bash echo Enter your age: read age if [[ $age == 18 ]] ; then echo Welcome new voter. elif [[ $age -gt 18 ]] ; then echo You are eligible for voting. else echo You are not eligible for voting. fi
This is the same program from the above two examples but the if statement part is broken down to two parts:
Decision in computer programming is crucial for controlling the flow of the program's execution. In the earlier program examples, the order of execution has been in one direction only; downward ie., from top to bottom. Using conditional statements like if, else-if, and if-else-if changes the flow of the program's execution by branching out from the main path, making the program flexible and intelligent :)
In the next chapter, we will discuss more about operators and the confusing difference between two equal to = and == operators. Till then, have a nice day.
Most Read Articles
Translate Our Site
Notice : Please be careful, after translation commands will change.
This website uses marketing and tracking technologies. Opting out of this will opt you out of all cookies, except for those needed to run the website. Note that some products may not work as well without tracking cookies.Opt Out of Cookies