Day 4 - Basic Linux Shell Scripting for DevOps Engineers.

Omkar ShindeOmkar Shinde
4 min read

Explain in your own words and examples, what is Shell Scripting for DevOps.

Shell scripting for DevOps refers to the practice of using shell scripts to automate various tasks and processes in the DevOps workflow. DevOps emphasizes collaboration, communication, and automation to streamline software development and deployment. Shell scripting, using scripting languages like Bash, allows DevOps engineers to automate repetitive tasks, configure infrastructure, deploy applications, and manage environments.

For Example Let's consider a scenario where you have a web application that needs to be deployed to development, staging, and production environments. You can create a shell script that performs the following tasks:

📍Pulling the Latest Code: The script can fetch the latest code from the version control system (e.g., Git) using commands like git pull.
📍Building the Application: It can execute build commands specific to your application, such as compiling code, running tests, and generating necessary artifacts.
📍Configuring Environment-specific Settings: The script can modify configuration files or environment variables based on the target environment. For example, it can switch database connections or API endpoints.
📍Packaging and Deploying the Application: The script can create deployment packages (e.g., Docker images, ZIP files) and deploy them to the appropriate environment using deployment tools like Ansible, Kubernetes, or simple SSH commands.
📍Running Post-Deployment Tasks: After the deployment, the script can trigger additional tasks, such as running database migrations, clearing caches, or restarting services.

What is #!/bin/bash? can we write #!/bin/sh as well?

  • This is known as shebang in Unix. Shebang is a collection of characters or letters that consist of a number sign and exclamation mark, that is (#!) at the beginning of a script. The main function of a shell is to interpret the UNIX commands given by the user. Unix consists of numerous shells out of that bash is one of them that is widely used. It is the default shell assigned by Linux-based operating systems.

    #!/bin/bash
    A script comprises several UNIX commands. Now, users are required to explicitly specify the type of shell they want to use to run their scripts. Now to explicitly specify the type of shell used by the script, Shebang is used. So we can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell.

    A script is nothing but several commands. In the below example, the first line of the script starts with a hash (#) and an exclamation mark (!). It instructs the operating system to use Bash as a default shell to run the script. In the second line, we are printing “Hello DevOpsCommunity!” to the console.

    #!/bin/sh
    The shebang line #!/bin/sh tells the interpreter to use the shell script while executing a code. In this case, /bin/sh refers to the Bourne shell, which is a simple and lightweight shell that is available on most Unix-based systems, including Linux.

    The #!/bin/sh and #!/bin/bash are both Unix shell scripts but offer different purposes that make them unique. Bash includes many advanced features that are lacking in Bourne shell, such as advanced history management, tab completion, and process substitution. On the other hand, the Bourne shell is faster and more efficient than Bash since it has a smaller code base and fewer features to manage.

  • Write a Shell Script that prints I will complete the #90DaysOofDevOps challenge.

    First of all, create a file using the nano editor as follows:

        nano Day4script.sh
    

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

        nano DevopsUserInput.sh
    

    From the above shell script user can input their name after reading the "name" variable. it prints the value stored in the variable, informing the user about their name.

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

        nano CompNum.sh
    

    For conditional Expressions [[ ]] syntax is used.

    "-gt" is used to compare if the 1st number is greater than the 2nd number.
    fi allows Shell to make decisions and execute statements conditionally.

In conclusion, shell scripting is a powerful tool for automating tasks and processes in the DevOps workflow. With the ability to write scripts using scripting languages like Bash, DevOps engineers can streamline and simplify complex operations, such as configuration management, deployment, and monitoring.

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn & Github.

Linkedin

Github

Hashnode

0
Subscribe to my newsletter

Read articles from Omkar Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Omkar Shinde
Omkar Shinde