How to Write Bash Scripts for Linux Task Automation

ShellShellBeginner
Practice Now

Introduction

This comprehensive bash scripting tutorial provides developers and system administrators with a practical guide to creating powerful automation scripts in Unix and Linux environments. From basic script structure to advanced techniques, learners will gain the skills needed to streamline system tasks, improve workflow efficiency, and write robust shell scripts.

Bash Scripting Basics

Introduction to Shell Scripting

Bash scripting is a powerful technique for automating tasks in Unix and Linux environments. As a fundamental skill in linux command line and unix shell programming, bash scripts enable system administrators and developers to create efficient, repeatable workflows.

Core Concepts of Bash Scripting

Script Structure and Execution

A typical bash script begins with a shebang line and contains a series of commands:

#!/bin/bash
## First bash script example

echo "Hello, Linux World!"

Key Components of Bash Scripts

Component Description Example
Shebang Specifies interpreter #!/bin/bash
Variables Store data name="John"
Conditionals Control flow if [ condition ]; then
Functions Reusable code blocks function greet() { }

Basic Script Execution Workflow

graph TD A[Write Script] --> B[Save .sh File] B --> C[Make Executable] C --> D[Run Script]

Practical Script Examples

Variable Declaration and Usage

#!/bin/bash
username="DevOps Engineer"
echo "Welcome, $username!"

current_date=$(date)
echo "Current date: $current_date"

Simple Conditional Logic

#!/bin/bash
age=25

if [ $age -ge 18 ]; then
    echo "You are an adult"
else
    echo "You are a minor"
fi

Script Permissions and Execution

To run a bash script, set executable permissions:

chmod +x script.sh
./script.sh

Advanced Scripting Techniques

Error Handling and Debugging

Trap and Signal Management

#!/bin/bash
trap 'echo "Error occurred!"; exit 1' ERR

function critical_operation() {
    [[ $? -ne 0 ]] && exit 1
}

Complex Control Structures

Advanced Loops and Conditionals

#!/bin/bash
declare -A servers=(
    [web]="nginx"
    [db]="postgresql"
    [cache]="redis"
)

for service in "${!servers[@]}"; do
    status=$(systemctl is-active "${servers[$service]}")
    case $status in
        active)
            echo "$service is running"
            ;;
        inactive)
            echo "$service is stopped"
            ;;
    esac
done

Script Optimization Techniques

Technique Description Performance Impact
Command Substitution Use $() Faster than backticks
Local Variables Scope limitation Reduces memory usage
Parameter Expansion Dynamic variable manipulation Efficient string processing

Parallel Processing

graph LR A[Main Script] --> B[Fork Process 1] A --> C[Fork Process 2] A --> D[Fork Process 3]

Concurrent Script Execution

#!/bin/bash
process_data() {
    local input=$1
    ## Complex data processing
}

for file in /data/*.log; do
    process_data "$file" &
done
wait

Dynamic Script Generation

#!/bin/bash
generate_config() {
    local service=$1
    local port=$2
    cat << EOF > "/etc/services/${service}.conf"
service_name=$service
listen_port=$port
EOF
}

generate_config "webserver" 8080

Advanced Parameter Handling

#!/bin/bash
while getopts ":h:p:" opt; do
    case $opt in
        h) host="$OPTARG"
        ;;
        p) port="$OPTARG"
        ;;
        \?) echo "Invalid option"
        ;;
    esac
done

Real-World Script Projects

System Health Monitoring Script

#!/bin/bash
LOG_DIR="/var/log/system_health"
mkdir -p $LOG_DIR

monitor_system() {
    ## CPU Usage
    echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4"%"}')" >> $LOG_DIR/system_health.log

    ## Memory Usage
    free -h | grep Mem: >> $LOG_DIR/system_health.log

    ## Disk Space
    df -h / >> $LOG_DIR/system_health.log
}

alert_critical_conditions() {
    local cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
    if (( $(echo "$cpu_usage > 80" | bc -l) )); then
        mail -s "High CPU Alert" [email protected] < $LOG_DIR/system_health.log
    fi
}

monitor_system
alert_critical_conditions

Automated Backup Solution

#!/bin/bash
BACKUP_DIRS=("/home" "/etc" "/var/www")
BACKUP_DESTINATION="/mnt/backup"
DATE=$(date +"%Y%m%d")

create_backup() {
    for dir in "${BACKUP_DIRS[@]}"; do
        tar -czf "$BACKUP_DESTINATION/backup_${dir//\//_}_$DATE.tar.gz" "$dir"
    done
}

rotate_backups() {
    find "$BACKUP_DESTINATION" -type f -mtime +30 -delete
}

create_backup
rotate_backups

Deployment Automation Workflow

graph TD A[Start Deployment] --> B[Pull Latest Code] B --> C[Run Tests] C --> D{Test Passed?} D -->|Yes| E[Deploy to Staging] D -->|No| F[Halt Deployment] E --> G[Deploy to Production]

Network Connectivity Checker

#!/bin/bash
CRITICAL_SERVICES=(
    "google.com:80"
    "github.com:443"
    "company-internal-service:8080"
)

check_network() {
    for service in "${CRITICAL_SERVICES[@]}"; do
        host=$(echo "$service" | cut -d: -f1)
        port=$(echo "$service" | cut -d: -f2)
        
        timeout 5 bash -c "</dev/tcp/$host/$port" 2>/dev/null
        
        if [ $? -eq 0 ]; then
            echo "$host:$port is ONLINE"
        else
            echo "$host:$port is OFFLINE"
            ## Optional: Send alert
        fi
    done
}

check_network

Service Management Script

Function Description Command
Start Services Initiate critical services systemctl start
Check Status Verify service health systemctl status
Restart Services Recover from failures systemctl restart

Log Rotation and Management

#!/bin/bash
LOG_PATHS=(
    "/var/log/nginx/access.log"
    "/var/log/mysql/slow-query.log"
)

rotate_logs() {
    for log in "${LOG_PATHS[@]}"; do
        if [ -f "$log" ]; then
            mv "$log" "$log.$(date +%Y%m%d)"
            touch "$log"
            chmod 640 "$log"
        fi
    done
}

compress_old_logs() {
    find /var/log -type f -name "*.log.*" -mtime +7 -exec gzip {} \;
}

rotate_logs
compress_old_logs

Summary

Bash scripting is a critical skill for modern system administrators and developers, enabling powerful automation and efficient task management across Unix and Linux platforms. By mastering script structure, variables, conditionals, and error handling techniques, professionals can create sophisticated scripts that simplify complex system operations and enhance productivity in technical environments.

Other Shell Tutorials you may like