Exploring Advanced Bash Scripting Concepts

Exploring Advanced Bash Scripting Concepts


📍Introduction

👋Hello guys! Welcome back to the exciting journey of Bash scripting! 🔥 This blog continues the comprehensive Bash scripting for DevOps series, exploring advanced concepts and taking your skills to the next level!💪🚀

Before we dive in, ensure you are familiar with basic Linux commands. If you need a refresher, check out my two-part blog series on Mastering Basic Linux Commands. Also, make sure you understand the basics of bash scripting, covered in my previous blog Bash Scripting Basics: Your Gateway to Automation.

Throughout this series, we'll progress from the fundamentals of Shell scripting to crafting essential advanced scripts for DevOps tasks. Get ready to unlock the full power of Shell scripting! Let's begin the exciting journey!🚀


📍If-elif

In the Bash Scripting Basics: Your Gateway to Automation blog we learned how to write if-else statements in the script. Which was helpful when we had a condition.

But, what if, we have multiple conditions? For that we use If-elif, the syntax is similar to an if-else statement.

Let's see the syntax of an if-elif statement using an example: Find the greatest number from 3 numbers.

📝Few things to note here:📝

  1. While dealing with multiple conditions we have to use some operators like && (and), == (checks are equal or not)-gt (greater than), etc.

  2. While using operators we can use syntax [[ expression1 <operator> expression2]] or [expression1] <operator> [expression2].

  3. I have used arguments as my variable instead of defining a variable inside so that I can compare any 3 numbers I wish by passing them as arguments.

Now, we execute the script using the command #./<filename.sh> after changing the file permissions. (Already covered in my Bash Scripting Basics blog)


📍Loops🔁🔄

If you want to do a single thing multiple times in a script then we can use loops. There are two ways in which you can write loops in bash:

  1. For-loops

  2. While-loops

✔For-loops

If you want to do a single thing multiple times in a script then we can use loops. Let's see the loop statement syntax with a simple loop script:

📝 A few things to note while writing loops in bh:📝

  • For loops to run it needs 3 things ( initialization; condition; iteration )

  • In the above image, we created a variable i and initialized it with 0.

  • Then we specify a condition i<10 telling when to stop this loop.

  • Then we specify by how much the i will be incremented, here, i++ means after every iteration of the loop i will be incremented by 1. Similarly, if we do i+2 then it will be incremented by 2, and so on.

Then we will execute the script.

As you can see the i variable starts from 0 and gets printed and incremented after every interaction. After printing 9 i.e. i=9 the value of i get incremented to 10 which fails the condition i<10. Therefore, the loop ends its execution.

Let's take a more advanced example:

  • Iterating files using loops: Create 10 files and then iterate all 10 files.

Step 1: Create 10 files using the command #touch {1..10}.txt.

Step 2: Write a script to iterate all the text files using for loops.

📝Few things to note here:📝

  • Here we follow another type of for-loop syntax where FILE in *.txt means, all files in this folder with an extension '.txt' print that file. (*) means all. We save and quit.

Now we execute the script:

The loop ends after iterating all the files inside the folder (where this script resides).

✔While Loops

It executes the same way the for-loop does which was discussed in my Bash Scripting Basics blog. The syntax is different, it is something like:

while [ condition ];
do
    # statements
    # commands
done

If the condition is true then the commands inside the while block are executed and are iterated again after checking the condition. Also if the condition is false the statements inside the while block are skipped and the statements after the while block are executed.

Let's convert the for-loop example from the Bash Scripting Basics blog into a while-loop. In the while-loop the script will be something like this:

In the above image, we created a variable i initialized it as 1, then in the while loop we put a condition i (less than)-lt 10, and then incremented i by doing ((i++)). This is the syntax to increment the variable.

Then we execute the script.


📍Functions

Function syntax in bash is:

#!/bin/bash
#It is a function
myFunction () {
echo Hello World from GeeksforGeeks
}

#function call
myFunction

The syntax is somewhat similar to making functions in other programming languages.

Let's understand functions by making a function that adds users to the system.

But, this won't work if we simply execute the file even after giving all the permissions. Because adding a user is something only the root user can do.

Therefore, for normal users to execute the script we have to use #sudo.

After, executing the script we can check if the user has been added or not, we can ls the /home/ directory or do see the passwd file using #cat /etc/passwd.

As we can see in the above image, the last line where varun user was added after executing the script.


📍Actual Script work for DevOps

DevOps engineers craft highly complex scripts for various tasks like user management and more. We have seen a shell script for adding a user in the previous section. Let's take another example: Creating a function taking a backup of a directory.

We will explore a detailed backup and archiving process for files and directories in the Advanced Linux blog.

But for now, let's take an overview of it here.

