Skip to main content

Command Palette

Search for a command to run...

Shell Scripting

Updated
6 min read
A

I am a Student, who finds beauty in simple things. I like to teach sometimes.

Shell scripting provides a powerful method for automating repetitive tasks within a Unix-like operating system. Bash (Bourne Again SHell) is a widely used command-line interpreter and scripting language. This document details the fundamentals of Bash scripting, including the creation and execution of .sh files, and its application in automation tasks like backups and system setup.

Bash Script Files: Structure and Creation

Bash scripts are plain text files containing a sequence of commands that are executed by the Bash interpreter. By convention, these files are given a .sh extension, though it is not strictly required for execution.

The first line of a Bash script is crucial and is known as the "shebang." It specifies the interpreter that should be used to execute the script. For Bash scripts, this line is typically:

#!/bin/bash

The #! characters are a special sequence recognized by the kernel. When a script with a shebang is executed, the kernel invokes the program specified on that line (in this case, /bin/bash) and passes the script file as an argument to it. If the path to Bash differs on a system, the shebang line should reflect that correct path (e.g., #!/usr/bin/env bash for a more portable approach that searches for bash in the user's PATH).

Following the shebang, the script contains any number of shell commands, comments, variables, control flow structures (like if statements, for and while loops), and function definitions. Comments begin with a # symbol and are ignored by the interpreter, serving to explain the script's logic.

Example of a simple script (myscript.sh):

#!/bin/bash

# This is a comment
echo "Hello, World!"
CURRENT_DATE=$(date +"%Y-%m-%d")
echo "Today's date is: $CURRENT_DATE"

In this example:

  • #!/bin/bash designates the Bash interpreter.

  • # This is a comment is a comment.

  • echo "Hello, World!" prints the string "Hello, World!" to the standard output.

  • CURRENT_DATE=$(date +"%Y-%m-%d") assigns the output of the date command (formatted as YYYY-MM-DD) to a variable named CURRENT_DATE. The $() construct is called command substitution.

  • echo "Today's date is: $CURRENT_DATE" prints the value of the CURRENT_DATE variable.

Executing Bash Scripts

To run a Bash script, it first needs to have execute permissions. The chmod command is used to set these permissions.

chmod +x myscript.sh

This command grants execute (+x) permission to the owner of the file myscript.sh.

Once the script has execute permission, it can be run in several ways:

  1. Specifying the full or relative path: If the script is in the current directory, it can be executed as:

     ./myscript.sh
    

    If it's in another directory, the full path must be provided:

     /path/to/your/script/myscript.sh
    
  2. Using the bash interpreter directly: This method does not require the script to have execute permission, nor does it strictly require the shebang line (though it's good practice to include it).

     bash myscript.sh
    
  3. Placing the script in a directory listed in the PATH environment variable: If a directory containing scripts is added to the user's PATH, the scripts within it can be executed by simply typing their names, just like any other system command. This is common for frequently used utility scripts.

Automation with Bash Scripting

Bash scripting is extensively used for automating various system administration and development tasks.

Backup Scripts

Automating backups is a common use case. Scripts can be written to archive files and directories, optionally compress them, and store them in a designated backup location. The tar command is frequently used for archiving and compression.

Example of a simple backup script (backup.sh):

#!/bin/bash

# Configuration
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/mnt/backup_drive/daily_backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
ARCHIVE_FILE="$BACKUP_DIR/backup_$TIMESTAMP.tar.gz"

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

# Create the archive
tar -czf "$ARCHIVE_FILE" "$SOURCE_DIR"

# Report status
if [ $? -eq 0 ]; then
  echo "Backup successful: $ARCHIVE_FILE"
else
  echo "Backup failed"
  exit 1
fi

# Optional: Remove backups older than 7 days
find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +7 -delete
echo "Old backups removed."

exit 0

In this backup script:

  • SOURCE_DIR specifies the directory to be backed up.

  • BACKUP_DIR defines the location where backups will be stored.

  • TIMESTAMP creates a unique timestamp for each backup file.

  • ARCHIVE_FILE is the full path and name of the compressed backup file.

  • mkdir -p "$BACKUP_DIR" creates the backup directory if it's not already present. The -p option ensures that parent directories are also created if needed.

  • tar -czf "$ARCHIVE_FILE" "$SOURCE_DIR" is the core backup command:

    • c: Creates a new archive.

    • z: Compresses the archive using gzip.

    • f: Specifies the archive file name.

  • $? is a special shell variable that holds the exit status of the last executed command. An exit status of 0 typically indicates success.

  • find ... -mtime +7 -delete locates and removes backup files older than 7 days.

This script can then be scheduled to run automatically using a cron job. For example, to run it daily at 2 AM, the crontab entry might look like:

0 2 * * * /path/to/your/script/backup.sh

Setup Scripts

Setup scripts are used to automate the configuration of new systems or software environments. They can install necessary packages, configure system settings, create directories, set up user accounts, and perform other initial setup tasks.

Example of a simple setup script (setup_dev_env.sh):

#!/bin/bash

echo "Starting development environment setup..."

# Update package lists
sudo apt update

# Install essential packages (example for a Debian-based system)
echo "Installing Git, Python3, and pip..."
sudo apt install -y git python3 python3-pip

# Verify installations
git --version
python3 --version
pip3 --version

# Create a projects directory
PROJECTS_DIR="$HOME/projects"
if [ ! -d "$PROJECTS_DIR" ]; then
  mkdir "$PROJECTS_DIR"
  echo "Created directory: $PROJECTS_DIR"
else
  echo "Directory $PROJECTS_DIR already exists."
fi

# Configure Git (replace with actual user info)
echo "Configuring Git..."
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"

echo "Development environment setup complete."
exit 0

This setup script:

  • Updates the system's package manager repository.

  • Installs specified software packages (git, python3, python3-pip) using apt (Debian/Ubuntu). The -y flag automatically confirms prompts.

  • Verifies the installations by printing their versions.

  • Creates a projects directory in the user's home directory if it doesn't already exist. The [ ! -d "$PROJECTS_DIR" ] is a test condition checking if the directory does not exist.

  • Configures global Git settings.

Running this script on a new system can significantly speed up the process of preparing it for development work. It ensures consistency and reduces the chance of manual errors.

Bash scripting is a fundamental skill for system administrators, developers, and power users. Its capacity to automate command sequences makes it an indispensable tool for managing systems efficiently and reliably. By understanding file structures, execution methods, and common command utilities, one can harness Bash to streamline a wide array of computational tasks.

More from this blog

Aman Pathak

58 posts

Things I would speak if the person in front of me is me