Ultimate Beginner's Guide to Shell Scripting for DevOps

SdeepSdeep
4 min read

Introduction

If you're new to DevOps, you might have heard about shell scripting as a must-have skill. But what exactly is it, and why is it so important?

Shell scripting allows you to automate repetitive tasks in the command line, making your work faster and more efficient. Whether you're deploying applications, managing servers, or setting up backups, shell scripts can save you time and reduce errors.

In this beginner-friendly guide, we'll cover:
✔ What shell scripting is
✔ Why it’s useful in DevOps
✔ Basic shell scripting concepts
✔ Practical examples to get you started

Let’s dive in!


What is Shell Scripting?

A shell script is a text file containing a series of commands that the shell (like Bash) executes one by one. Instead of typing commands manually, you write them in a script and run it whenever needed.

Why Learn Shell Scripting for DevOps?

  1. Automate Repetitive Tasks – No more typing the same commands again and again.

  2. Faster Deployments – Run multiple commands in one go.

  3. Error Reduction – Scripts execute the same way every time.

  4. Works Everywhere – Most Linux servers and CI/CD tools support shell scripting.


Getting Started with Shell Scripting

1. Writing Your First Script

Open a text editor (like nano or vim) and create a file:

nano hello.sh

Add the following code:

#!/bin/bash  # This is called the "shebang" – tells the system to use Bash
echo "Hello, DevOps World!"  # Prints a message

Save the file (Ctrl + X, then Y in nano) and make it executable:

chmod +x hello.sh

Run it:

./hello.sh

Output:

Hello, DevOps World!

🎉 Congratulations! You just wrote your first shell script!


2. Variables – Storing Data

Variables hold values that can be reused.

#!/bin/bash
NAME="Alice"
echo "Welcome, $NAME!"

Output:

Welcome, Alice!

3. Taking User Input

Use read to get input from the user.

#!/bin/bash
echo "What's your name?"
read USER_NAME
echo "Hello, $USER_NAME!"

Run it:

./input.sh

Output:

What's your name?
> John
Hello, John!

4. Conditional Statements (If-Else)

Check conditions before running commands.

#!/bin/bash
echo "Enter a number:"
read NUM

if [ $NUM -gt 10 ]; then
    echo "$NUM is greater than 10."
else
    echo "$NUM is 10 or less."
fi

Output:

Enter a number:
> 15
15 is greater than 10.

5. Loops – Doing Things Repeatedly

For Loop

#!/bin/bash
for i in 1 2 3 4 5; do
    echo "Number: $i"
done

Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

While Loop

#!/bin/bash
COUNT=1
while [ $COUNT -le 5 ]; do
    echo "Count: $COUNT"
    COUNT=$((COUNT + 1))
done

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Practical Shell Scripting Examples for DevOps

1. Automated Backup Script

#!/bin/bash
# Backup a directory
BACKUP_DIR="/home/user/backups"
SOURCE_DIR="/var/www/html"
DATE=$(date +%Y-%m-%d)

mkdir -p $BACKUP_DIR
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR"
echo "Backup completed on $(date)" >> /var/log/backup.log

2. Check Disk Space & Alert

#!/bin/bash
DISK_USAGE=$(df -h | grep '/dev/sda1' | awk '{print $5}' | cut -d'%' -f1)
THRESHOLD=80

if [ $DISK_USAGE -gt $THRESHOLD ]; then
    echo "Warning: Disk usage is $DISK_USAGE%!" | mail -s "Disk Alert" admin@example.com
fi

3. Install Software Automatically

#!/bin/bash
# Update system and install Nginx
sudo apt update
sudo apt install -y nginx
sudo systemctl start nginx
echo "Nginx installed and running!"

Best Practices for Beginners

Use comments (#) – Explain what your script does.
Test scripts in a safe environment – Don’t run untested scripts on production servers.
Use set -e – Exit the script if any command fails.
Store scripts in version control (Git) – Track changes and collaborate with others.


Conclusion

Shell scripting is a superpower for DevOps engineers. With just a few commands, you can automate tasks, save time, and reduce mistakes. Start with simple scripts, practice regularly, and soon you'll be writing powerful automation tools!

Next Steps:

  • Try modifying the examples above

  • Explore more commands (grep, awk, sed)

  • Learn about cron jobs to schedule scripts

Got questions? Drop them in the comments! 🚀

#DevOps #ShellScripting #Linux #Automation #BeginnersGuide

0
Subscribe to my newsletter

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

Written by

Sdeep
Sdeep

👋 Hello! I'm passionate about DevOps and I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!