One thing that you will probably realize early on when planning out a shell script, is that you cannot possibly anticipate every single outcome.
The best that you can do is plan for the outcomes and possibilities that you can anticipate, and funnel the remainders into a catch-all.
This is made possible by the most common control structure: the if statement.
Concept
The concept of an if statement is very simple and straight-forward: If a condition is met, then an action is performed.
The great part is how easy it is to build onto an if statement, making use of multiple instances of “else if”, and ultimately “else”, to handle the various scenarios that may occur.
As an if statement conceptual example, let’s jump back to that shopping list that I wrote for my sister years ago. Although it was written in C, I need only outline the concept of it here, rather than the particular details.
if fabric is lycra 1 meter white 1 meter black elseif fabric is jersey 1/2 meter white 1/2 meter black else no fabric end
As you can see, based on the conditions that are met (or not met), instructions are given for actions to be performed.
Just take that same concept, apply it to Linux commands, and you’re all set!
Syntax
The if statement syntax refers to the rules that govern how the statement is arranged, so that it can be properly interpreted.
You can use the best logic known to mankind and still have a malfunctioning script if your syntax is wrong.
Let’s take a look at two example if statements, and then break them down.
if condition then action(s) to perform fi if condition; then action(s) to perform fi
The statement begins with the word if, after which the condition is specified.
If the condition is true, then the specified actions will be performed.
If the condition is false, then the specified actions will be skipped over.
In some languages, the indenting of the actions is required for the syntax of the control structure. In shell scripting, the indentation is not required, but is strongly encouraged as good coding practice, and to keep things neat and readable.
The statement ends with a backwards if (fi).
As you can see, there are two different ways to indicate the end of the condition and transition into the actions to be performed if the condition is true. Either use a semicolon (;) and then on the same line, or put then on a new line without the semicolon.
Now, using this same basic syntax, the if statement can have additional components to complicate (or simplify) the logic.
if condition then action(s) to perform elif condition then action(s) to perform else action(s) to perform fi
If an if statement condition is true, its actions will be performed, but if it returns false, you can keep on going, with the help of elif, meaning “else if”.
Multiple “elifs” can be used in a single if statement, although there will come a point when a case statement may be better suited to your needs.
When you’ve exhausted all of your “else if” logic, you can catch the remaining possibilities under an “else”, and specify what actions to perform. Note that the “else” part of the statement does not need followed by a “then”.
Our last topic regarding if statement syntax is on the subject of nesting.
Sometimes “else if” is not enough, and it becomes necessary to start a whole new if statement inside of an existing if statement. This is called nesting, and is perfectly acceptable, so long as you take care to complete each separate if statement’s syntax properly and in order. The first statement opened should be the last one closed, and the last one opened should be the first one closed, etc.
If Statement Example
I decided not to explain what this example script does, meaning that you will either have to:
a) Read through and interpret what it does.
b) Run it and find out what it does.
c) Both of the above.
Have fun!
#!/bin/bash read -p "Please Enter An Integer Between 500 and 600: " int1 if (( $int1 >= 500 )) && (( $int1 <= 600 )) then echo "Congrats. You entered the number ${int1}, which is between 500 and 600." else echo "Sorry, you didn't follow the instructions. Game over." exit 0 fi read -p "Please Enter A Sport: " sport if [ ! -z "$sport" ] then echo "You like $sport." if [ $sport = 'soccer' ] then echo "I like soccer, too." elif [ $sport = 'basketball' ]; then echo "My brother likes basketball, too." else echo "I'm sure that other people like $sport, too." fi else echo "Sorry, you didn't follow the instructions. Game over." exit 0 fi exit 0
Conclusion
As you write scripts, you'll find that the if statement opens the door to a number of new and exciting possibilities.
In our next lesson, we'll tackle the case statement, which is similar to multiple if statements all stacked on top of one another.