🚀 DevOps Journey – Day 5: Shell Scripting

Table of contents

Welcome back to Day 5 of our DevOps Journey!
So far, we’ve explored Linux basics, permissions, and working with the terminal. Today, we take a major leap: Shell Scripting.
Shell scripting is the first step towards automation, and in DevOps, automation is everything. Whether it’s provisioning servers, deploying applications, or monitoring systems, scripts are the glue that binds your operations together.
📝 Why Shell Scripting?
Before diving into scripts, let’s understand why they matter in DevOps:
Save Time & Effort – Instead of typing 10 commands every morning to check server health, you can run one script.
Consistency – Manual work can lead to mistakes. Scripts ensure the same steps run every time.
Integration – Shell scripts can be called from tools like Jenkins, Ansible, or Terraform to automate pipelines.
Troubleshooting – Collecting logs, checking disk space, restarting services—scripts make it repeatable and reliable.
👉 In short: if Linux commands are your tools, shell scripting is your toolbox.
🛠️ Getting Started with Your First Script
Every programming journey starts with Hello World—and shell scripting is no different.
#!/bin/bash
# Author: DevOps Learner
# Date: 22-Aug-2025
# Description: First shell script
echo ""
echo "Hello, World!"
echo ""
Steps:
Save file as
hello_
world.sh
Make it executable:
chmod +x hello_world.sh
Run it:
./hello_world.sh
✅ Output:
Hello, World!
Why is this important?
The first line
#!/bin/bash
tells the system which shell should run the script.Comments (
#
) help you and others understand the script later.chmod +x
makes it executable—without it, you can’t run the script directly.
📌 DevOps connection: Your first deployment script will look very similar—start simple, then add complexity.
⚙️ Automating Basic Tasks
Imagine you log in daily and type these commands:
pwd
→ current directoryls -l
→ list fileswhoami
→ user infodate
→ current time
Instead, put them all in one script:
#!/bin/bash
# Script to automate basic Linux tasks
echo "Running some daily checks..."
echo ""
pwd
ls -l
whoami
date
cal
touch fileA fileB fileC
Benefits:
Saves time when you repeat tasks daily.
Reduces errors—no chance of forgetting a command.
Can be scheduled with cron to run automatically.
📌 DevOps connection: Automating health checks is the first step towards monitoring and alerting systems.
🖥️ System Administration Scripts
A DevOps engineer is also part system administrator. Tasks like checking memory, CPU, and disk usage can be automated:
#!/bin/bash
# Admin health check script
echo "===== System Health Report ====="
echo ""
top -n 1 | head -10 # Show top 10 processes
df -h # Disk usage
free -m # Memory usage
uptime # System uptime
iostat # CPU/IO stats
echo ""
echo "===== End of Report ====="
Why useful?
Can be run before deployments to ensure the server is healthy.
Helps during incidents: run the script once instead of remembering 5 commands.
Can redirect output to a file (
> health.log
) for later analysis.
📌 DevOps connection: This is the foundation for building self-healing servers and automated monitoring scripts.
🔄 Variables in Shell Scripts
Variables make scripts reusable and dynamic.
#!/bin/bash
# Variables example
DIR="pwd"
DATE="date"
CAL="cal 2025"
echo "Current Directory: $($DIR)"
echo "Today's Date: $($DATE)"
echo "Calendar:"
$CAL
Things to note:
Variables store values (commands, text, numbers).
Access variables with
$VAR
Enclose commands in
$()
to execute them.
📌 DevOps connection: Variables are heavily used in deployment pipelines—storing version numbers, build IDs, or server names.
⌨️ Input & Output (Interactive Scripts)
Scripts don’t always run blindly—they can ask for input.
#!/bin/bash
# Input/output example
echo "What is your name?"
read NAME
echo "Hello, $NAME! Welcome to DevOps."
Extended Example:
echo "What is your role?"
read ROLE
echo "Do you like automation? (yes/no)"
read ANSWER
if [ "$ANSWER" == "yes" ]; then
echo "Great! You're a true DevOps Engineer."
else
echo "No worries, you'll get there!"
fi
📌 DevOps connection: Installers, setup scripts, and configuration wizards all use input/output scripts.
🧩 Conditional Logic – If/Else
Scripts become powerful when they make decisions.
#!/bin/bash
COUNT=5
if [ $COUNT -eq 10 ]; then
echo "Count is 10"
else
echo "Count is not 10"
fi
Use Cases:
Check if a service is running → restart if not.
Verify if a file exists before processing.
Automate failover scripts.
📌 DevOps connection: If/else is key in CI/CD pipelines to decide whether to proceed with the next stage.
🔁 Loops in Scripts
Automation often needs repetition.
For loop example:
for i in 1 2 3 4 5
do
echo "Iteration $i"
done
While loop example:
COUNT=0
while [ $COUNT -lt 5 ]
do
echo "Count = $COUNT"
COUNT=$((COUNT+1))
done
Real-world examples:
Create 10 backup files in one go.
Retry a failed command until it succeeds.
Monitor a service every 5 seconds.
📌 DevOps connection: Loops are used in deployment retries, batch jobs, and infrastructure automation.
🏁 Exit Status – Success or Failure?
Every Linux command returns a status code:
0
→ SuccessNon-zero → Failure
Example:
ls /tmp/test.log
echo $?
If file exists → returns 0
If not → non-zero (e.g., 2
)
Why important?
Ensures your script knows when to stop.
Prevents accidental deployments when something fails.
Forms the backbone of error handling in automation.
📌 DevOps connection: Exit codes decide whether Jenkins marks a build SUCCESS or FAILED.
✅ Key Takeaways
Scripts = automation superpower for DevOps.
Start simple, add variables, conditions, and loops.
Always check exit codes for reliable pipelines.
Keep scripts organized in a dedicated folder.
The best scripts are simple, reusable, and modular.
Subscribe to my newsletter
Read articles from Hari Prasad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
