Day 4 - Basic Linux Shell Scripting for DevOps Engineers

Day 4 - Basic Linux Shell Scripting for DevOps Engineers

·

6 min read

Introduction

In this blog, we will come across Shell and Shell Scripting. We'll be understanding some basic components of Shell Scripting like Shebang with some hands on practice at the end. We'll also understand the steps involved in writing a perfect Shell Script. So without anymore delay, let's jump into it.

What is Shell?

A Shell is a special user program which provide an interface to user to use the application. A Shell accepts input in the human-readable format and then converts it into something that the Kernel understands. With the help of Shell, we can run commands, perform tasks and much more. It is a Command Language Interpreter that accepts input from devices like keyboard or from files.

NOTE: The Shell starts as soon as we log in to the terminal.

What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. It is used to automate tasks. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. The script has the extension of .sh for example: myscript.sh

Types of Shell Script:

There are several shells are available for Linux systems like

  • BASH (Bourne Again SHell) – It is the most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.

  • CSH (C SHell) – The C shell’s syntax and its usage are very similar to the C programming language.

Some Components of Shell Script:

  • #!bin/bash : This is called a shebang, and it's a special line at the beginning of a script that tells the system which interpreter should be used to execute the script. In this case, it specifies Bash as the interpreter.

  • #!/bin/sh: This shebang indicates that the script should be interpreted using the default system shell. It might be Bash on some systems, but on others, it could be a different shell like Dash or Bourne shell.

      #!/bin/sh
      echo "This script is using the default system shell."
    

Note: To Check the Shell used by your system, use which bash command

which bash

Steps for writing a Shell Script:

Step 1:

  • Use any editor of your choice like vim and create a file with .sh extension.

Step 2:

  • After writing the script execute permission of your scripts as follows.

    chmod +x <script_name>

    chmod 755 <script_name>

Step 3:

  • Execute your shell script as follows:

    sh <script_name>

    ./<script_name>

Practice with a Scenario:

Imagine you are embarking on the #90DaysOfDevOps challenge, and you decide to create a dynamic Shell Script to document your progress. As you work on your script, you want it to interactively engage with users. Here's a scenario-based interactive question for you:

Scenario:

You've just completed the first week of your #90DaysOfDevOps challenge, and you're excited to automate a Shell Script to share your progress with fellow developers. You decide to include functionalities that allow users to input their own achievements, showcase the power of variable handling, and demonstrate basic conditional logic.

Interactive Question:

Imagine you are writing a Shell Script to showcase your commitment to the #90DaysOfDevOps challenge. The script will have three main features:

  1. Print a motivational message: "I will complete #90DaysOfDevOps challenge."

  2. Take user input: Allow users to share their own achievements in the challenge.

  3. Variable demonstration: Accept inputs from both user arguments and direct user input, then print and compare the variables.

Can you create an interactive Shell Script that accomplishes these tasks? Include examples of user interactions, variable handling, and an if-else statement to compare two numbers within the script.

Solution:

  1. Printing the message

     echo "I will complete #90DaysOfDevOps challenge."
    
  2. Taking User Input:

    To take the user input in a shell script, "read" followed by the name to store the input. in our case we have named it as "userInput".

     read userInput
    
  3. Accepting input from an argument:

    In shell scripting, you can accept input from the user by using arguments. Arguments are values that you provide to a script when you run it. Inside your script, you can access the arguments using special variables:

    • $0: The script's name.

    • $1, $2, ...: The first, second, etc., arguments provided.

For example:

    #!/bin/bash

    echo "Script name: $0"
    echo "First argument: $1"

When you run the script from the command line, you can provide arguments after the script's name:

For example, running ./myscript.sh value1 value2 would make $1 hold "value1" and $2 hold "value2".

Complete Script for the Scenario question:

#!/bin/bash

# Feature 1: Print motivational message
echo "Motivational Message: I will complete #90DaysOfDevOps challenge."

# Feature 2: Take user input
read -p "Share your achievement in the challenge: " userAchievement

# Feature 3: Variable demonstration
# Accept input from user arguments
arg1=$1
arg2=$2

# Display user input and arguments
echo "Your Achievement: $userAchievement"
echo "Arguments provided: Argument 1 - $arg1, Argument 2 - $arg2"

# If-else statement to compare two numbers
if [ "$arg1" -eq "$arg2" ]; then
    echo "The two numbers are equal."
else
    echo "The two numbers are not equal."
fi

Explanation:

  • The script starts by printing a motivational message using echo.

  • It then prompts the user to share their achievement in the challenge and stores the input in the userAchievement variable using read.

  • -p stands for taking paragraph as an input.

  • The script accepts two arguments from the user and assigns them to variables arg1 and arg2.

  • It then prints the user's achievement and the values of the arguments.

  • Finally, an if-else statement compares the two numbers provided as arguments and prints whether they are equal or not.

  • -eq : is for comparing both values.

To run the script:

chmod +x my_script.sh  # Make the script executable
./your_script.sh "5" "5"  # Example usage with arguments

Note: arg1 stores the value of first argument i.e. 5 and arg2 stores the value of second argument i.e. 5 again.

Conclusion:

In summary, this blog introduced the basics of Shell and Shell Scripting, covering essential components, types of scripts, and key steps for script creation. The practical scenario demonstrated the creation of an interactive Shell Script for the #90DaysOfDevOps challenge, incorporating motivational messages, user input, and variable handling. You'll gain insights into the fundamentals of Shell Scripting and acquired a practical example for creating engaging and dynamic scripts.

Ending this with quote

Embrace the penguin mindset: resilient, adaptable, and always moving forward in the face of challenges.

Happy learning.

Did you find this article valuable?

Support DevOps with Aakash by becoming a sponsor. Any amount is appreciated!