Day 9 of 90 Days of DevOps Challenge: conditional statements and loops


Operators in Shell Scripting
In shell scripting, operators are used to perform various operations like arithmetic, comparisons, and logical evaluations.
Let’s break them down into key categories:
1. Arithmetic Operators
Used to perform basic mathematical operations.
Operator | Meaning | Example |
+ | Addition | echo $((2 + 3)) → 5 |
- | Subtraction | echo $((5 - 2)) → 3 |
* | Multiplication | echo $((4 * 3)) → 12 |
/ | Division | echo $((10 / 2)) → 5 |
% | Modulus | echo $((7 % 3)) → 1 |
Note: Use $((expression))
for arithmetic in Bash.
2. Relational / Comparison Operators
Used to compare numbers in if statements.
Operator | Meaning | Example ([ $a -eq $b ] ) |
-eq | Equal to | True if a = b |
-ne | Not equal to | True if a ≠ b |
-gt | Greater than | True if a > b |
-lt | Less than | True if a < b |
-ge | Greater than or equal | True if a ≥ b |
-le | Less than or equal | True if a ≤ b |
3. Logical Operators
Used to combine multiple conditions.
Operator | Meaning | Example |
&& | Logical AND | if [[ $a -gt 5 && $a -lt 10 ]] |
! | Logical NOT | if ! [ $a -eq 5 ] |
4. String Operators
Used for string comparisons.
Operator | Meaning | Example |
= | Equal | [ "$a" = "$b" ] |
!= | Not equal | [ "$a" != "$b" ] |
-z | String is empty | [ -z "$a" ] |
-n | String is not empty | [ -n "$a" ] |
5. File Test Operators
Used to check properties of files and directories.
Operator | Description |
-e | File exists |
-f | Regular file |
-d | Directory |
-r | File is readable |
-w | File is writable |
-x | File is executable |
Example:
if [ -f myfile.txt ]; then
echo "File exists"
fi
What Are Control Structures?
Control structures guide how your script executes:
Loops: repeat tasks
Conditional statements: make decisions
Break/Continue: allow finer control within loops
Conditional Statements: if, else & elif
Conditional statements let a script make decisions based on certain conditions.
Basic Syntax
if [ condition ]; then
# code if true
elif [ another_condition ]; then
# code if second condition true
else
# code if all above fail
fi
Example: Number Comparison
num=10
if [ $num -gt 5 ]; then
echo "$num is greater than 5"
elif [ $num -eq 5 ]; then
echo "$num is equal to 5"
else
echo "$num is less than 5"
fi
Looping Statements
Loops allow repeating tasks, making scripts efficient and clean.
1. For Loop
Used when you know how many times to repeat. Useful for iterating over files, directories, arguments, etc.
for i in 1 2 3 4 5
do
echo "Number: $i"
done
2. While Loop
Repeats as long as the condition is true. Ideal when the end condition isn’t predetermined.
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
Controlling Loop Execution
Sometimes, you need to exit or skip part of a loop. That's where break
and continue
help.
Break Statement
The Break Statement immediately exits the loop regardless of the condition.
for i in {1..10}
do
if [ $i -eq 5 ]; then
break
fi
echo "i: $i"
done
Output:
i: 1
i: 2
i: 3
i: 4
Continue Statement
Skips the execution of the current iteration’s remaining statements and immediately proceeds to the next cycle of the loop.
for i in {1..5}
do
if [ $i -eq 3 ]; then
continue
fi
echo "i: $i"
done
Output:
i: 1
i: 2
i: 4
i: 5
Hands-on Practice
1. Write a script to check if a number is even or odd.
#!/bin/bash
read -p "Enter a number: " num
if (( num % 2 == 0 )); then
echo "$num is even."
else
echo "$num is odd."
fi
2. Create a script that prints numbers 1 to 5 using a while loop.
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo "Number: $i"
((i++))
done
3. Write a script to compare two numbers and print the greater one.
#!/bin/bash
read -p "Enter first number: " a
read -p "Enter second number: " b
if [ $a -gt $b ]; then
echo "$a is greater than $b"
elif [ $a -lt $b ]; then
echo "$b is greater than $a"
else
echo "Both numbers are equal"
fi
4. Write a script using a for loop to display only odd numbers between 1 and 10.
#!/bin/bash
for ((i=1; i<=10; i++))
do
if (( i % 2 != 0 )); then
echo "Odd number: $i"
fi
done
5. Write a script to check if a given file exists and is readable.
#!/bin/bash
read -p "Enter file name: " file
if [ -r "$file" ]; then
echo "The file '$file' exists and is readable."
else
echo "The file does not exist or is not readable."
fi
Final Thoughts
Today’s dive into loops and conditionals helped me understand how logic drives automation. From if-else
to while
and for
loops, these constructs bring flexibility and intelligence to scripts. Knowing when to use break
or continue
makes a big difference in controlling flow effectively.
Mastering these basics has shown me that shell scripting is not just about code, it’s about solving problems efficiently. I’m excited to keep building on this foundation and bring more automation into everyday DevOps tasks. This journey is getting more insightful with every line I write!
Subscribe to my newsletter
Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
