Linux Scripting Mastery

Automate, optimize, and streamline your workflows with professional scripting techniques

Bash Essentials

Shebang & Variables

The shebang line specifies the interpreter. Always declare variables with meaningful names and use quotes to prevent word splitting.

#!/bin/bash
NAME='Linux Expert'
echo 'Hello $NAME'

💡 Use readonly for constants

Conditionals

Modern bash supports double brackets for pattern matching and regex comparisons. Always quote variables in conditionals.

if [[ -f 'file.txt' ]]; then
  echo 'File exists'
fi

💡 Use [[ ]] for advanced tests

Parameter Expansion

Parameter expansion provides powerful string manipulation capabilities directly in bash.

echo ${VAR:-default}
${VAR%%.*} # Remove extension

💡 Use ${VAR?} for required params

Advanced Techniques

Process Substitution

Treat command output as temporary files for complex pipelines.

diff <(ls dir1) <(ls dir2)

💡 Use >(cmd) for output

Signal Handling

Ensure proper cleanup and graceful shutdown of long-running scripts.

trap 'cleanup' EXIT SIGINT SIGTERM

💡 Reset traps with trap - SIGNAL

Arrays & Associative Arrays

Bash 4+ supports complex data structures for advanced scripting.

declare -a files=()
declare -A config=([user]=admin)

💡 Use += for appending

Text Processing

AWK Patterns

AWK is ideal for column-based data processing and reporting.

awk '/error/ {count++} END {print count}' log.txt

💡 Use BEGIN for initialization

Grep Strategies

Combine grep options like -ivrn for powerful text searching.

grep -P '\d{3}-\d{4}' contacts.txt

💡 Use -E for extended regex

Here Documents

Embed documents/templates directly in scripts.

cat <<EOF
Multi-line
content
EOF

💡 Use <<- to strip tabs

Python Integration

CLI Arguments

Create user-friendly command-line interfaces with validation.

import argparse
parser = argparse.ArgumentParser()

💡 Use click for complex CLIs

File Handling

Context managers ensure proper resource cleanup.

with open('file.txt') as f:
    data = f.read()

💡 Use pathlib for paths

Subprocess Management

Safely execute system commands with proper error handling.

subprocess.run(['ls', '-l'], check=True, capture_output=True)

💡 Use shlex.split for complex commands

Scheduling & Logging

Log Rotation

Maintain manageable log files and prevent disk space issues.

/var/log/*.log {
  daily
  rotate 7
  compress
  delaycompress
}

💡 Test with logrotate -d

Systemd Services

Create robust managed services with automatic restarts.

[Service]
ExecStart=/path/to/script.sh
Restart=on-failure

💡 Use journalctl for logs

Timeouts

Prevent hung processes from affecting system operations.

timeout 30s long_running.sh

💡 Use --preserve-status

Scripting Best Practices

Input Validation

Always sanitize and validate external inputs

if [[ ! -f "$INPUT_FILE" ]]; then
  echo "Invalid file" >&2
  exit 1
fi
  • ✓Check file existence and permissions
  • ✓Validate expected formats
  • ✓Sanitize user-provided strings

Error Handling

Implement comprehensive error recovery

set -eo pipefail
trap 'error_handler $LINENO' ERR
  • ✓Use set -euo pipefail
  • ✓Implement cleanup traps
  • ✓Log errors with timestamps

Documentation

Maintain inline and external documentation

## Purpose: Automated backup script
## Usage: ./backup.sh [OPTIONS]
  • ✓Header with purpose and author
  • ✓Usage examples
  • ✓Option descriptions

Version Control

Manage scripts with Git

git init
git add .
git commit -m 'Initial script version'
  • ✓Use meaningful commit messages
  • ✓Tag production versions
  • ✓Maintain changelog

Debugging Techniques

bash -x

Trace script execution

bash -x script.sh

set -x

Enable debug mode in script

set -x # Your code set +x

trap 'echo $BASH_COMMAND' DEBUG

Trace individual commands

trap 'echo "Executing: $BASH_COMMAND"' DEBUG

PS4

Custom debug prompt

PS4='+ ${BASH_SOURCE}:${LINENO}: '

shellcheck

Static analysis tool

shellcheck script.sh

pdb

Python debugger

python3 -m pdb script.py

Security Checklist

âš Validate all user inputs
âš Use parameter expansion instead of eval
âš Avoid command injection vulnerabilities
âš Set proper file permissions
âš Use password-less sudo judiciously
âš Sanitize environment variables
âš Limit script privileges with setuid/setgid
âš Regularly audit third-party dependencies