#Day 6 Advance Shell Scripting.

#Day 6 Advance Shell Scripting.

Write a Script to create multiple directories.

Well, that's not a big deal, we can create a no of directories in one command.

mkdir day{1..90},

surely, it will create 90 directories in one go. But Wait!! How you can customize it, if the user has another specific requirement how this will work?

Shell Script Approach:

Our shell script, createdirectories.sh, allows for the creation of directories with customizable names and ranges. By providing arguments for the directory name prefix, start number, and end number, users can generate directories tailored to their specific requirements. Here's a quick recap of how the script works:

  • Flexibility: Users have the flexibility to define the directory name prefix and the range of directories to create.

  • Automation: Once the script is executed with the specified arguments, it handles the directory creation process automatically.

  • Script Reusability: Users can reuse the script with different parameters, making it suitable for various directory creation tasks.

Result

Explanation of Script.

Let's break down the script line by line.

#!/bin/bash

This line specifies the interpreter for the script, indicating that it should be executed using the Bash shell.

#Here we will try to create directories using shell script

This is a comment that provides a brief explanation of what the script aims to do. Comments are lines in a script that are not executed and are meant for human readers to understand the code.

echo "enter root_dir name"

This line prints a message to the terminal asking the user to enter the name of the root directory.

read root_dir

This line reads the input provided by the user and assigns it to the variable root_dir.

mkdir $root_dir && cd $root_dir

This line creates the root directory specified by the user ($root_dir) using the mkdir command. The && operator ensures that the next command (cd $root_dir) is executed only if the previous command (mkdir $root_dir) is successful. It changes the current directory to the newly created root directory.

echo "enter the custom directory name"

This line prints a message to the terminal asking the user to enter the name of the custom directory.

read custom_dir

This line reads the input provided by the user and assigns it to the variable custom_dir.

for ((num=$1; num<=$2; num++)); do

This line starts a for loop that iterates from the value of the first command-line argument ($1) to the value of the second command-line argument ($2). The loop variable num is initialized with the value of the first argument, incremented by 1 in each iteration.

mkdir "${custom_dir}${num}"

Within the loop, this line creates a directory using the mkdir command. The directory name is constructed by concatenating the custom_dir variable with the current value of num.

done

This line marks the end of the for loop.

Create a Script to back up all your work done till now.

Fantastic, For an actual work-life example, It may be necessary for you to take folder backups as a DevOps engineer. Let's see if we can figure out how to make a backup, zip it up, and store it somewhere else.

Explanation of script.

#!/bin/bash

This line specifies the interpreter to be used for running the script, which is bash in this case.

source_directory="/home/ubuntu/scripts" target_directory="/home/ubuntu/backups"

These lines define two variables: source_directory and target_directory, representing the source directory from which backups will be taken and the target directory where the backups will be stored, respectively. Adjust these paths according to your actual source and target directories.

backup_filename="backup_$(date +%Y-%m-%d).tar.gz"

This line creates a variable backup_filename and assigns it a value. It's a string containing the word "backup_" followed by the current date in the format "YYYY-MM-DD", and ending with ".tar.gz". This format creates a unique backup filename with the current date.

echo "Backup process started"

This line prints a message indicating that the backup process has started to the terminal.

tar -czvf "${target_directory}/${backup_filename}" "$source_directory"

This line creates a compressed tar archive of the source directory ($source_directory) and saves it as a file with the name stored in the variable backup_filename in the target directory ($target_directory). The options -czvf specify:

  • c: Create a new archive.

  • z: Compress the archive using gzip.

  • v: Verbose mode, showing the files being processed.

  • f: Use archive file or device tarfile.

echo "Backup Complete"

This line prints a message indicating that the backup process is complete to the terminal.

Crontab

The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. Crontab stands for “cron table, ” because it uses the job scheduler cron to execute tasks; cron itself is named after “chronos, ” the Greek word for time. cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.

Linux Crontab Syntax

The Linux Crontab Format is represented by the following syntax:

MIN HOUR DOM MON DOW CMD
FieldDescriptionAllowed Value
MIN (Minute)Specifies the minute when the command will runIt ranges from 0 to 59.
HOURDenotes the hour of the day when the command is scheduled to execute.It spans from 0 to 23.
DOM (Day of Month)Specifies the day of the month for the task.It ranges from 1 to 31.
MON (Month)Indicates the month during which the command will be executed.It varies from 1 to 12.
DOW (Day of Week)Specifies the day of the week for the task.It is represented by numbers from 0 to 6, where both 0 and 6 correspond to Sunday.

Practice cron entries on the below platform.

https://crontab.guru/#_\_*


"Thank you for joining me on this journey into DevOps! Your curiosity is the driving force behind this exploration. If you found value in these insights, I'm thrilled! Keep the DevOps flame alive, and stay tuned for more exciting adventures in the world of technology. Happy Learning! 🚀✨"

"Closing the gap between us—let's connect and make our virtual bond stronger on social media!

Click on -Umesh Salunkhe