DEV Community

Cover image for Automation with Bash scripts
UWABOR KING COLLINS
UWABOR KING COLLINS

Posted on

Automation with Bash scripts

During my #120DaysOfDevOps journey, Day 7 was dedicated to mastering the creation of a simple shell script and understanding why it stands as the ultimate solution for automating tasks. Shell scripts have proven to be a game-changer in the world of automation, enabling developers and system administrators to streamline processes effectively.

On Day 7, I delved into the fundamentals of crafting a shell script and emphasised its unparalleled advantages for task automation. With shell scripts, I discovered a powerful tool that empowers me to execute a series of commands in a seamless, automated manner, reducing the need for manual intervention and thereby minimising errors.

The true strength of shell scripts lies in their cross-platform compatibility and flexibility. By utilising popular scripting languages like Bash or Python, I can create scripts that can run on various operating systems, making them adaptable to diverse environments and setups.

Why Automate?

Have you ever found yourself immersed in a lengthy and intricate task at the command line, only to breathe a sigh of relief when it's finally completed, thinking, "Phew! I'm glad that's over. I won't have to deal with that again!"? I can relate to that sentiment all too well, and I soon came to realise a fundamental truth: nearly everything I ever do on a computer, whether it's for personal use, professional projects, or consulting engagements, is bound to resurface in the future.

The realisation struck me with increasing frequency, driving home the importance of automation and the role it plays in alleviating the burden of repetitive tasks. What I once believed to be a one-time ordeal soon transformed into an opportunity to streamline and optimise my workflow for future endeavours.

Whether it's managing configurations, deploying software, or orchestrating complex operations, the likelihood of encountering similar scenarios down the line is significant. Embracing automation became the logical solution to ensure efficiency, reduce errors, and free up valuable time and mental energy.

By automating tasks through scripting languages like Bash, Python, or Power Shell, I discovered the power to encapsulate the knowledge and steps required to perform a task, enabling me to replicate and execute them effortlessly when needed. The ability to create reusable scripts and leverage them as building blocks for future automation endeavours has been a game-changer.

Now, instead of dreading the inevitable recurrence of time-consuming and intricate tasks, I approach them with a newfound confidence. I recognise that automation serves as a reliable ally, empowering me to conquer challenges with ease and efficiency, while freeing up mental space to focus on higher-value initiatives.

In conclusion, the recognition that almost everything I do on a computer is bound to resurface in the future has been a catalyst for embracing automation as a fundamental principle in my workflow. The ability to automate tasks not only saves time and minimises errors but also grants me the freedom to tackle new challenges with confidence and alleviate the burden of recurring endeavours.

At the beginning of my journey, I found myself resorting to writing down the steps required for various tasks on pieces of paper, thinking it would help me remember and replicate them in the future. However, I soon realised the impracticality of this approach and sought a more efficient solution. I turned to a simple notepad application on my computer, believing it would provide a more organised and accessible way to store the information. Yet, once again, I had a moment of revelation.

It occurred to me that if I was already storing this valuable data on my computer, there was a smarter way to leverage it. Why not take it a step further and transform those scribbles into a comprehensive shell script? By creating a script and storing it in a standardised location like /usr/local/bin or ~/bin, I could elevate my automation game to a whole new level.

The realisation struck me: instead of manually executing those tasks step by step, I could simply invoke the shell script by typing its name and let it handle the rest. This epiphany was a game-changer, saving me time and effort while ensuring consistency and accuracy in task execution.

By utilising the power of shell scripting, I was able to transform my once fragmented and scattered notes into a centralised, executable solution. No longer did I need to sift through piles of paper or search for scattered digital files. With a simple command, I could initiate the script and watch as it effortlessly carried out the tasks I used to perform manually.

The beauty of this approach lies not only in its convenience but also in its scalability. As I continued to encounter new tasks and requirements, I could expand my repertoire of shell scripts, creating a library of reusable automation tools that could be easily accessed and invoked whenever needed.

Shell script

Writing shell programs, or scripts, is a highly effective way to optimise time utilisation. Once a shell program is created, it can be rerun as needed, allowing for efficient task repetition. Additionally, shell scripts can be updated to adapt to changes in Linux releases, hardware and software installations, evolving objectives, and maintenance requirements, including bug fixes. Automation is crucial, and every task executed via the terminal should be automated whenever possible, saving time in the long run. Bash scripts can range from a few commands to thousands, offering great flexibility in creating comprehensive automation solutions.

Hello World

Here's a simple shell script example and the steps to create it. I relied on the classic "Hello world" example found in countless programming books. When executed from the command line, it appears as follows:

Image description
A program or shell script is essentially a set of instructions for the computer to follow. However, manually entering these instructions on the command line can be tiresome, particularly for lengthy and intricate programs. To save time and minimise errors, it's preferable to store the instructions in a file that can be executed with a single command.

The first task is to create a file to contain your program. Use the touch command to create the empty file, hello, then make it executable:

Image description

Now, use your favourite editor to add the following line to the file:

Image description

Save the file and run it from the command line. You can use a separate shell session to execute the scripts:

Image description

Shebang

The single statement functions well when employing Bash or a shell compatible with the script's commands. If no specific shell is indicated in the script, the default shell will be employed to execute the commands.

The subsequent step is to ensure the script runs using the Bash shell, regardless of the default shell. This is accomplished by adding a shebang line as the first line of the script. The shebang line, denoted by the #! characters, explicitly designates the shell to be used for executing the script. In this case, it specifies Bash, but it could be any other shell. It's important to note that if the specified shell is not installed, the script will not run.

By adding the shebang line, the script now appears as follows:

Image description
Run the script again you should see no difference in the result. If you have other shells installed (such as ksh, csh, tcsh, zsh, etc.), start one and run the script again.

Scripts vs. compiled programs

Shell scripts are invaluable tools for sysadmins when it comes to automating various tasks. Being stored in ASCII text format, they can be easily understood and modified by both humans and computers. This openness allows for examination of the script's logic, syntax, and potential errors, enabling efficient troubleshooting.

This my post didn't get very far with creating a shell script, but it did create a very small one called Hello World. In the upcoming updates of my journey, we would talk about how to create a Bash script template serving as a foundation for other scripts. This template will include a variables, several basic functions, and logic to handle various options required for scripts to run.

Resources

Thanks for reading see you in my next Journey!

Top comments (1)

Collapse
 
kennethstack profile image
Kenneth Mahon

loving this alot