# A Beginner's Guide to Shell Scripting in DevOps

# Shell Scripting for DevOps - Automate Your Tasks

**Lecture 8** in the DevOps journey dives deep into the essential topic of **Shell Scripting**. In this blog, we’ll cover what shell scripting is, its purpose, basic commands, script creation, automation using cron jobs, and more. Shell scripting is a crucial skill for DevOps engineers, as it allows for task automation and interaction with the OS kernel.

---

## What is Shell Scripting?

1. **Definition**: Shell scripting is essentially a file containing a series of shell commands, which can be executed sequentially.
    
2. **Purpose**: It’s used to automate repetitive tasks, which is valuable in DevOps for saving time and reducing human error.
    
3. **Shell**: A shell is a command-line interface that interacts with the OS kernel. There are multiple types of shells, including `bash` (Bourne Again Shell) and `sh`.
    
4. **File Extension**: Shell script files typically have the `.sh` extension.
    
5. **Shebang**: `#!/bin/bash` is a shebang that specifies the script is for the bash shell.
    

---

## Creating and Running Shell Scripts

To create a shell script, follow these steps:

1. **Create Script**: Use `vim` [`hello.sh`](http://hello.sh) to create a file.
    
2. **Make Executable**: Use `chmod 774` [`hello.sh`](http://hello.sh) to make it executable.
    
3. **Run Script**: Use `./`[`hello.sh`](http://hello.sh) or `bash` [`hello.sh`](http://hello.sh) to execute.
    

```bash
# Example of a simple shell script
#!/bin/bash

# This is a comment
echo "Hello, World!"
```

---

## Comments in Shell Scripts

1. **Single-line Comment**: Use `#` at the start of the line.
    
2. **Multi-line Comment**:
    
    * Use the syntax `<<COMMENT` and `COMMENT` to enclose multiple lines.
        
    
    ```bash
    <<COMMENT
    This is a multi-line comment example.
    COMMENT
    ```
    

---

## Assignment: Creating a Movie Dialog Script

Here’s a simple script simulating a dialog from the movie *Salaar*:

```bash
#!/bin/bash

# This is Salaar movie dialogue

<<Salaar
Vardha and Deva ki dost
Salaar

echo "Deva: Tu jab bhi bulayega mai aaunga"
echo "Vardha: Vaada kar!"
echo "Deva: Ye vaada hai mera."
echo "Vardha: Deva abb chal sath mai"
echo "Deva: Chal bhai."
echo "Vardha: Ruk jara date dekhlu tuje le jane ka: "
date
```

1. **Make Executable**: `chmod 774` [`salaar.sh`](http://salaar.sh)
    
2. **Run Script**: `./`[`salaar.sh`](http://salaar.sh)
    
3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1731084843738/e79fa359-e625-4da1-a085-ac9f78e02e45.png align="center")
    

---

## Scripting Concepts

### Variables and Constants

1. **Print Output**: `echo` is used to print anything.
    
2. **User Input**: `read` gathers input from the user, and `read -p` adds a prompt.
    
    ```bash
    #!/bin/bash
    read -p "Enter your name: " name
    echo "My name is $name."
    ```
    

### Installing Packages via Script

To automate package installation, here’s a script to install NGINX:

```bash
#!/bin/bash
echo "***************INSTALLING NGINX**************"
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
echo "**************INSTALLED NGINX******************"
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1731084940939/f03073c6-b379-49a5-a4ed-9ee975ef564e.png align="center")

---

## Installing Packages Using a Script

In this section, I learned how to create a script to install any package passed as an argument. Here's the shell script to install any package using a command-line argument:

### Script: `install_`[`package.sh`](http://package.sh)

```bash
#!/bin/bash

<< note
This script will install any package passed as an argument.

./install_package.sh <arg>

$1 is used to get the first argument (<arg>) which is passed by running the above command to install any package.
note

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

# Updating system
sudo apt update

# Installing the package
sudo apt install $1 -y

# Start and enable the service
sudo systemctl start $1
sudo systemctl enable $1

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

### Steps to Execute:

1. Create the script using `vim install_`[`package.sh`](http://package.sh).
    
2. Change the file permissions:  
    `chmod 774 install_`[`package.sh`](http://package.sh).
    
3. Run the script with the desired package as an argument. For example, to install **SSH**, run:  
    `./install_`[`package.sh`](http://package.sh) `ssh`.
    

### Output Image:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1731085126643/cc636189-c493-4e06-a18f-270051d9b226.png align="center")

---

## Creating a Backup of Scripts

A backup script example:

```bash
#!/bin/bash

<< note

This scripts takes backup of any destination path given in argument
./backup.sh /home/ubuntu/scripts

note

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

backup_dir="${timestamp}_backup.zip"

zip -r $backup_dir $1

echo "Backup Completed!"
```

1. **Execute**: `./`[`backup.sh`](http://backup.sh) `/home/ubuntu/scripts`
    
2. **Result**: This compresses all scripts in `/home/ubuntu/scripts` into a `.zip` file with a timestamp.
    
3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1731085301889/6af32dc4-3a4c-4602-a42e-37f033373326.png align="center")
    

---

## Automating Execution with Cron Jobs

**Cron** is a tool that automates tasks on a schedule.

1. **Editing Cron Jobs**: Use `crontab -e` to access and edit the cron file.
    
2. **Schedule Example**: The following runs a backup script every minute.
    
    ```bash
    */1 * * * * bash /home/ubuntu/scripts/backup.sh /home/ubuntu/scripts
    ```
    
3. **Updated** [`backup.sh`](http://backup.sh) Script: This updated script stores backups in a `backups` directory with a timestamp.
    
    ```bash
    #!/bin/bash
    
    << note
    
    This scripts 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 Completed!"
                                                                       
    }          
    create_backup $1
    ```
    
4. **To Stop Cron**: Comment out or delete the scheduled job in `crontab`.
    
5. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1731085196835/e6461718-1a10-4046-b424-85f5547ccdef.png align="center")
    

---

## Conclusion and Next Steps

This blog covered foundational shell scripting concepts, from creating scripts to automating backups using cron jobs. Shell scripting is a powerful tool in DevOps that enables engineers to automate complex tasks efficiently. In the upcoming blog, we’ll explore **Backup Rotation**—a critical practice for managing space and organizing backups systematically.

### Stay tuned for the next blog on **Backup with Rotation**, where we’ll delve into creating a robust script to handle backups while managing storage space.

---
