A Step Ahead in Mastering Shell Scripting: An Ally for DevOps Automation ππ

Finding Way to the Cloud.
Introduction: π
Any aspiring DevOps engineer must be proficient in shell scripting because it is a potent tool crucial to DevOps automation. This blog delves into shell scripting and examines its various characteristics, valuable uses, and significance in automating DevOps tasks. Let's move forward!
In this blog, we'll discuss shell scripting-related issues such as user administration, automation with corn and crontab, and more. Let's get going.
Tasksπ : Write a script to automate directory making.
Example 1:
So, here we are going to write a script that when the script is executed as
./script.sh mydir 1 90


NOTE: These 2 pictures are one after the other.
Now while going through the script there are various situations that are in the 2 'if' statement and 1 'if else' statement.

This 'if' statement will validate whether the user has input 3 arguments or not. If the user has passed 3 arguments then there will be exit code 1 else it will throw an error and display how to input the argument as shown in the echo command.

This 'if' statement will validate that the 1st integer must be less than the 2nd integer, else it will throw an error as shown in the echo command.

This 'if else' statement will validate whether the user has input integer or not in the 2nd and 3rd argument as required.
Here when the script is executed with correct arguments passed it will create directories accordingly. Here the script is executed as
./script.sh mydir 1 90 and it creates 90 directories as
day1 day2 day3 .... day90

Example 2:
When the script is executed as./script.sh Movie 20 50 then it creates 20 directories as Movie20 Movie21 Movie23 ...Movie50

Here are the resultsβοΈβοΈ
Tasksπ : Create a Script to back up all your work done till now.
Backups are an important part of DevOps Engineers' day to Day activities, You can create a homemade tool based on Bash scripts and it will work for you, so here we are going to look at how to take backup by just running a simple script.

First of all, in the script named "backup.sh" we have made a variable name "source_folder_path" and gave it the path of the directory of which we have to take back up. After that, we created another variable "backup_folder_path" and gave it the path of the directory where we are going to store the backup.
After that, we created a variable name "curr_time" to get time and date in a specific format so that later on we can attach it to the backup file name so that we can get info about the specific time when we have taken the backup.
The "backup_file" variable has been given the path and time stamp and putting ".tgz" as an extension makes it a TAR archive file, comprising of multiple files and folders, that has been compressed using the Gnu Zip (gzip) compression software.
"tar czf" - This command creates a tar file called $backup_file.tgz which is the archive of all "$source_folder_path" files in the script directory as shown.

Now we will execute the script "./backUp.sh"ππ»

Here the scripts execute successfullyπ€πΌ, now let us look at the output in the respective folder.

Here the backup has been completed with a time stamp in the backup folder.
π β° Cron and Crontab
Ever wished you had a personal assistant to execute tasks automatically, so you could focus on more important things? Well, Cron and Crontab are here to make your dreams come true! π
Cron job:
Cron is a task scheduler that operates on a time basis. It operates in the background, executing commands or scripts at predefined intervals. Users who set up and maintain software environments use Cron to schedule jobs and to run periodically at fixed times, days, dates, or intervals.
Crontab: The configuration file
Crontab (short for cron table) is a configuration file that allows users to specify cron job schedules. Each system user has their crontab, which is kept in a spool directory. The cron daemon runs in the background and examines these crontab files regularly to schedule and perform jobs.
Syntax of Crontab:
The crontab syntax consists of five fields (time units) followed by the command or script to be executed.

- The five fields represent:
Minute (0-59)
Hour (0-23)
Day of the month (1-31)
Month (1-12)
Day of the week (0-7, where 0 and 7 represent Sunday)
For more combination and practice ππ» https://crontab.cronhub.io/
π§ Examples of Cron Jobs:
Here are some real-world examples of Cron jobs:
Back up your files every night at midnight. π
Clean up log files every week on Sunday. ποΈ
Send weekly reports every Monday morning. π
Update system databases every month. π
Managing Crontabs:
To manage your crontabs, you can use the crontab command with various options. For example:
crontab -e: Edit your crontab.crontab -l: View your current crontab.crontab -r: Remove your crontab.
π Conclusion:
Let Cron Do the Work!
Cron and Cron Tab are your allies in automation, making your life as a DevOps engineer easier. Whether it's routine tasks or scheduled maintenance, Cron ensures everything runs on time, like clockwork. π°οΈ So, embrace the power of automation with Cron and let your system take care of business while you focus on the big picture! π
Happy Automating with Cron and Cron Tab! π



