Control structures are fundamental elements in Linux shell scripting that allow scripts to make decisions and control the flow of execution. Instead of executing commands sequentially in a fixed order, control structures enable scripts to respond dynamically to varying conditions, execute repetitive tasks efficiently, and handle multiple scenarios within a single script.
Linux shell supports mainly two types of control structures: conditional statements and loops. Conditional statements make decisions based on the truth value of expressions, while loops repeat commands until specified conditions are met. Understanding and applying these constructs help in implementing logic and flow control similar to any programming language, but tailored for shell environments and command execution.
Conditional Statements
To build logical and flexible scripts, it is essential to understand conditional statements in Bash. The list below introduces the primary constructs used for branching logic.
if-elif-else
The if statement executes commands based on whether a condition is true or false. It starts with if followed by a condition and then keywords, concluding with fi (reverse of if). Optionally, elif and else clauses provide multiple conditional branches and a default action.
if [ condition ]; then
# commands if condition true
elif [ another_condition ]; then
# commands if another_condition true
else
# commands if none above conditions true
fi1. Conditions can be expressions or test commands.
2. Square brackets [ ] are commonly used with spaces separating components.
3. Commands return exit statuses; zero means true, non-zero means false.
Example:
if [ $USER == "root" ]; then
echo "You are root"
else
echo "You are a regular user"
ficase Statement
The case statement provides a cleaner way to perform multiple matches against a variable. It is useful for handling menus, options, or multiple patterns.
case $variable in
pattern1)
commands ;;
pattern2)
commands ;;
*)
default commands ;;
esacLoops repeat a block of commands multiple times based on a condition or a set of values.
for Loop
Iterates over a list of items or over command-line arguments.
for var in list; do
commands
doneExample:
for file in *.txt; do
echo "Processing $file"
donewhile Loop
Executes the block repeatedly until the condition becomes false.
while [ condition ]; do
commands
doneExample:
count=1
while [ $count -le 5 ]; do
echo "Count is $count"
((count++))
doneuntil Loop
Like while, but executes until the condition becomes true.
until [ condition ]; do
commands
doneAdditional Flow Control Commands
1. break exits the current loop prematurely.
2. continue skips the remaining commands in the current loop iteration.
3. exit terminates script execution immediately.
Important Notes About Conditions
1. Conditions can be string tests (-z for empty, = for equality), numerical comparisons (-eq, -lt, -gt), or file tests (-f for file existence, -d for directory).
2. Logical operators like && (and), || (or), and ! (not) can combine or invert conditions.
3. Always ensure spaces around brackets and operators for syntactical correctness.