π DevOps Journey β Day 4: Shell Scripting Basics

Table of contents

Welcome to Day 4 of my DevOps journey! π
In DevOps, automation is everything β from deploying applications to monitoring systems and managing infrastructure. At the heart of this automation lies shell scripting, one of the most powerful tools every DevOps engineer must master.
What is Kernel?
The kernel is the core of any operating system (like Linux/Unix).
It manages:
Hardware (CPU, memory, devices)
Processes (running programs)
File system (read/write operations)
π Think of the kernel as the bridge between hardware and software.
πΉ What is Shell?
The shell is an interface between the user and the kernel.
It:
Takes commands from the user
Sends them to the kernel
Displays the result
Example: When you type ls
, the shell asks the kernel to fetch directory contents and shows them to you.
Step-by-step of what happens when you type ls
:
You type the command.
The shell receives your command.
The shell understands you want to list files.
The shell makes a system call to the kernel.
The kernel talks to the hard disk and fetches the file list.
The kernel sends results back to the shell.
The shell displays it on screen.
β‘ This whole process is fast and efficient β showing how the shell acts as an intermediary.
πΉ What is Shell Scripting?
A shell script is a file containing a series of commands written for the shell to execute.
Instead of typing commands one by one, you can automate tasks by writing them in a script.
Example:
#!/bin/bash
echo "Hello, World!"
π‘ Shell scripting is widely used for automation, backups, monitoring, installation scripts, and system administration.
πΉ Types of Shell
Different shells in Linux/Unix:
Bash (Bourne Again Shell) β Most popular
Sh (Bourne Shell) β Original shell
Csh (C Shell) β C-like syntax
Ksh (Korn Shell) β Advanced features
Zsh (Z Shell) β Extended Bash, customizable
π Check installed shells:
cat /etc/shells
πΉ Starting the Shell
When you log in, the shell starts automatically.
You can start another shell:
bash sh zsh
To exit:
exit
πΉ How to Run a Shell Script
Step 1: Create a Script File
#!/bin/bash
echo "This is my first shell script!"
Step 2: Make it Executable
chmod +x script.sh
Step 3: Run it
1οΈβ£ Absolute Path
/home/user/scripts/script.sh
2οΈβ£ Relative Path
./script.sh
scripts/script.sh
../script.sh
3οΈβ£ Running with Shell Command
bash script.sh
sh script.sh
zsh script.sh
4οΈβ£ Adding to PATH
export PATH=$PATH:/home/user/scripts
script.sh
πΉ Script Naming Conventions
Keep scripts in one place β
scripts/
Use correct extensions β
.sh
,.bash
Why? β Clarity, team understanding, professionalism
πΉ Script File Permissions
Scripts need execute (x) permission.
By default, new files donβt get execute permission.
Best Practices:
Personal script β
700
Team/shared β
755
Sensitive β
700
πΉ Script Format
Order to follow:
- Shebang β interpreter
#!/bin/bash
- Header block β info
# =========================================================
# Script Name : backup.sh
# Description : Creates compressed backup of logs
# Author : Hari
# Created : 21-Aug-2025
# Modified : 22-Aug-2025
# =========================================================
- Safety options
set -euo pipefail
- Variables
LOG_DIR="/var/logs/app"
BACKUP_FILE="backup_$(date +%F).tar.gz"
- Functions
backup_logs() {
echo "[INFO] Starting backup..."
tar -czf "$BACKUP_FILE" "$LOG_DIR"
}
- Main Logic
if [ -d "$LOG_DIR" ]; then
backup_logs
echo "[INFO] Backup completed successfully."
else
echo "[ERROR] Log directory not found: $LOG_DIR"
fi
πΉ Script Execution Sequence
Runs line by line, top β bottom
Order matters
Example:
ls
date
df -h
π Flow can be changed only by conditions, loops, functions.
π― Key Takeaways β Day 4
Kernel = Core, Shell = Interface
Shell scripting automates tasks
Ways to run scripts β absolute, relative, interpreter, PATH
Follow conventions, permissions, clean format
Scripts execute sequentially
π Day 4 Challenge
Create
hello_
devops.sh
printing username + date.Save in
scripts/
directory.Add header, safety options, variables.
Set permissions β
755
.Run using absolute, relative, and shell command methods.
Subscribe to my newsletter
Read articles from Hari Prasad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
