In this lesson, we will delve into an essential aspect of shell scripting — Control Structures and Logical Operators. Control structures such as if
, elif
, and else
allow you to make decisions within your scripts, enabling them to respond intelligently based on different conditions. Additionally, logical operators like &&
(AND) and ||
(OR) help you combine multiple conditions to create more complex logic.
Let's get started!
The if/elif/else
control structure allows you to perform different actions based on various conditions. Here’s the syntax:
Bash1if [ condition1 ] 2then 3 # Commands to execute if condition1 is true 4elif [ condition2 ] 5then 6 # Commands to execute if condition1 is false and condition2 is true 7else 8 # Commands to execute if both condition1 and condition2 are false 9fi
Each condition must be enclosed in []
. After the condition, the then
keyword specifies the code to execute if the condition evaluates to 0
(true).
All control structures must start with an if
statement. The elif
and else
statements are optional. Each elif
condition is checked until one of the conditions evaluates to true. If none of the if
or elif
conditions are true, the else
statement is executed.
The control structure must end with fi
.
Let's take a look at an example to determine if a number is positive, negative, or neither.
Bash1#!/bin/bash 2# Script to demonstrate if-else control structure 3number=5 4 5if [ $number -gt 0 ] 6then 7 echo "The number is positive" # Prints: "The number is positive" 8elif [ $number -lt 0 ] 9then 10 echo "The number is negative" # This won't execute 11else 12 echo "The number is zero" # This won't execute 13fi
number=5
initializes the variablenumber
with the value 5.- The
if
statement checks ifnumber
is greater than 0. - If true, it prints "The number is positive".
- If the first condition is false, the
elif
statement checks ifnumber
is less than 0. - If the
elif
condition is also false, theelse
statement executes, printing "The number is zero".
Logical operators allow you to combine multiple conditions in your control structures. The &&
(AND) operator ensures both conditions must be true, while the ||
(OR) operator requires at least one condition to be true.
Let's examine how they work:
The &&
(AND) operator is used to combine multiple conditions in a control structure such that all the conditions must be true for the block of commands to execute. For example:
Bash1#!/bin/bash 2# Variables 3num1=10 4num2=20 5str1="Hello" 6str2="World" 7 8# Using && (AND) Operator 9if [ $num1 -lt $num2 ] && [ "$str1" != "$str2" ] 10then 11 echo "num1 is less than num2 AND str1 is not equal to str2" # Prints: "num1 is less than num2 AND str1 is not equal to str2" 12else 13 echo "Condition is false" # This won't execute 14fi
num1
is 10,num2
is 20,str1
is "Hello", andstr2
is "World".- The
if
statement checks ifnum1
is less thannum2
ANDstr1
is not equal tostr2
. - Both conditions are true, so the script prints:
"num1 is less than num2 AND str1 is not equal to str2"
.
The ||
(OR) operator is used to combine multiple conditions in a control structure such that at least one of the conditions must be true for the block of commands to execute. For example:
Bash1#!/bin/bash 2 3# Variables 4num1=10 5num2=20 6 7# Using || (OR) Operator 8if [ $num1 -gt 5 ] || [ $num2 -lt 5 ] 9then 10 echo "num1 is greater than 5 OR num2 is less than 5" # Prints: "num1 is greater than 5 OR num2 is less than 5" 11else 12 echo "Neither condition is true" # This won't execute 13fi
- The
if
statement checks ifnum1
is greater than 5 ORnum2
is less than 5. - Since
num1
is indeed greater than 5, the script prints:"num1 is greater than 5 OR num2 is less than 5"
.
Excellent work! In this lesson, you explored the fundamental concepts of control structures and logical operators in shell scripting. You learned about if-else
statements and how to combine conditions using &&
and ||
operators.
These constructs are powerful tools that enable you to write more intelligent and responsive scripts. Understanding and applying these concepts will greatly enhance your ability to automate tasks and solve real-world problems.
Now, it's time to put your newfound knowledge to the test! Move on to the practice sections where you will apply what you've learned through hands-on exercises. Happy scripting!