# Getting Started With Shell Scripting for DevOps

# Introduction

## What is shell scripting?

Shell scripting is writing a sequence of commands for the command-line shell (most commonly **Bash** on Linux) into a text file so they run automatically. Instead of typing commands one by one, you save them as a script (e.g., [`hello.sh`](http://backup.sh)) and execute the file and then the shell reads and runs each command in order.

```bash
#!/bin/bash
# hello.sh - a simple shell script
echo "Hello, world!"
```

Make it executable with `chmod +x` [`hello.sh`](http://hello.sh) and run `./`[`hello.sh`](http://hello.sh). That file is a shell script.

## Why DevOps engineers must know it

DevOps engineers work with servers, deployments and automation every day. Shell scripting makes this work faster and easier. It helps in automating tasks like taking backups, checking system health, installing packages or monitoring logs. Shell scripts run on almost every Linux server, so you do not need to install anything extra. They are also used inside CI/CD pipelines such as Jenkins, GitHub Actions and GitLab CI to run commands during build and deployment.

In short, shell scripting saves time, reduces errors and helps DevOps engineers automate their daily tasks efficiently.

# Basics You Must Know

## What is a shell

A shell is a program that takes the commands you type and tells the operating system to run them.  
The most commonly used shells in Linux are  
• bash (Bourne Again Shell)  
• sh (Bourne Shell)

When people talk about shell scripting, they usually mean writing scripts for bash.

## Creating and running a script

A shell script is simply a text file that contains a list of commands.

Steps to create and run your first script

1. Create a new file
    

```bash
nano hello.sh
```

2. Add some commands
    

```bash
echo "Hello from shell script"
```

3. Save and exit
    
4. Make the script executable using chmod +x
    

```bash
chmod +x hello.sh
```

5. Run the script
    

```bash
./hello.sh
```

## What is chmod +x

chmod is used to change file permissions.  
+x means giving the file permission to be executed like a program.  
Without this permission, the shell will not allow you to run the script directly.

## What is a Shebang

A shebang is the first line written at the top of a shell script.

Example

```bash
#!/bin/bash
```

This line tells the system which interpreter should run the script.  
In this case, it is the bash shell.

# Everyday Bash Commands Used in Scripts

Shell scripts mainly run Linux commands. These are the basic commands you will use almost every day.

## File operations

These commands help you create, copy, move or delete files and folders.

**Create a folder**

```bash
mkdir project
```

**Copy a file**

```bash
cp file1.txt backup.txt
```

**Remove a file**

```bash
rm oldfile.txt
```

---

## Viewing files

These commands help you read or search inside files.

**Show the full content of a file**

```bash
cat log.txt
```

**Scroll through a file slowly**

```bash
less log.txt
```

**Search for a word inside a file**

```bash
grep error log.txt
```

## Pipes and redirection

Pipes and redirection allow you to connect commands and control where output goes.

**Pipe symbol (|)**  
Takes the output of one command and sends it to another.  
Example

```bash
cat log.txt | grep error
```

This shows only the lines containing the word “error”.

**Single arrow (&gt;)**  
Sends the output to a new file. If the file already exists, it will be replaced.

```bash
echo "Backup complete" > status.txt
```

**Double arrow (&gt;&gt;)**  
Adds output at the end of an existing file.

```bash
echo "New entry added" >> status.txt
```

# Variables and Inputs

Shell scripts often store information in variables and take input from the user. This makes scripts flexible and interactive.

## Creating variables

A variable stores a value such as text, numbers or file names.

Example

```bash
name="Sushant"
age=22
```

Important  
There should be no spaces around the equal sign.

## Using a variable

To use the stored value, you put a dollar sign before the variable name.

Example

```bash
echo $name
echo $age
```

This will print the values stored in the variables.

## Reading user input

The read command allows your script to take input from the user while it is running.

Example

```bash
echo "Enter your name"
read username

echo "Hello $username"
```

In this script  
• The script asks the user to enter a name  
• The read command stores the input inside the username variable  
• The script prints a message using that variable

# Conditions (If Else)

Conditions help scripts make decisions. They allow the script to run different commands based on different situations.

## Basic syntax

This is the basic structure of an if else statement in bash.

```bash
if [ condition ]
then
    commands
else
    commands
fi
```

Example

```bash
num=10

if [ $num -gt 5 ]
then
    echo "Number is greater than 5"
else
    echo "Number is not greater than 5"
fi
```

## Checking files

You can check if a file or folder exists before running a command.

**Check if a file exists**

```bash
if [ -f myfile.txt ]
then
    echo "File found"
else
    echo "File not found"
fi
```

**Check if a directory exists**

```bash
if [ -d myfolder ]
then
    echo "Directory exists"
else
    echo "Directory does not exist"
fi
```

## Checking numbers

Bash uses special operators to compare numbers.

**Equal to**

```bash
[ $a -eq $b ]
```

**Not equal to**

```bash
[ $a -ne $b ]
```

**Greater than**

```bash
[ $a -gt $b ]
```

**Less than**

```bash
[ $a -lt $b ]
```

Example

```bash
marks=75

if [ $marks -ge 60 ]
then
    echo "Pass"
else
    echo "Fail"
fi
```

# Loops

Loops allow you to run a command multiple times. They are very useful in automation and DevOps.

## for loop

A for loop runs a command for each item in a list.

Example

```bash
for i in 1 2 3 4 5
do
    echo "Number is $i"
done
```

You can also loop through files

```bash
for file in *.txt
do
    echo "Found file: $file"
done
```

## while loop

A while loop keeps running as long as the condition is true.

Example

```bash
count=1

while [ $count -le 5 ]
do
    echo "Count is $count"
    count=$((count + 1))
done
```

## Simple DevOps example: looping over log files

This example checks all `.log` files inside a logs folder.

```bash
for log in logs/*.log
do
    echo "Checking file: $log"
    grep "error" $log
done
```

This script  
• Goes through every log file  
• Prints its name  
• Searches for the word "error" inside it

Useful when you want to quickly scan multiple log files on a server.

# Functions

Functions help you group a set of commands together and reuse them whenever needed. This makes your scripts cleaner and easier to maintain.

## Creating simple functions

A function is defined once and can be called many times.

Example

```bash
greet() {
    echo "Hello, welcome to the script"
}

greet
greet
```

The script will print the message two times because the function is called two times.

Another example with parameters

```bash
show_name() {
    echo "Your name is $1"
}

show_name "Sushant"
```

Here, `$1` represents the first argument passed to the function.

## Why functions matter in automation

Functions are very useful in DevOps and automation because

• They help avoid repeating code  
• They make scripts shorter and easier to read  
• They allow you to build reusable tasks like backup, clean up or health checks  
• They make large scripts more organized and modular

Example use cases  
• A function for checking CPU usage  
• A function for taking backups  
• A function for sending alerts

Functions turn a long script into small, manageable pieces that are easier to maintain.

# Automation With Cron Jobs

## What is cron

Cron is a time based scheduler in Linux.  
It allows you to run scripts automatically at fixed times, such as every minute, every hour, or every day.

Cron is very useful for DevOps engineers because it helps automate tasks without manual effort.

Examples of tasks cron can automate  
• Backups  
• Log cleanup  
• Health checks  
• Sending reports

## Scheduling a task

Cron jobs are stored in a file called the crontab.

To edit the crontab, use

```bash
crontab -e
```

Cron job format

```bash
* * * * * command
```

The five stars represent

1. Minute
    
2. Hour
    
3. Day of month
    
4. Month
    
5. Day of week
    

Example  
Run a script every day at 3 AM

```bash
0 3 * * * /home/user/backup.sh
```

## Example: daily backup script

**Step 1: Create a backup script**

```bash
#!/bin/bash

cp -r /home/user/data /home/user/backup_folder
echo "Backup completed at $(date)"
```

Save it as [`backup.sh`](http://backup.sh) and make it executable

```bash
chmod +x backup.sh
```

**Step 2: Schedule it with cron**

```bash
0 2 * * * /home/user/backup.sh
```

This will run the backup script every day at 2 AM.

# Real DevOps Examples

These are small scripts that DevOps engineers commonly use in real work. Each example is short, practical and easy to understand.

## Install packages

This script installs a list of packages on a Linux server.

```bash
#!/bin/bash

packages="nginx git curl"

for pkg in $packages
do
    echo "Installing $pkg"
    sudo apt-get install -y $pkg
done

echo "All packages installed"
```

This is useful for setting up a new server quickly.

## Backup a folder

This script creates a backup with the current date in the file name.

```bash
#!/bin/bash

source_folder="/home/user/data"
backup_folder="/home/user/backups"
date=$(date +%Y-%m-%d)

cp -r $source_folder $backup_folder/data-$date

echo "Backup completed"
```

A simple script like this can be connected to cron for automatic backups.

## Monitor CPU or memory

This script checks CPU usage and prints a warning if it is high.

```bash
#!/bin/bash

cpu=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')

echo "CPU Usage: $cpu"

if (( $(echo "$cpu > 80" | bc -l) ))
then
    echo "Warning: High CPU usage"
fi
```

You can use a similar script to monitor memory, disk usage or logs on a server.

# Conclusion

Shell scripting is one of the most important skills for anyone working in DevOps. It helps automate everyday tasks, makes server management faster, and reduces manual work. With simple scripts, you can take backups, install packages, monitor system health and manage logs without doing the same steps again and again. Once you understand the basics, you can build powerful automation that saves time and avoids human errors.
