Shell Scripting for DevOps

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.

Article content
Shebang Line

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).

Article content
Comment

3. Variable Declaration 🔤:

Declare variables that will be used in the script. Variables are used to store and manipulate data.

Article content
Variable Declaration

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.

Article content
Main Body of Script
Article content
Complete Script


Lets see few more examples of shell scripting.

  • Write a Shell Script which prints I will complete #90Days0OfDevOps challenge

Create the file with .sh extension. Open the editor like vim and echo what you need to print

Article content
Shell Script

To run the script either use bash filename or ./filename command.

Article content
Output

  • Write a Shell Script to take user input, input from arguments and print the variables:


Article content
Shell Scripting
Article content
Output

  • Write an Example of If else in Shell Scripting by comparing 2 number

Article content
Shell Script
Article content
Output


To view or add a comment, sign in

More articles by Komal Mote

  • Essential Linux Commands for File Management

    Linux is a powerful operating system that provides a wide range of commands for managing files and directories. Whether…

  • Linux for DevOps

    DevOps Architecture: 1) Build: DevOps introduces the utilization of cloud resources and resource sharing, making the…

    1 Comment

Others also viewed

Explore content categories