Day Four: Shell Scripting- The #90DaysOfDevOps Challenge

Day Four: Shell Scripting- The #90DaysOfDevOps Challenge

Welcome to Day 4 of the #90DaysOfDevOps challenge! Today, we dive into the world of basic Linux shell scripting.

Link to GitHub Repo for the challenge.

For Day four of the challenge, the following tasks were to be completed:

  1. Explain what is Shell Scripting for DevOps.

  2. Explain what #!/bin/bash is and whether we can use #!/bin/sh as well.

  3. Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

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

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

  1. What is Shell Scripting for DevOps?

Shell scripting for DevOps is the creation of scripts using a shell language (such as bash) to automate tasks related to system administration, deployment, and management in a Linux environment. These scripts use the command line to execute various operations. Shell scripting is an essential skill for DevOps engineers.

  1. #!/bin/bash

The line #!/bin/bash also referred to as the shebang or hashbang at the beginning of a shell script indicates the path to the interpreter that should be used to execute the script. In this case, it specifies that the script should be interpreted by the Bash (Bourne Again shell). Similarly, #!/bin/sh specifies the default shell interpreter as the Bourne shell.

  1. A shell script to print a message

First, we write the script. In my case, I have written it into a file script.sh

#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"

Remember to give yourself permissions to execute the file as well by running the command chmod 700 script.sh.

When we run the script we get the following result:

  1. A shell script that takes user input

First, we write the script. In my case, I have reused the previous file script.sh

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name! Welcome to the #90DaysOfDevOps challenge."

When we run the script we get the following result:

  1. A shell script that takes arguments

     #!/bin/bash
     echo "The first argument is: $1"
     echo "The second argument is: $2"
    

    Run the script with two arguments like this: ./script.sharg1 arg2.

    When we run the script we get the following result:

    1. If ... Else in Shell Scripting

First, we write the script.

#!/bin/bash
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
    echo "$num1 is less than $num2"
else
    echo "$num1 is equal to $num2"
fi

When we run the script we get the following result:

This script compares two numbers (num1 and num2) and prints a message based on the comparison result.

That wraps up Day 4 of the #90DaysOfDevOps challenge! Shell scripting is a powerful tool for automation in the Linux environment, and mastering it is crucial for DevOps professionals. Stay tuned for more challenges and insights as we progress through the journey together. Happy scripting!