Day 6 of My 100-Day DevOps Challenge: Shell Scripting

Day 6 of My 100-Day DevOps Challenge: Shell Scripting

·

4 min read

Introduction

As I continue my DevOps journey, I've discovered that shell scripting is more than just writing code—it's about creating intelligent solutions that transform complex tasks into simple, executable commands. Today, I'm sharing my deep dive into the world of shell scripting, a skill that's becoming increasingly crucial in the tech landscape.

Understanding Shell Scripting

Shell scripting is the art of creating text files containing a series of commands that can be executed by a shell, typically Bash. It's like writing a detailed instruction manual for your computer, telling it exactly what to do, when to do it, and how to do it.

My First Shell Script

Recently, I embarked on creating my first shell scripts, and the journey has been nothing short of enlightening. Let me walk you through the process and key learnings.

Creating a Shell Script: Efficiency in Action

I discovered that creating a shell script is more than just typing commands—it's about understanding the underlying principles of automation and system interaction.

#!/bin/bash

# My first automation script
echo "Welcome to my DevOps Automation Journey!"

# Create a project structure
mkdir -p project/{src,tests,docs}
touch project/README.md project/requirements.txt

# Simple log generation
echo "Project initialized on $(date)" > project/setup.log

The Magic of Shebangs: Understanding Script Execution

The first line #!/bin/bash is more than just syntax—it's a crucial directive that tells the system which interpreter to use. I learned there are subtle but important differences:

  • #!/bin/sh: A POSIX-compliant shell, minimal and universally compatible

  • #!/bin/bash: An advanced shell with rich features

  • #!/usr/bin/env bash: A portable method that finds the bash executable

Granting Permissions: The Gateway to Execution

One of the most critical steps I learned was managing file permissions:

# Grant execute permissions
chmod 755 myscript.sh

# Breakdown of 755:
# 7 (owner): read + write + execute
# 5 (group): read + execute
# 5 (others): read + execute

Advanced Scripting: Beyond the Basics

As I delved deeper, I discovered shell scripting is a powerful tool for automation and system management.

Variables and User Interaction

#!/bin/bash

# Prompting for user input
read -p "Enter your project name: " project_name
read -sp "Enter your secret key: " secret_key

# Conditional logic
if [ -z "$project_name" ]; then
    echo "Project name cannot be empty!"
    exit 1
fi

# Creating project with user input
mkdir "$project_name"
echo "$secret_key" > "$project_name/.env"

Loops: Automating Repetitive Tasks

Loops transformed my approach to system management:

#!/bin/bash

# Backup multiple directories
backup_dirs=("/home/projects" "/etc/configs" "/var/logs")

for dir in "${backup_dirs[@]}"; do
    backup_name="backup_$(basename "$dir")_$(date +%Y%m%d)"
    cp -R "$dir" "/backups/$backup_name"
    echo "Backed up $dir to $backup_name"
done

Real-World DevOps Automation

Here's a comprehensive script that demonstrates multiple concepts:

#!/bin/bash
set -e  # Exit immediately if a command exits with a non-zero status

# Simple system information and backup script

# Function to display system information
system_info() {
    echo "===== System Information ====="
    echo "Hostname: $(hostname)"
    echo "Operating System: $(uname -o)"
    echo "Kernel Version: $(uname -r)"
    echo "Current User: $USER"
    echo "Current Date: $(date)"
}

# Function to create a simple backup
create_backup() {
    local backup_dir="/tmp/system_backup_$(date +%Y%m%d)"

    # Create backup directory
    mkdir -p "$backup_dir"

    # Copy important configuration files
    cp /etc/passwd "$backup_dir/passwd_backup"
    cp /etc/hosts "$backup_dir/hosts_backup"

    echo "Backup created at: $backup_dir"
}

# Main script execution
main() {
    # Display a welcome message
    echo "Welcome to the Simple System Utility Script!"

    # Call system information function
    system_info

    # Prompt user for backup
    read -p "Do you want to create a system backup? (y/n): " choice

    # Conditional backup creation
    if [[ "$choice" == "y" || "$choice" == "Y" ]]; then
        create_backup
    else
        echo "Backup skipped."
    fi
}

# Execute the main function
main

You can save this script (e.g., as system_utility.sh), make it executable with:

chmod +x system_utility.sh

Then run it:

./system_utility.sh

Key Learnings and Reflections

  1. Automation is Power: Shell scripting turns repetitive tasks into elegant, repeatable processes.

  2. Continuous Learning: Each script is an opportunity to improve and optimize.

  3. Flexibility is Key: Understanding different shells and their nuances is crucial.

  4. Error Handling Matters: Always implement robust error checking and logging.

Looking Ahead: The Scripting Journey Continues

Shell scripting is not just a skill—it's a mindset. As I continue to explore and learn, I'm constantly amazed by the possibilities that open up with each new script.

Stay curious, keep scripting, and remember: in the world of DevOps, automation is your best friend!