Advanced Linux Shell Scripting for DevOps Engineers

Photo by RoonZ nl on Unsplash

Advanced Linux Shell Scripting for DevOps Engineers

Hello, I am Krishnapal Singh and I am excited to share my learning on #90DaysOfDevOps through this task.

TASK 1 : Creating Dynamic Directories with Bash scripting. It is a powerful tool that allows developers to automate various tasks. In this article, we will look at how to create dynamic directories with bash scripting.

#!/bin/bash

for (( i=$2; i<=$3; i++));do
    mkdir $1$i
    echo "created directory: $1$i"
done

In this script, we will take three arguments, the directory name, the starting number of directories, and the ending number of directories. The line for (( i=$2; i<=$3; i++));do starts a for loop that runs from the start number of directories specified by the second argument ($2) to the end number of directories specified by the third argument ($3). The loop variable is named i, and it increments by 1 with each iteration.

The mkdir $1$i command creates a new directory with the specified directory name ($1) and the current value of the loop variable ($i). $1$i concatenates the value of $1 and $i together to create the dynamic directory name.

The final line echo "created directory: $1$i" simply outputs a message to the console confirming that the directory has been created.

TASK 2: Backing Up Files with Bash Scripting Backups are a crucial aspect of DevOps engineers' daily activities. In this section, we will look at a simple bash script to back up a file.

In this script, we will set the source and destination paths, the backup filename, and the file to be backed up. We will then create the backup archive using the tar command.

For example, if we set the source directory to "/home/vagrant" and the file to be backed up to "script4.sh", and the destination directory to "/home/vagrant/backupfolder", the script will create a backup archive named "my_backup.tar" in the destination directory.

Here's an example of the script:

#!/bin/bash

# set the source and destination paths
src_dir="/home/vagrant"
dest_dir="/home/vagrant/backupfolder"

# set the filename for the backup
backup_filename="my_backup.tar"

# set the filename of the file to backup
file_tobackup="script4.sh"

# create the backup archive
tar -cvf "${dest_dir}/${backup_filename}" "${src_dir}/${file_tobackup}"
cd "$dest_dir"

TASK 3 : Understanding Cron and Crontab Cron is a time-based job scheduler in Linux that allows users to schedule commands or scripts to run automatically at specific intervals. The crontab command is used to create, edit, and manage cron jobs.

The syntax for creating a cron job is as follows:

* * * * * /path/to/command arg1 arg2

The asterisks represent the timing information for the job. From left to right, they represent minutes, hours, days, months, and weekdays. For example, "* " means the job will run every minute.

Here's an example of a cron job that runs a script every day at 3:30 AM:

30 3 * * * /path/to/script.sh

In this example, "30 3 *" means the script will run every day at 3:30 AM.

Cron jobs are a powerful tool for automating tasks and saving time for DevOps engineers. By using cron, you can schedule tasks to run automatically and focus on other important tasks.

#devops #happy_learning #90DaysofDevOps. Hit like & connect with me :krishnapal singh rawat .