Shell Scripting
I am a Student, who finds beauty in simple things. I like to teach sometimes.
Search for a command to run...
I am a Student, who finds beauty in simple things. I like to teach sometimes.
No comments yet. Be the first to comment.
A step-by-step series for absolute beginners to learn how computers work, how to install and use an operating system, and how to start programming.
Effective software development relies on robust tools for managing code changes and facilitating collaboration. Git, a distributed version control system, and GitHub, a platform for hosting Git repositories, are fundamental components in modern devel...
There is a distinct heaviness that descends when life proceeds smoothly on the surface. Externally, everything may be stable, yet the desire to die can persist not because of tragedy, but because of a realization regarding the future. If the destinat...
"I must not fear abstraction. Abstraction is the mind-killer. Abstraction is the little-death that brings total obliteration. I will face my abstraction. I will permit it to pass over me and through me. And when it has gone past I will turn the inner...
Ever wondered how the Python or JavaScript code you write actually makes your computer's fans spin up? How do abstract commands like print("Hello, World!") get turned into physical actions? The magic lies in a fundamental, deeply interconnected relat...
I'll speed through setting up an ASIC synthesis flow for the Ibex RISC-V core using entirely open-source tools. Tools Python 3.12.8 (for environment management) Yosys (logic synthesis) sv2v (SystemVerilog to Verilog conversion) OpenSTA (static ti...
Imagine this scene: A dimly lit room, humming with the quiet thrum of advanced technology. Three alien scientists are hunched over a console, staring intently at a string of data flashing across a screen: 0101010100... Alien Scientist #1: "It isn't r...
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 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.
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:
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
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
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.
Bash scripting is extensively used for automating various system administration and development tasks.
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 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.