Advanced Shell Scripting: Pushing the Limits📈

High functional, goal-oriented, and result driven Analyst with 2+ years of progressive experience. contributing to organizational success through high-level strategy and the implementation of excellence. Strong track record of inspiring effective cross-functional collaboration and innovative problem solving skills.
Passionate about exploring the ever-evolving world of DevOps technology. On a journey to unlock the secrets of seamless development, continuous integration, and deployment.
Committed to continuous improvement and lifelong learning.
✍️Prerequisites:
To get the most out of this blog, a foundational grasp of shell scripting and its mechanics is beneficial. If you're new to shell scripting, you can find a concise introduction in one of my previous blog posts; feel free to explore it here:
https://alkama.hashnode.dev/shell-scripting-fundamentals
🧮Shell scripting: Level Up
📍Looping Statements:
Just like any other programming language Shell scripting also supports the concept of looping statements, which allows the user to execute the set of instructions for certain no. of times.
Certainly, there are three main types of loop statements in shell scripting:
1. while loop: The while loop evaluates a condition and repeatedly executes the statements within the loop as long as the condition remains true. It's commonly used when the exact number of iterations is unknown.

The above code will print the number from 1 to 10, each no. in new line.
2. for loop: A for loop is handy when you need to execute a specific number of iterations. It's recommended to use the FOR loops when you are already aware of the no. of times you want your loop to execute. For eg: you are aware that you want your FOR loop to execute only 10 times to print the number.

"done" is a keyword used at the end of the FOR loop to know or help the shell comprehend the structure and control the flow of the loop by defining the end-of-loop structures. It is used to make sure that the script continues with the right instructions after the loop and that the loop functions as planned.
3. until loop: The until loop is similar to the while loop but works the other way around. It executes the loop body until the specified condition becomes true.

📍Sequence Expression:
By specifying the range's beginning and ending points, the Bash sequence expression creates a range of integers or characters. Typically, "for" loops are utilised in conjunction with it. The sequence expression syntax is: {START..END[..INCREMENT]}
To aid in your comprehension of the idea, consider the following examples:


📍seq command:
In Linux, the seq command is used to create numbers from FIRST to LAST in INCREMENT steps. It is a highly helpful command for generating a list of numbers in a while, for, and until loop. The syntax for the seq command seq [option] is as follows: seq [OPTION]... LAST or seq [option] FIRST INCREMENT LAST
Specifying an Increment: You can also set an increment value to control the step between numbers. For instance, to generate even numbers from 2 to 10:
Let's look at an instance to get a better idea of how this works:

📍Functions in scripting:
Bash scripting facilitates the idea of function, supporting features like code reuse, code maintenance, and repeated executions of the same code.
Bash function:
The syntax to define or to write the function is to use the keyword Function or the (). function name_of the_function() {....}
for eg:

you just need to write the name of the function to call it. If i write my_first_function in the bash script, the code written in echo will be printed as an output.
📍Cron:
A cron is a Linux command line utility that executes the program at regular intervals. It wakes up every minute and looks at the control file which is known as the crontab file which can be found at /var/spool/cron/crontabs for instructions to be performed at that instance of time. After executing them successfully it goes back to sleep only to wake up the next minute. Also known as cron jobs.
The other way to define cron is a time-based job scheduler which allows the user to execute the set of instructions at a particular time of interval.
You must be wondering how can we use the cron in DevOps.
Cron supports one of the fundamental building blocks of the DevOps methodology i.e. to automate. we can schedule the script to take a backup, do the software update at dawn, send a reminder mail, and whatnot at a particular time. These scheduled tasks are often referred to as "cron jobs."
📍crontab in Linux:
Under Linux, the crontab command is used to create, view, edit, and manage scheduled tasks, often known as "cron jobs." Users can create and manage their own cron job schedules with the crontab command as below:
crontab cron.txt
Just like any other Linux command cron also supports options, such as:
crontab -l: To see crontab entries (cron jobs) and see the contents of the system crontab file, use the crontab -l command.
crontab -r: The current crontab file will be deleted with crontab -r.
crontab -i: A prompt will appear before a user's crontab is removed when using crontab -i. It is strongly advised to use it in conjunction with the -r flag to create the -ri flag.
crontab -e: To edit system crontabs, use crontab -e. If a crontab hasn't been made yet, this command will make one.
A crontab file has 5 fields as shown below:

What can be the possible wasys to give input for each 5 field. Those are describes as below:
Field | Possible input values |
| Minute | 0-59 |
| Hour | 0-23 |
| Day of month | Jan-31 |
| Month | 01-Dec |
| Day of week | 0-6. 0 depicts Sunday. In some systems, a value of 7 represents Sunday instead. |
| Command | Command to execute. |
crontab also supports the special character feature. Some of the characters and theri meaning are:
Symbol | Meaning | Example |
| \ (asterisk)* | Select all possible values in a field | Put an * in the hour field to have the job run every hour. |
| , (comma) | A comma is used to separate multiple values | When you enter 0,3,5 in the day of week field, the task will run on Sunday, Wednesday and Friday. |
| – (hyphen) | Used to set a range of values | 1-10 in the day of month field will run the task from the January through October. |
| / (separator) | A separator is used to divide values | */10 in the hour field will make the task run every 10 hours. |
For eg, if you want the cron job to run every 5 minute, the expression will be:

you can explore more about the cron expression 👉 https://crontab.guru/
The system administrator mostly uses cron to gather information on system performance and carry out maintenance tasks like deleting old files as a housekeeping task.
💡Conclusion:
Learning about function, sequence expressions..etc will help you to evalutate and level up your scripting game. And supporting the methodology of DevOps i.e. to automate.



