Day 17: Mastering Shell Scripting – From Basics to Practical Use

AkankshaAkanksha
4 min read

Today I dedicated my learning to Shell Scripting — one of the most essential skills for DevOps and SRE engineers. Shell scripts act as automation blueprints for repetitive tasks, system monitoring, and deployments. Below is my structured recap, along with the assignments I solved.


1. What is Shell Scripting?

Shell scripting is about writing a series of Linux commands in a file (with .sh extension) and executing them in sequence.

Why it matters:

  • Automation: Handle backups, log analysis, deployments.

  • Efficiency: Reduce manual effort and human error.

  • Consistency: Ensure repeatability in tasks.

  • Monitoring & Maintenance: Regular system health checks.


2. Essential Concepts

Shebang

The shebang (#!) defines which interpreter should run the script.

#! /bin/bash

Variables

Variables store values for reuse.

name="Akanksha"
echo "Hello $name"

Types of variables:

  • System variables (e.g., $USER, $HOME)

  • User-defined variables (custom, stored in .bashrc for persistence)

Operators

  • Arithmetic: + - * / %

  • Relational: -eq, -ne, -gt, -lt, -ge, -le

Conditionals

if [ $num -gt 10 ]; then
    echo "Number is greater than 10"
else
    echo "Number is 10 or smaller"
fi

Loops

For repeating tasks:

for ((i=1; i<=5; i++))
do
   echo $i
done

While loop (runs until condition becomes false):

while [ $num -le 5 ]
do
   echo $num
   ((num++))
done

Command Line Arguments (CLA)

  • $0 → script name

  • $1, $2, … → arguments

  • $# → total number of arguments

  • $@ → all arguments as separate items

Functions

Reusable blocks of logic:

factorial () {
   num=$1
   fact=1
   for ((i=1; i<=num; i++))
   do
      fact=$((fact * i))
   done
   echo $fact
}

factorial 5

3. Assignments and Solutions

Task 1: Display System Information

#!/bin/bash
echo "Current User: $(whoami)"
echo "Current Directory: $(pwd)"
echo "Today's Date: $(date +"%A, %B %d, %Y")"
echo "System Uptime: $(uptime -p)"

Output Example

Current User: ubuntu
Current Directory: /home/ubuntu
Today's Date: Sunday, August 17, 2025
System Uptime: up 3 hours, 20 minutes

Task 2: Interactive Script

#!/bin/bash
echo "Enter your name: "
read name
echo "Enter your favorite place: "
read place
echo "Hello $name, your favorite place is $place!"

Task 3: Check if a File Exists

#!/bin/bash
file=$1
if [ -e "$file" ]; then
    echo "The file $file is available in $(pwd)"
else
    echo "The file $file does not exist in $(pwd)"
fi

Task 4: Check File Type

#!/bin/bash
name=$1
if [ -f "$name" ]; then
    echo "The $name is a Normal File"
elif [ -d "$name" ]; then
    echo "The $name is a Directory"
else
    echo "$name does not exist"
fi

Task 5: Even or Odd Checker

#!/bin/bash
read -p "Enter a number: " num
if (( num % 2 == 0 )); then
    echo "$num is Even"
else
    echo "$num is Odd"
fi

Task 6: Largest of Three Numbers (CLA)

#!/bin/bash
if [ $# -ne 3 ]; then
    echo "Usage: $0 num1 num2 num3"
    exit 1
fi

if [ $1 -ge $2 ] && [ $1 -ge $3 ]; then
    echo "$1 is the largest"
elif [ $2 -ge $1 ] && [ $2 -ge $3 ]; then
    echo "$2 is the largest"
else
    echo "$3 is the largest"
fi

Task 7: Print Even Numbers (For Loop)

#!/bin/bash
for ((i=2; i<=20; i+=2))
do
   echo $i
done

Task 8: List Specific File Types in Directory

#!/bin/bash
path=$1
for file in "$path"/*.{py,java,txt}
do
    [ -e "$file" ] && echo $file
done

Task 9: Monitor CPU Usage (While Loop)

#!/bin/bash
while true
do
    echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')%"
    sleep 5
done

Task 10: Factorial with Functions

#!/bin/bash
factorial () {
   num=$1
   fact=1
   for ((i=1; i<=num; i++))
   do
      fact=$((fact * i))
   done
   echo "Factorial of $num is $fact"
}

read -p "Enter a number: " n
factorial $n

4. Key Takeaways

  • Shell scripting provides the foundation for automation in DevOps.

  • Using variables, operators, conditionals, loops, CLA, and functions, I can now write reusable and production-friendly scripts.

  • The assignments I solved today simulate real-world DevOps use cases like monitoring, system checks, and automation tasks.

0
Subscribe to my newsletter

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

Written by

Akanksha
Akanksha