Day 5 - Shell Scripting โ Fundamentals for DevOps & Linux Admins
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.
Search for a command to run...
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.
1. Difference between Docker and Kubernetes Docker โ Builds and runs containers.Kubernetes โ Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory โ there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs โ such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout โ payments) cannot rely on Pod IP because it changes, creating issue...
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.
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.
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.
| 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.
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)
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.
Create a file
vi testscript.sh
Add the shebang
#!/bin/bash
Write commands
echo "My name is Dinesh"
Make it executable
chmod +x testscript.sh
Run the script
./testscript.sh
OR
sh testscript.sh
#!) ExplainedThe 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
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
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
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
#########################################################
echo#!/bin/bash
#########################################################
# Author: Dinesh
# Script to display node health
#########################################################
echo "Disk Usage:"
df -h
echo "Memory Usage:"
free -g
echo "CPU Count:"
nproc
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
set -eStops the script immediately if any command fails.
This prevents executing the next steps if an earlier one fails.
set -o pipefailEnsures that errors in piped commands (|) are also caught.
#!/bin/bash
set -x
set -e
set -o pipefail
df -h
free -g
nproc
ps -ef | grep "ssh" | awk '{print $2}'
ps -ef | grep "ssh"
ps -ef | grep "ssh" | awk '{print $2}'
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).
# 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"
To locate files anywhere in Linux:
find / -iname apache.log
if-else Syntaxif [ expression ]
then
# Commands to execute if condition is true
else
# Commands to execute if condition is false
fi
a=4
b=10
if [ $a -gt $b ]
then
echo "a is greater than b"
else
echo "b is greater than a"
fi
for LoopUsed 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
| 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 ๐