Skip to main content

Day 5 - Shell Scripting — Fundamentals for DevOps & Linux Admins

Published 5 min readMarkdown
Part of seriesDevOps

🧠 What is Shell Scripting?

Shell scripting is nothing but automation — the process of converting manual repetitive tasks into automatic commands.

💡 In simple words:
Shell scripting reduces manual effort by executing predefined Linux commands automatically.


⚙️ Fundamentals of Shell Scripting

Why do we need Shell Scripting?

In Windows, we can create files/folders easily using the GUI.
In Linux servers (especially in cloud and DevOps), there’s no GUI — everything is done through command line.

A shell allows you to talk to your operating system using commands.


🧩 What is a Shell?

A Shell is a command-line interpreter that acts as a bridge between the user and the Linux kernel.
When you type a command, the shell interprets it and sends it to the OS for execution.


🧮 Different Types of Shells

ShellDescription
shBourne Shell (original UNIX shell)
bashBourne Again Shell (most popular and default in Linux)
kshKorn Shell
cshC Shell
dashDebian Almquist Shell (used in Ubuntu for /bin/sh)

👉 We’ll learn using bash, since it’s widely used for scripting and DevOps.


🧰 Purpose of Shell Scripting in DevOps

A DevOps Engineer performs automation and system management tasks, such as:

  • Infrastructure management

  • Code management (Git repositories)

  • Configuration management (Ansible, Puppet, etc.)

  • Monitoring system health

  • Scheduling jobs (cron)

Example:

Imagine a DevOps Engineer at Amazon managing 10,000 Linux VMs.
They need to monitor:

  • CPU utilization

  • Memory usage

  • Disk space

Doing this manually on each VM is impossible.
Instead, a shell script can:

  • Log in to each VM

  • Collect health data

  • Send an email alert if usage crosses limits

✅ Result: Fully automated health monitoring.


✍️ How to Write a Shell Script

  1. Create a file

     vi testscript.sh
    
  2. Add the shebang

     #!/bin/bash
    
  3. Write commands

     echo "My name is Dinesh"
    
  4. Make it executable

     chmod +x testscript.sh
    
  5. Run the script

     ./testscript.sh
    

    OR

     sh testscript.sh
    

🔹 Shebang (#!) Explained

The shebang line tells Linux which shell interpreter to use to run the script.

Examples:

#!/bin/bash     # Use Bash shell
#!/bin/sh       # Use default shell (might point to bash or dash)
#!/bin/ksh      # Use Korn shell

⚠️ Important Note:

  • Earlier, /bin/sh was linked to /bin/bash.

  • In newer Ubuntu/Debian systems, /bin/sh points to /bin/dash.
    Dash is faster but lacks many bash features.

✅ So, always use:

#!/bin/bash

💬 Comments in Scripts

Use comments (#) to describe your script.
It helps you and others understand the purpose.

Example:

#!/bin/bash
# create a folder
mkdir folder1

# create two files
touch file1 file2

🧾 Metadata (Author Information)

Always include a header for versioning and documentation.

#!/bin/bash
#########################################################
# Author: Dinesh
# Date: 10-10-2025
# Version: v1
# Description: This script outputs the node health
#########################################################

🖨️ Displaying Output using echo

#!/bin/bash
#########################################################
# Author: Dinesh
# Script to display node health
#########################################################

echo "Disk Usage:"
df -h

echo "Memory Usage:"
free -g

echo "CPU Count:"
nproc

🐞 Display Output using Debug Mode (set -x)

For long scripts (hundreds of lines), using echo for every step isn’t practical.
Enable debug mode to print each command before execution:

#!/bin/bash
set -x
df -h
free -g
nproc

Disable by commenting:

#set -x

⚠️ Error Handling

1. set -e

Stops the script immediately if any command fails.
This prevents executing the next steps if an earlier one fails.

2. set -o pipefail

Ensures that errors in piped commands (|) are also caught.

Example:

#!/bin/bash
set -x
set -e
set -o pipefail

df -h
free -g
nproc

ps -ef | grep "ssh" | awk '{print $2}'

🔍 Process and Column Filtering

Find process:

ps -ef | grep "ssh"

Extract specific column:

ps -ef | grep "ssh" | awk '{print $2}'

🧾 DevOps Use Case — Searching for Errors in Log Files

When an application fails, logs are the first thing to check.
But logs are often huge and stored in remote storage (e.g., AWS S3, Azure Blob).

Common commands:

# Download and search logs directly
curl <logfile_url> | grep "ERROR"

# Or download log file
wget <logfile_url>

# Then search locally
cat apache.log | grep "ERROR"

🔍 find Command Example

To locate files anywhere in Linux:

find / -iname apache.log

🔁 Loops in Shell Scripting

🔸 if-else Syntax

if [ expression ]
then
    # Commands to execute if condition is true
else
    # Commands to execute if condition is false
fi

Example:

a=4
b=10

if [ $a -gt $b ]
then
    echo "a is greater than b"
else
    echo "b is greater than a"
fi

🔸 for Loop

Used when we want to repeat actions multiple times.

Example — print numbers from 1 to 10:

for i in {1..10}
do
    echo "Number: $i"
done

Another example — list all files in a folder:

for file in /var/log/*.log
do
    echo "Checking $file"
    grep "ERROR" $file
done

✅ Summary

ConceptDescription
ShellInterface between user and Linux OS
Shell ScriptFile with shell commands
Shebang (#!)Defines which shell to use
set -xDebug mode
set -eExit on error
set -o pipefailCatch errors in piped commands
Use CaseAutomate monitoring, file management, log analysis
LoopsControl execution flow (if, for, etc.)

Would you like me to continue the next section with real DevOps examples, like:

  • Script to monitor CPU/memory usage and send email alerts

  • Script to rotate and compress logs daily

  • Script to check service status across multiple servers

Those will make your notes production-ready 🚀

About the author

  • Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.

No comments yet. Be the first to comment.

More from this blog