Leveraging Examples to Harness the Limitless Potential of Linux Day-4 #90DaysofDevOps
Table of contents
The commands continue...
Whatever stage of your Linux experience, learning shell scripting can significantly increase your productivity and efficiency. With the help of shell scripts, you may automate a variety of chores and optimize your workflow because they are compact yet mighty programs written in the shell language that carry out a series of commands in sequential order. To help you understand the principles and put them to use in actual situations, we'll walk you through the fundamentals of shell scripting in this blog.
We can set aliases for the commands to make things simpler while you run the code. Let's see a conversation between Ryan and Sharon (our imaginary colleagues from the IT teams):
So while Sharon wanted to talk to Ryan and understand how the alias command works, Ryan quickly spun up his VM on the AWS machine with the Linux OS and then:
and then...
Understanding Shell Scripting
To understand how shell scripting works, I have created a simple file called "greeter" with an extension ".sh" specifying that it is a shell script
Let's see the permissions for this file and also make sure that we have enough permissions to execute this script.
The ls -al command clearly shows that there is no executable permission for the script that we just created. Let's change the same with the chmod command.
Boom!!! The file now has executable permission... let's see how it behaves when we execute it from the terminal:
Good enough!! That was the first achievement in shell scripting... We now wrote a simple script that displayed us "Hello, World!". Wasn't that pretty simple? Let's now see if we can make it a bit more interesting and instead of just displaying just a "Hello, World!" message if it can check and greet a user.
You may ask me where is step 2 in the screenshot... Simple that is the creation of the script using the vim editor... Here is a snip of the script:
Now, the script was working fine when I said the name "Sandeep" (matching case). Let's quickly attempt something else and see what may be returned as the output.
Conclusion
Let's turn to Ryan (remember? our IT guy on the top of this page) and see what has got to say.
While I have tried my best to show you what can be the usage of shell scripting, how it is written and what can it do... there are endless possibilities and endless combinations of commands. Get creative and start exploring the possibilities. Happy learning!!