Skip to main content

Command Palette

Search for a command to run...

Shell Scripting for DevOps: Automating Tasks with Bash

Updated
9 min read
Shell Scripting for DevOps: Automating Tasks with Bash

DevOps is heavily focused on automation. Instead of running the same commands manually every day, we write scripts that execute tasks automatically.

Shell scripts help automate:

  • Server setup

  • Application deployment

  • Log monitoring

  • Backup operations

  • Infrastructure tasks

  • CI/CD steps


1. What is Shell Scripting?

A shell script is a file containing a sequence of Linux commands that the shell executes.

Instead of typing commands manually in the terminal, we store them in a script file.

Example:

echo "Starting deployment..."
git pull
docker compose up -d
echo "Deployment completed"

This script can deploy an application in seconds.


2. Creating Your First Bash Script

Create a file:

vim hello.sh

Add this code:

#!/bin/bash

echo "Hello DevOps Engineer!"
echo "Welcome to Shell Scripting"

What is #!/bin/bash ?

This is called a shebang.
It tells the system to run the script using the bash shell.


3. Assigning Script Permissions

By default, scripts cannot be executed.

Give execute permission:

chmod +x hello.sh

Run the script:

./hello.sh

Output:

Hello DevOps Engineer!
Welcome to Shell Scripting

4. Variables in Shell Script

Variables store values that can be reused.

Example:

#!/bin/bash

name="Harry"
company="CodeTech"

echo "Hello $name"
echo "Welcome to $company"

Important rules:

  • No spaces around =

  • Access variables using $

Example with system variables:

#!/bin/bash

echo "Current User: $USER"
echo "Home Directory: $HOME"
echo "Current Directory: $PWD"

5. Constants in Shell Script

Constants are variables whose value should not change.

Example:

#!/bin/bash

readonly APP_NAME="MyApp"

echo "Application name is $APP_NAME"

Attempting to change it:

APP_NAME="NewApp"

Will result in:

readonly variable

6. Taking User Input

Shell scripts can take input from users.

Example:

#!/bin/bash

echo "Enter your name:"
read name

echo "Hello $name"

Example 2:

#!/bin/bash

read -p "Enter server IP: " server_ip

echo "Connecting to $server_ip"

7. If Condition

Used for decision making.

Syntax:

if [ condition ]
then
    commands
fi

Example:

#!/bin/bash

read -p "Enter your age: " age

if [ $age -ge 18 ]
then
    echo "You are eligible to vote"
fi

If-Else Example

#!/bin/bash

read -p "Enter a number: " num

if [ $num -gt 10 ]
then
    echo "Number is greater than 10"
else
    echo "Number is 10 or less"
fi

8. Automation Script: Package Installer

DevOps engineers often automate server setup.

Example script to install Nginx.

#!/bin/bash

echo "Installing Nginx..."

sudo apt update

sudo apt install nginx -y

echo "Starting Nginx service"

sudo systemctl start nginx
sudo systemctl enable nginx

echo "Nginx installation completed"

Script to Install Docker

#!/bin/bash

echo "Installing Docker..."

sudo apt update

sudo apt install docker.io -y

sudo systemctl start docker
sudo systemctl enable docker

echo "Docker installed successfully"

9. For Loop in Shell Script

Loops repeat tasks.

Syntax:

for variable in list
do
   commands
done

Example 1: Simple Loop

#!/bin/bash

for i in 1 2 3 4 5
do
    echo "Number: $i"
done

Example 2: Range Loop

for i in {1..5}
do
   echo "Iteration $i"
done

Example 3: C-Style Loop

for (( i=1; i<=5; i++ ))
do
    echo "Value: $i"
done

Practical Example: Multiple Server Ping

#!/bin/bash

servers=("google.com" "github.com" "docker.com")

for server in "${servers[@]}"
do
    ping -c 1 $server
done

10. While Loop in Shell Script

A while loop runs as long as a condition remains true.
It is useful when the number of iterations is not fixed.

Syntax:

while [ condition ]
do
    commands
done

Example:

#!/bin/bash

num=0

while [[ $num -le 10 ]]
do
    echo $num
    num=$((num+2))
done

Output:

0
2
4
6
8
10

Use cases in DevOps:

  • Retry operations

  • Polling services

  • Waiting for containers/servers to become ready

Example: wait until a service becomes available.

#!/bin/bash

while ! curl -s http://localhost:8000 > /dev/null
do
    echo "Waiting for service..."
    sleep 2
done

echo "Service is up!"

11. Script Arguments

Arguments allow passing values when running the script.

Example:

./deploy.sh nginx

Script:

#!/bin/bash

echo "Installing package: $1"

Important variables:

Variable Meaning
$1 First argument
$2 Second argument
$@ All arguments
$# Number of arguments

Example:

#!/bin/bash

echo "Total arguments: $#"
echo "All arguments: $@"

11. Functions in Shell Script

Functions organize code for reuse and avoid repetition.

Example:

#!/bin/bash

install_nginx() {
    sudo apt update
    sudo apt install nginx -y
}

install_nginx

Function with Arguments

#!/bin/bash

greet() {
    echo "Hello $1"
}

greet "DevOps Engineer"

12. Error Handling

DevOps scripts must detect failures.

Example:

#!/bin/bash

sudo apt install nginx -y

if [ $? -eq 0 ]
then
    echo "Installation successful"
else
    echo "Installation failed"
fi

$? returns the exit status of the previous command.

0 = success
non-zero = failure


Better Error Handling

set -e

#!/bin/bash

set -e

echo "Installing dependencies..."

sudo apt update
sudo apt install nginx -y

echo "Setup completed"

set -e stops the script if any command fails.


set -u

Stops the script if an undefined variable is used.

Example:

#!/bin/bash

set -u

echo $username

If username is not defined, the script exits.

This prevents hidden bugs.


13. Using grep in Shell Scripts

grep searches text.

Example:

Find errors in logs.

#!/bin/bash

grep "ERROR" /var/log/syslog

Practical example:

#!/bin/bash

if grep -q "docker" processes.txt
then
    echo "Docker process found"
else
    echo "Docker not running"
fi

14. Using awk in Shell Scripts

awk is used for text processing.

Example:

df -h | awk '{print \(1,\)5}'

Example:

Get usernames from /etc/passwd.

awk -F: '{print $1}' /etc/passwd

15. Using find in Shell Scripts

find searches files.

Example:

find /var/log -name "*.log"

Example in script:

#!/bin/bash

echo "Finding log files..."

find /var/log -name "*.log"

17. Input and Output Redirection

In Linux, commands normally print output to the terminal.
Redirection allows us to store output in files or redirect errors.


Redirect Output to a File

> overwrites a file.

echo "Deployment started" > deploy.log

File content:

Deployment started

Append Output to a File

>> appends instead of overwriting.

echo "Deployment completed" >> deploy.log

Input Redirection

< sends file content as input to a command.

Example:

wc -l < file.txt

Counts lines inside the file.


Redirect Errors

Errors normally go to stderr.

Example:

ls wrongfile 2> error.log

This stores errors inside error.log.


Redirect Both Output and Errors

command > output.log 2>&1

Example:

docker build . > build.log 2>&1

This stores:

  • normal output

  • errors

inside one log file.

This is very common in automation scripts.


Using && (Run Next Command if Previous Succeeds)

Example:

sudo apt update && sudo apt install nginx -y

Meaning:

Run install only if update succeeds

Useful in deployment scripts.


Using || (Run if Previous Command Fails)

Example:

mkdir project || echo "Directory already exists"

Meaning:

If mkdir fails, print message

Using | (Pipes)

Pipes send the output of one command as input to another.

Example:

ps aux | grep docker

Meaning:

List processes and filter docker

Pipes are extremely common in log analysis and monitoring scripts.


18. Automation Script: System Details

Below is a small script that prints useful system information, which is helpful for monitoring servers.

#!/bin/bash

<<usage
Creates functions for system information
- RAM used
- Storage used
- Top processes
usage

check_memory() {
    free -h | awk 'NR==2 {print $7}'
}

check_storage() {
    df -h | awk 'NR==2 {print $4}'
}

check_most_mem_consuming_process() {
    ps aux --sort=-%mem | awk 'NR==2 {print \(1,\)2,$4}'
}

show_details() {

    echo "======== SYSTEM DETAILS ========"

    echo "Available memory"
    check_memory

    echo "Available Storage"
    check_storage

    echo "Most memory intensive process"
    check_most_mem_consuming_process
}

show_details

Example output:

======== SYSTEM DETAILS ========
Available memory
5.2Gi
Available Storage
42G
Most memory intensive process
root 2456 3.4

This type of script can be used for:

  • server health checks

  • monitoring scripts

  • automation dashboards

  • cron-based system reports


Final Thoughts

Shell scripting plays a critical role in DevOps automation because it allows engineers to connect multiple tools together and automate infrastructure tasks.

With the concepts covered in this guide, you can now:

  • Write basic and advanced bash scripts

  • Automate server setup and deployments

  • Process logs using grep, awk, find

  • Build robust scripts with proper error handling

  • Monitor systems using automation scripts

Mastering shell scripting will significantly improve your ability to automate and manage infrastructure efficiently*.*