Day 5 - Shell Scripting โ Fundamentals for DevOps & Linux Admins
๐ง 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
| Shell | Description |
sh | Bourne Shell (original UNIX shell) |
bash | Bourne Again Shell (most popular and default in Linux) |
ksh | Korn Shell |
csh | C Shell |
dash | Debian 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
Create a file
vi testscript.shAdd the shebang
#!/bin/bashWrite commands
echo "My name is Dinesh"Make it executable
chmod +x testscript.shRun the script
./testscript.shOR
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/shwas linked to/bin/bash.In newer Ubuntu/Debian systems,
/bin/shpoints 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
| Concept | Description |
| Shell | Interface between user and Linux OS |
| Shell Script | File with shell commands |
Shebang (#!) | Defines which shell to use |
set -x | Debug mode |
set -e | Exit on error |
set -o pipefail | Catch errors in piped commands |
| Use Case | Automate monitoring, file management, log analysis |
| Loops | Control 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 ๐