To take a backup of files or a directory, we just need to compress it and store it in a backup directory/folder.

Step 1

Compress the folder which you need to backup using the command:

#tar czf <backup_filename>.tar.gz <backup_file/directoryname>: This will compress the <backup_file/directoryname> file with Gunzip compression with the filename as mentioned <backup_filename>.tar.gz This .tgz extension tells it is a Gunzip file.

The tar command options are czf which means compress, zip, and file- points to the file I'd like to compress.

Step 2: Create a directory to store backup files.

#mkdir backups: Will create a directory named backups.

Step 3

Then move the compressed file to the backup directory using the command #mv.

And your backup is done.🥳🎉

Now, we will automate this backup process by writing these commands in a bash script.

Therefore, create a backup.sh script, open it in the Vim editor, write the commands, and then save & quit.

src_dir means source directory.

tgt_dir means target directory.

Here, to directly take the backup of the scripts directory I used the command in this format:

#tar czf <targetfilepath.tgz> <sourcefilepath>(which needed to back up).

Then execute the bash script:

Note: More about the tar command will be explored in detail in my upcoming Advanced Linux commands blog, stay tuned for that!😉


📍Cron Job

❓Q) What is Cron❓

🔧DevOps engineers use the Cron utility to automate and schedule repetitive tasks that they perform daily. Instead of manually executing these tasks every day, the engineer sets up Cron to run them automatically at specific times and intervals. This way, the tasks run in the background, ensuring efficiency and accuracy in their execution. 👨‍💻Cron serves as a crucial daily utility for automating and scheduling these tasks, enabling DevOps engineers to focus on more critical aspects of their work.🚀

❓Q) What is Crontab❓

📅 Crontab is a crucial file for scheduling and executing tasks in the background. ✨ To set up a scheduled task, you create a Crontab file containing a series of commands that dictate the timing and frequency of execution. 💻 This file allows you to specify a bunch of commands that determine when your tasks will run, how often they'll run, and at what specific times ⏰, among other things. 🕒🔧 It's a handy way to keep your tasks running smoothly! 💪🚀

Cron is a service, utility, or practice, the actual work is done using Crontab. This Crontab file is located in the /etc/ directory, you can see this crontab by using the #cat command.

#cat /etc/crontab: This will display the crontab file on the screen and it contains the definition of crontab. It tells us the format to follow while creating a crontab file.

📝5 things needed to be considered while writing in crontab. The given format is:

Minute Hour Day-of-Month Month Day-of-week #command

Let's understand with a cronjob example how to write a cronjob in a crontab following the above format:

* * 1,5 * sun echo "Hello": Here, (*) means all (every day, every week, and so on). There is only a single space after every word.

So, the above cronjob means it will print "Hello" at:

every minute every hour on the 1st and 5th day of every month Sunday.

💡There are 3 commands related to crontab which you always have to remember.📝

  • #crontab -l: Displays a list of currently active crontabs for this user.

  • #crontab -e: This -e means to edit, this command will create and open a crontab in an editor to let you write your cronjobs.

    Note: While creating your first crontab it will ask you which editor to use. It is up to you to use Vim, vi, or Nano in whichever editor you are most comfortable with.

After that, your crontab will be opened and it will look something like this:

  • #cat /etc/crontab: This will display the crontab file on the screen and it contains the definition of crontab. It tells us the format to follow while creating a crontab file. Same as we have seen previously.

Now, you can write your first cron job.

In the above image, I am redirecting the output of the command to a file by specifying the absolute(complete) path of that file. If the file doesn't exist, the cron job will automatically create a new file and redirect the output into it. 🔄📂🔗

As you can see the crontab was empty therefore, it was creating a new crontab, we can also see the time is 11:51 (hrs: minutes) and our cronjob will be executed at 11:54 since we scheduled it for that time.

Congratulations!!🥳 🎉You just created your first cron job.

"Stay tuned for a more advanced cronjob, with the utilization of advanced Linux commands which will soon be uploaded in this blog section. 🚀I will cover these commands in detail in my upcoming blogs, and they will be uploaded shortly.📝 Don't worry; you won't want to miss it!"😉


📍Conclusion

Thank you for reading this blog! 📖 We've reached the end of the Bash Scripting series, but there's much more to come as I gain more experience and learn more in my DevOps journey. 🤩Stay tuned for exciting advanced Linux commands in my next blog. 🚀 Meanwhile, keep practicing and unleashing your creativity with these scripts. 💻🔧 More valuable content is on its way!

If you enjoyed this blog and found it helpful, please give it a like 👍, share it with your friends, do share your thoughts, and give me some valuable feedback.😇 Don't forget to follow me for more such blogs! 🌟


📍References

This blog's Topics and scripts are referred from the following 2 videos: