Day 4 #90DaysOfDevOpsChallenge : Basic Linux Shell Scripting for DevOps Engineers

ASHISH PATILASHISH PATIL
4 min read

Today’s focus is on grasping the essentials of Linux shell scripting, an invaluable skill for anyone pursuing a career in DevOps. Let's explore some fundamental concepts and engage in hands-on tasks that will aid in automating processes and managing Linux systems more effectively.

What is a Kernel?

The kernel is the central component of an operating system. It manages all interactions between hardware and software, ensuring efficient resource allocation, like CPU time, memory, and input/output operations. Think of it as the brain of the system that coordinates all activities, enabling applications to function smoothly and securely.

What is a Shell?

A shell acts as the intermediary between the user and the operating system. It takes user commands, interprets them, and forwards them to the kernel for execution. The shell can also run scripts stored in files, allowing users to automate tasks. Common shells in Linux include bash (Bourne Again Shell) and sh (Bourne Shell), with bash offering more advanced features.

What is Linux Shell Scripting?

Linux shell scripting involves crafting a sequence of commands in a script file that the shell can execute. This practice is particularly useful for automating repetitive tasks, streamlining system administration, and configuring server environments within a DevOps context. For example, shell scripts can be utilized for tasks such as installing software, updating system configurations, or gathering system metrics.

Tasks

  1. What Does Shell Scripting Mean for DevOps?
    In the DevOps landscape, shell scripting is a pivotal asset for automating various operational processes. By automating routine tasks, DevOps professionals can redirect their focus toward more strategic initiatives. Examples of automation through shell scripting include:

    • Simplifying the application deployment process.

    • Facilitating routine backups of critical data.

    • Monitoring system performance logs for anomalies.

Given that Linux dominates cloud and server environments, scripting has become an essential skill for managing and configuring infrastructure efficiently.

  1. What is #!/bin/bash? Can We Use #!/bin/sh?
    The line #!/bin/bash is known as a shebang. It indicates which interpreter should be used to run the script. In this instance, /bin/bash specifies the use of the Bash shell. Conversely, if you write #!/bin/sh, the script will utilize the sh shell, which is a simpler version with limited functionality. While both can be employed, it’s advisable to use #!/bin/bash for scripts requiring advanced Bash features.

  2. How to Write a Simple Shell Script?
    Below is an example of a simple shell script that outputs a motivational message related to our DevOps challenge:

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

    Output:

     I will complete the #90DaysOfDevOps challenge.
    

    To execute this script, save it as challenge.sh, and run the following commands:

     chmod +x challenge.sh
     ./challenge.sh
    
  3. Shell Script with User Input and Arguments
    The following script demonstrates how to accept user input and command-line arguments:

     #!/bin/bash
     # Requesting user input
     echo "Please enter your name:"
     read name
     echo "Welcome, $name!"
     # Accessing command-line arguments
     echo "The first argument you provided is: $1"
     echo "The second argument you provided is: $2"
    

    To run this script, save it as input_script.sh, and execute it as follows:

      chmod +x input_script.sh
     ./input_script.sh DevOps Linux
    

    Sample Output:

     Please enter your name:
     ASHISH
     Welcome, ASHISH!
     The first argument you provided is: DevOps
     The second argument you provided is: Linux
    
  4. Example of an If-Else Statement in Shell Scripting
    Below is a straightforward example demonstrating how to compare two numbers:

     #!/bin/bash
     num1=10
     num2=20
     if [ $num1 -gt $num2 ]; then
         echo "$num1 is greater than $num2"
     else
         echo "$num1 is less than or equal to $num2"
     fi
    

    Output:

     10 is less than or equal to 20
    

    To run this script, save it as compare.sh and execute it:

     chmod +x compare.sh
     ./compare.sh
    

Were the Tasks Challenging?

Today's activities served as an excellent introduction to the foundational concepts of Linux shell scripting. The ideas were clearly articulated, and it was exciting to realize the potential of scripting in automating tasks within a DevOps environment. Writing scripts that handle user input and command-line arguments was particularly engaging. Overall, these practical tasks greatly reinforced my grasp of Linux shell scripting basics.

That wraps up Day 4! Be sure to share your insights with the DevOps community. Mastering shell scripting is a vital skill that will enhance your efficiency and automation capabilities throughout your career. Looking forward to more DevOps exploration tomorrow!

Let’s learn together and grow together in the world of DevOps! 🌱🚀 #DevOpsJourney #LearningTogether #GrowthMindset #TechCommunity #TrainWithShubham #90DaysOfDevOps

Follow my #90DaysOfDevOps challenge on LinkedIn and stay tuned for more updates!

10
Subscribe to my newsletter

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

Written by

ASHISH PATIL
ASHISH PATIL

Hi I am Ashish Patil, a fresher with a keen interest in AWS, DevOps, and Linux. I have been learning about cloud infrastructure and automation, and I am excited to apply my skills in real-world projects. I am committed to continuous improvement and eager to grow in the tech industry.