πŸš€ DevOps Journey – Day 4: Shell Scripting Basics

Hari PrasadHari Prasad
4 min read

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:

  1. You type the command.

  2. The shell receives your command.

  3. The shell understands you want to list files.

  4. The shell makes a system call to the kernel.

  5. The kernel talks to the hard disk and fetches the file list.

  6. The kernel sends results back to the shell.

  7. 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

  1. Keep scripts in one place β†’ scripts/

  2. Use descriptive names β†’ backup_db.sh, check_date.sh

  3. Use correct extensions β†’ .sh, .bash

  4. 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:

  1. Shebang β†’ interpreter
#!/bin/bash
  1. Header block β†’ info
# =========================================================
# Script Name : backup.sh
# Description : Creates compressed backup of logs
# Author      : Hari
# Created     : 21-Aug-2025
# Modified    : 22-Aug-2025
# =========================================================
  1. Safety options
set -euo pipefail
  1. Variables
LOG_DIR="/var/logs/app"
BACKUP_FILE="backup_$(date +%F).tar.gz"
  1. Functions
backup_logs() {
  echo "[INFO] Starting backup..."
  tar -czf "$BACKUP_FILE" "$LOG_DIR"
}
  1. 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

  1. Create hello_devops.sh printing username + date.

  2. Save in scripts/ directory.

  3. Add header, safety options, variables.

  4. Set permissions β†’ 755.

  5. Run using absolute, relative, and shell command methods.

0
Subscribe to my newsletter

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

Written by

Hari Prasad
Hari Prasad