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

Vaishnavi DVaishnavi D
5 min read

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.

OperatorMeaningExample
+Additionecho $((2 + 3)) → 5
-Subtractionecho $((5 - 2)) → 3
*Multiplicationecho $((4 * 3)) → 12
/Divisionecho $((10 / 2)) → 5
%Modulusecho $((7 % 3)) → 1

Note: Use $((expression)) for arithmetic in Bash.

2. Relational / Comparison Operators

Used to compare numbers in if statements.

OperatorMeaningExample ([ $a -eq $b ])
-eqEqual toTrue if a = b
-neNot equal toTrue if a ≠ b
-gtGreater thanTrue if a > b
-ltLess thanTrue if a < b
-geGreater than or equalTrue if a ≥ b
-leLess than or equalTrue if a ≤ b

3. Logical Operators

Used to combine multiple conditions.

OperatorMeaningExample
&&Logical ANDif [[ $a -gt 5 && $a -lt 10 ]]
!Logical NOTif ! [ $a -eq 5 ]

4. String Operators

Used for string comparisons.

OperatorMeaningExample
=Equal[ "$a" = "$b" ]
!=Not equal[ "$a" != "$b" ]
-zString is empty[ -z "$a" ]
-nString is not empty[ -n "$a" ]

5. File Test Operators

Used to check properties of files and directories.

OperatorDescription
-eFile exists
-fRegular file
-dDirectory
-rFile is readable
-wFile is writable
-xFile 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!

4
Subscribe to my newsletter

Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaishnavi D
Vaishnavi D