What is Shell Scripting?

Shell scripting is the practice of writing programs (scripts) that are executed by a shell. These scripts automate tasks, combine multiple commands, and create powerful workflows that would be tedious to perform manually.

Definition and Purpose

A shell script is a text file containing a series of commands that the shell can execute. Instead of typing commands one by one, you write them in a file and execute the entire sequence at once.

Key Benefits:

  • Automation: Eliminate repetitive manual tasks
  • Consistency: Ensure tasks are performed the same way every time
  • Efficiency: Save time and reduce human error
  • Scheduling: Run scripts automatically at specific times
  • Complex Logic: Implement decision-making and loops

When to Use Shell Scripts

Perfect for:

  • System Administration: User management, backups, log rotation
  • File Operations: Batch processing, file organization
  • Development Workflows: Build processes, deployment scripts
  • Data Processing: Log analysis, report generation
  • System Monitoring: Health checks, alert systems

Not Ideal for:

  • Complex Calculations: Use Python, R, or specialized tools
  • GUI Applications: Shell scripts are command-line oriented
  • Performance-Critical Tasks: Compiled languages are faster
  • Cross-Platform Applications: Shell scripts are OS-specific

Types of Shell Scripts

1. Simple Command Sequences

#!/bin/bash
# Backup script
cp /home/user/documents/* /backup/
tar -czf /backup/backup_$(date +%Y%m%d).tar.gz /backup/*.txt
echo "Backup completed at $(date)"

2. Interactive Scripts

#!/bin/bash
# User input script
echo "What's your name?"
read name
echo "Hello, $name! Welcome to shell scripting."

3. System Administration Scripts

#!/bin/bash
# System monitoring script
echo "System Status Report - $(date)"
echo "=========================="
echo "Disk Usage:"
df -h
echo "Memory Usage:"
free -h
echo "CPU Load:"
uptime

4. Data Processing Scripts

#!/bin/bash
# Log analysis script
echo "Analyzing web server logs..."
grep "ERROR" /var/log/apache2/error.log | wc -l
echo "Total errors found"

Shell Script Components

1. Shebang Line

#!/bin/bash
# Tells the system which interpreter to use

2. Comments

# This is a single-line comment
: '
This is a
multi-line comment
'

3. Variables

name="John"
age=25
echo "Name: $name, Age: $age"

4. Commands

ls -la
pwd
date

5. Control Structures

if [ $age -gt 18 ]; then
    echo "Adult"
else
    echo "Minor"
fi

Script Execution Methods

1. Make Executable and Run

# Make script executable
$ chmod +x myscript.sh

# Run the script
$ ./myscript.sh

2. Run with Shell Interpreter

# Run with bash
$ bash myscript.sh

# Run with sh
$ sh myscript.sh

3. Source the Script

# Execute in current shell (affects current environment)
$ source myscript.sh
# or
$ . myscript.sh

Script Structure Best Practices

Basic Template:

#!/bin/bash

# Script: example.sh
# Purpose: Demonstrate shell script structure
# Author: Your Name
# Date: $(date)

# Set strict error handling
set -euo pipefail

# Global variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="/var/log/myscript.log"

# Functions
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

main() {
    log_message "Script started"

    # Main script logic here
    echo "Hello, World!"

    log_message "Script completed"
}

# Execute main function
main "$@"

Common Shell Scripting Patterns

1. Error Handling

#!/bin/bash
set -e  # Exit on any error

command_that_might_fail || {
    echo "Command failed!"
    exit 1
}

2. Argument Processing

#!/bin/bash
if [ $# -eq 0 ]; then
    echo "Usage: $0 <filename>"
    exit 1
fi

filename="$1"
echo "Processing file: $filename"

3. Configuration Files

#!/bin/bash
# Load configuration
CONFIG_FILE="config.conf"
if [ -f "$CONFIG_FILE" ]; then
    source "$CONFIG_FILE"
else
    echo "Configuration file not found!"
    exit 1
fi

Shell Scripting vs Other Languages

Shell Scripts Excel At:

  • System integration
  • File manipulation
  • Process management
  • Quick automation tasks
  • Gluing together existing tools

Other Languages Better For:

  • Python: Complex logic, data analysis, web development
  • C/C++: Performance-critical applications
  • JavaScript: Web applications, Node.js servers
  • Go: Network services, system tools

Development Environment

Essential Tools:

  • Text Editor: vim, nano, VS Code, or any editor
  • Shell: bash, zsh, or your preferred shell
  • Version Control: git for tracking changes
  • Testing: shellcheck for syntax checking

Useful Commands:

# Check script syntax
$ bash -n myscript.sh

# Debug script execution
$ bash -x myscript.sh

# Use shellcheck for best practices
$ shellcheck myscript.sh

Real-World Example

Here’s a practical backup script:

#!/bin/bash
# backup.sh - Simple backup script

# Configuration
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="backup_$DATE.tar.gz"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create backup
echo "Starting backup of $SOURCE_DIR..."
tar -czf "$BACKUP_DIR/$BACKUP_NAME" "$SOURCE_DIR"

# Check if backup was successful
if [ $? -eq 0 ]; then
    echo "Backup completed successfully: $BACKUP_DIR/$BACKUP_NAME"

    # Remove backups older than 7 days
    find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +7 -delete
    echo "Old backups cleaned up"
else
    echo "Backup failed!"
    exit 1
fi

Shell scripting is a powerful skill that bridges the gap between simple command-line usage and full programming. It’s an essential tool for anyone working with Linux or Unix systems.