Control flow (Decision construct) in shell scripting enable the program to execute some block of code based on some conditions evaluating to true or false. Control statements in shell is generally achieved by use of if statements and its derivatives. In this article, we are going to walk you throughout the process of writing your first control structure.
Creating a script file with correct exension
Shell scripting can be done in particular shell implementation like Bash, Zsh, Ksh and sh.
Each shell has its specific file extension like .sh, .zsh or .ksh. Therefore, you have to know what
shell is powering your terminal before you name your file. To do so execute the following command:
echo $0 # prints the special variable representing name of current program, in this case the shell
After ascertaining the current shell environment, create a script file with corresponding extension.
For example, bash has the extension .sh, and zsh has the extension .zsh.
Including a shebang and the file path
Shebang (combination of octothrope/hash and exclamation, #!) is an interpreter directive which is
followed by a path to the interpreter/shell program you want to execute the script. It is not strictly
required but considered good practice because it ensures portability and predictability.
Since we already know the shell program, we then use it to get its path:
which bash # replace bash with your shell
echo $SHELL #prints the file path to the shell
After determining the path, open the file using your preferred text editor:
Include the shebang followed closely by the file path at the top of the file:
#!/bin/bash
Execute to test
Add an
echo “We good so far”
Since systems file permissions prevents unauthorized read, writes and execution of files, you need to grant execute permissions to the shell program:
chmod o+x script.sh #grant every user execution permissions.
You can then execute the file either by specifying the shell program (repetition if you included shebang) or let the shebang direct.
sh script.sh
./script.sh
The test string will be printed on the terminal indicating successful creation of script file.
Control Flow Statements in Shell
There are three control flow statements in shell program and both are based on an a expression
(mostly between square brackets unless implemented in C-style) evaluating to boolean value.
An exit status of zero means true while all non-zero status means false. All
Operators for Control Flow
Conditions for the
| Operator | Stands for |
|---|---|
| -eq | equal to |
| -ne | not equal to |
| -lt | less than |
| -gt | greater than |
| -le | less than or eqqual to |
| -ge | greater than or equal to |
Other shell operators like string comparison operators and file test operators can also be used within the square brackets.
a. if ... then
The condition between the square brackets executed and
if [2 -gt 1]; then
echo “Two is greater than one”
fi
b. if … else
The condition between the square brackets executed and if the exit status is zero, the shell command inside
the
if [2 -gt 3]; then
echo “Two is greater than three”
else
echo “Two is less than three”
NOTE: This examples are bordering useless, we used them to reduce cognitive load by focusing on syntax.
YOU MIGHT ALSO LIKE
c. if … elif
The condition between the square brackets executed and if the exit status is zero, the shell command
inside the
Age=18
if [ $age -gt 18 ]; then
echo “You’re over 18, you can vote.”
elif[ $age -eq 18 ];
echo “You’re 18, you can vote.”
else
echo “You’re too young to vote.”
fi
d. Nested if
This control structure involves placing an
if [ $age -gt 20];
if[ $age -gt 13 ]; then
echo “You’re a teenager”
fi
fi