Shell Scripting for DevOps
As we already know DevOps is a term combining "development" and "operations," is a set of practices that aim to automate and streamline the processes of software development and IT operations. At the heart of DevOps lies the concept of automation, and one powerful tool in the DevOps toolkit is shell scripting.
Before we dive into scripting lets know what exactly the shell is and how does it work?🚀
🐚Shell is basically the bridge between the 🖥️computer’s operating system i.e kernel and the users that empowers the users 🧑💻 to performs the tasks like running programs, managing files, automating the processes etc. It acts as a mediator that translates human readable commands into the instructions that system language that kernel can execute.
And if we talk about shell scripting 📄, it involves writing various commands and instructions in a script file (.sh file) that can be executed.
Shell Scripting Structure 📜:
Shell scripts follow a simple structure that typically includes comments, the shebang line, variable declarations, and the main body of the script. Here's a basic outline of a shell script structure:
1. Shebang Line ❗️:
In Linux ‘#!’ is known as ‘shebang’ its is used to specify which interpreter is used to execute the script
#!/bin/bash is a shebang line that specifies the path to the Bash shell interpreter. It tells the system to use bash shell to interpret and execute the script while #!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts.
2. Comments #️⃣:
Comments are lines in the script that are not executed but provide information about the script. They are preceded by the # character. Comments are essential for documenting your code and making it more understandable for others (or yourself in the future).
3. Variable Declaration 🔤:
Declare variables that will be used in the script. Variables are used to store and manipulate data.
4. Main Body 📝:
This section contains the main logic of your script. It consists of commands and operations that will be executed when the script is run. Here's where you perform tasks such as conditionals, loops, and calling other scripts or commands.
Recommended by LinkedIn
Lets see few more examples of shell scripting.
Create the file with .sh extension. Open the editor like vim and echo what you need to print
To run the script either use bash filename or ./filename command.