Understanding Shell Scripting

Understanding Shell Scripting


Shell scripting is a powerful way to automate repetitive tasks, manage system processes, and handle system administration tasks in a Linux environment. By writing simple scripts, you can significantly improve productivity and reduce errors. This blog will introduce you to shell scripting basics, followed by examples of real-world tasks using scripts to install packages and create backups.


Example 1: Installing Nginx

Here’s a script that installs the popular web server Nginx:

#!/bin/bash

<<note
This script will install nginx
note

echo "*************INSTALLING NGINX*************"

sudo apt-get update
sudo apt-get install nginx -y

sudo systemctl start nginx
sudo systemctl enable nginx

echo "*************INSTALLED NGINX*************"        

Explanation

  1. Documentation: The <<note and note block provides a multi-line comment describing the script’s purpose.
  2. Updating the Package List:sudo apt-get update ensures the package list is up to date before installation.
  3. Installing Nginx:sudo apt-get install nginx -y installs Nginx, where -y automatically confirms the installation prompt.
  4. Starting and Enabling Nginx:

- sudo systemctl start nginx starts the web server immediately.

- sudo systemctl enable nginx ensures Nginx starts automatically on system boot.


Example 2: Installing Any Package

Sometimes, you might want a generic script to install any package by simply passing its name as an argument.

#!/bin/bash

<<note
This Script will install any package passed as argument
note

echo "**********INSTALLING $1**********"

sudo apt-get update
sudo apt-get install $1 -y

sudo systemctl start $1
sudo systemctl enable $1

echo "**********INSTALLED $1**********"        

Explanation

  1. Dynamic Package Name: $1 is a parameter, which means it takes the value of the first argument passed to the script. For example, running ./install.sh apache2 will replace $1 with apache2.
  2. Modularity: This script is versatile and reusable for any package. Simply change the argument to install a different package.
  3. Example Usage:

./install.sh apache2  # Installs Apache2
./install.sh mysql    # Installs MySQL        

4. Starting and Enabling the Package:After installation, the script attempts to start and enable the service.


Example 3: Creating Backups

Backups are essential for safeguarding data. The following script creates a compressed backup of any directory.

#!/bin/bash

<<note
This script takes backup of any destination path given in argument

./backup.sh /home/ubuntu/scripts
note

function create_backup() {
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')

backup_dir="/home/ubuntu/backups/${timestamp}_backup.zip"

zip -r $backup_dir $1

echo "BACKUP COMPLETE"
}

create_backup $1
        

Explanation

  1. Timestamped Backups: The script generates a unique timestamp using date '+%Y-%m-%d_%H-%M-%S', ensuring every backup file has a unique name.
  2. Backup Directory: The backup is saved in /home/ubuntu/backups/. The output file is a .zip archive named with the timestamp.
  3. Zipping Files: zip -r $backup_dir $1 recursively compresses the specified directory into a zip file.
  4. Functionality: The create_backup function encapsulates the backup logic, making the script modular and easy to maintain.
  5. Dynamic Backup Path: $1 is used to accept the target directory as an argument.

./backup.sh /home/ubuntu/scripts        


To view or add a comment, sign in

More articles by Rishabh Sharma

  • Docker Volumes

    Docker volumes let you save data used by your containers, so it doesn’t disappear when the container stops or is…

  • Docker Fundamentals : Hands On - II

    Understanding Dockerfile A Dockerfile is a simple text file containing a set of instructions used to build a Docker…

  • Docker Fundamentals : Hands On - I

    Setting Up Docker on AWS EC2 (Ubuntu) Step 1: Update the Package List Purpose: Updates the package list on your system…

  • Docker Fundamentals : Part III

    Understanding Docker Engine Docker Engine is the core software that enables the creation, management, and operation of…

  • Docker Fundamentals : Part II

    Understanding Virtualization Virtualization is a technology that allows multiple operating systems (OS) to run on a…

  • Docker Fundamentals : Part I

    Understanding Docker in Layman's Terms Imagine you bake a cake. To do this, you need specific ingredients, tools, and…

    1 Comment
  • Exploring GitHub

    If you are new to Git and Github , check out this blog for understanding git. What Is GitHub? GitHub is a web-based…

  • Version Control System: Git

    1. What is a Version Control System (VCS)? A Version Control System (VCS) is a crucial tool that enables developers to…

  • Basics of Shell Scripting

    What is a Shell Script? A shell script is simply a text file that contains a series of commands to be executed by the…

  • Linux System Management Tools: systemctl and find

    Linux system management can be both powerful and efficient when you know how to use the right command-line tools. Two…

Insights from the community

Others also viewed

Explore topics