USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Conditional Logic in Scripts

Lesson 34/49 | Study Time: 20 Min

Conditional logic is a cornerstone of programming and scripting that enables decision-making within scripts. In Linux shell scripting, conditional statements control the flow of execution based on specific conditions, allowing scripts to behave dynamically in response to different inputs or system states.

Basics of Conditional Statements in Shell Scripts

The primary conditional structures in shell scripting are:


  • if
  • if...else
  • if...elif...else
  • case statements


Conditions are tested using expressions inside square brackets [ ] or double brackets [[ ]] with operators for comparison.

Syntax of if Statement

bash
if [ condition ]
then
# commands to execute if condition is true
fi


Example:

bash
if [ $a -gt $b ]
then
echo "a is greater than b"
fi


if...else Statement

Runs one set of commands if a condition is true, and another if it is false.

bash
if [ condition ]
then
# commands if condition true
else
# commands if condition false
fi


Example:

bash
if [ $num -eq 10 ]
then
echo "Number is ten"
else
echo "Number is not ten"
fi

if...elif...else Ladder

For multiple conditions, use elif (else if).

bash
if [ condition1 ]
then
# commands if condition1 true
elif [ condition2 ]
then
# commands if condition2 true
else
# if no condition met
fi


Example:

bash
if [ $score -ge 90 ]
then
echo "Grade A"
elif [ $score -ge 80 ]
then
echo "Grade B"
else
echo "Grade C or below"
fi

Using the case Statement

The case statement matches a variable against multiple patterns, often used instead of multiple if statements for clarity.

Syntax:

bash
case $variable in
pattern1)
# commands
;;
pattern2)
# commands
;;
*)
# default commands
;;
esac


Example:

bash
case $choice in
1) echo "You chose option 1";;
2) echo "You chose option 2";;
*) echo "Invalid option";;
esac

Common Operators for Conditions

Type      Operator                 Description            Example
Numeric-eqequal to[ $a -eq $b ]
-nenot equal to[ $a -ne $b ]
-gtgreater than[ $a -gt $b ]
-ltless than[ $a -lt $b ]
String=equal to[ "$str" = "abc" ]
!=not equal[ "$str" != "abc" ]
<less than (in ASCII)[ "$a" \< "$b" ]
>greater than (in ASCII)[ "$a" \> "$b" ]
File testing-f fileis regular file[ -f filename ]
-d diris directory[ -d directory ]
Logical Operators

  • AND: && or -a
  • OR: || or -o
  • NOT: !


Example:
bash
if [ $a -gt 0 ] && [ $a -lt 10 ]
then
echo "a is between 1 and 9"
fi
Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways