Day 4: Linux Shell Scripting for DevOps Engineers.

Radheya Zunjur
3 min readJun 23, 2023

--

Today we are going to learn Linux Shell Scripting. As DevOps engineer one should be aware of scripting because it allows them to automate tasks, manage infrastructure as code, facilitate continuous integration and deployment, and orchestrate various operations efficiently. It helps save time, improve efficiency, and ensure consistency in software development and operations.

1)What is Kernel?
The kernel is the core component of an operating system. with complete control over everything in the system. It acts as a bridge between the hardware and the software layers, providing essential services and managing system resources. The kernel is responsible for tasks such as memory management, process scheduling, device driver management, and handling system calls

What is Shell ?
A shell is a command-line interpreter or a program that provides a user interface for interacting with an operating system. It allows users to execute commands, run scripts, and perform various operations by typing commands in a shell prompt.

2) What is #!/bin/bash?
#!/bin/bash vs. #!/bin/sh: The #!/bin/bash and #!/bin/sh are called shebangs and are used to specify the interpreter to execute the script. The shebang #!/bin/bash indicates that the script should be interpreted using the Bash shell. Bash (Bourne Again SHell) is a popular and feature-rich shell used in many Unix-like systems. On the other hand, #!/bin/sh refers to the system’s default shell interpreter.

3) What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

4) What is Cron?

In DevOps, Cron is a time-based job scheduler that allows for the automation of recurring tasks and job execution at specified intervals. It is commonly used to automate routine operations, such as deployments, backups, system maintenance, and data synchronization. Cron plays a crucial role in streamlining and orchestrating various processes within the DevOps workflow, enabling efficient task automation and improving overall operational efficiency.

5) What is Crontab?

In DevOps, crontab is a command that enables the management and scheduling of cron jobs. It allows DevOps engineers to create, edit, and remove cron jobs according to specific schedules. Crontab is an essential tool for automating repetitive tasks, executing scripts, and ensuring timely execution of scheduled operations within the DevOps environment. It facilitates the efficient management of recurring tasks, enabling smoother operations and improved productivity in DevOps workflows.

crontab -e = To set an cron job

crontab -l = To list all cron jobs running

Shell Scripts :

  1. Write a shell script to add user to linux

#!/bin/bash

add_user()
{
USER=$1
PASS=$2

useradd -m -p $PASS $USER && echo “Successfully added $user”

}

#MAIN

add_user radhey test@123

2) Write Shell Script to take backup of a directory

#!/bin/bash

src_dir=/home/ubuntu/scripts
tgt_dir=/home/ubuntu/backup

curr_timestamp=$(date “+%Y-%m-%d-%H-%M-%S”)
backup_file=$tgt_dir/$curr_timestamp.tgz
echo “$backup_file”
echo “taking backup at $curr_timestamp”

tar czf $backup_file $src_file ./*
echo “backup file”

3) Write Shell Script to check disk space usage of your system?

#!/bin/bash

df -H | awk ‘{print $5 “ “ $1}’ | while read output;
do
echo “Disk detail $output”

usage=$(echo $output | awk ‘{print $1}’ | cut -d’%’ -f1)
file_sys=$(echo $output | awk ‘{print $2}’)
echo $usage
if [ “$usage” -ge 90 ]; then
echo “CRITICAL for $file_sys”
fi
done

4) Write a Cron Job to check disk space usage every minute and write that log file into different directory.

#!/bin/bash
alert=90
backup_date=$(date +’%m/%d/%Y %H:%M:%S’)
echo $backup_date
df -H | awk ‘{print $5 “ “ $1}’ | while read output;
do
echo “Disk detail $output”

usage=$(echo $output | awk ‘{print $1}’ | cut -d’%’ -f1)
file_sys=$(echo $output | awk ‘{print $2}’)
echo $usage
if [ “$usage” -ge $alert ]; then
echo “CRITICAL for $file_sys on $backup_date”
else
echo “No Critical disk space issue was found for $file_sys on $backup_date”
fi
done

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet