🖥️Day 4 - Basic Linux Shell Scripting for DevOps Engineers

🖥️Day 4 - Basic Linux Shell Scripting for DevOps Engineers

📍What is Kernel?

The kernel serves as the central component of an operating system, acting as a bridge between hardware and user-level applications. Its main functions include managing system resources, ensuring a secure and stable execution environment, and enabling communication between software and hardware components.

🗝Key functions of the Linux kernel include:

  1. Memory management.

  2. Process Management.

  3. File System Management.

  4. Security.

  5. Device Drivers.

  6. System Calls.

📍What is Shell?

In Linux, a shell serves as a Command Line Interface (CLI), enabling users to interact with the operating system through text-based commands. Essentially, it acts as a mediator between the user and the computer, facilitating communication and task execution through commands. Additionally, it functions as a scripting language, enabling users to create scripts for automating various tasks.

📍What is Linux Shell Scripting?

Shell scripting is a powerful way to automate tasks, run complex commands, and perform various operations in a more efficient and streamlined manner. It refers to the practice of writing scripts or programs using a shell (command-line interface) in a Linux or Unix-like operating system. These scripts are essentially sequences of commands written in a scripting language that the shell can interpret and execute.

📍What is Shell Scripting for DevOps with Examples?

In DevOps, Shell Scripting is pivotal for automating and streamlining tasks throughout the development and operations lifecycle. It stands as a critical scripting tool for DevOps experts to automate regular tasks, oversee infrastructure, and coordinate processes.

Here are some key aspects of Shell Scripting in the context of DevOps:

  1. Automation of Deployment and Configuration:

    Shell scripts are used to automate the deployment of applications and configuration of servers. For example, a script can be created to install dependencies, set up environment variables, and deploy the latest version of an application.

      # Example: Deploying a web application
      #!/bin/bash
    
      echo "Updating code from version control..."
      git pull
    
      echo "Installing dependencies..."
      npm install
    
      echo "Restarting the application server..."
      systemctl restart my-todo-app
    
  2. Continuous Integration/Continuous Deployment (CI/CD):

    Shell scripts are commonly used in CI/CD pipelines to automate building, testing, and deploying applications. They integrate with CI/CD tools to execute these tasks automatically when code changes are pushed.

      # Example: CI/CD pipeline script
      #!/bin/bash
    
      echo "Building the application..."
      ./build.sh
    
      echo "Running tests..."
      ./run_tests.sh
    
      echo "Deploying to production..."
      ./deploy_prod.sh
    
  3. Infrastructure as Code (IaC):

    Shell scripts help implement Infrastructure as Code principles, allowing DevOps teams to define and manage infrastructure using code. This includes provisioning and configuring servers, networks, and other infrastructure components.

      # Example: Provisioning a virtual machine
      #!/bin/bash
    
      echo "Creating a new virtual machine..."
      virt-install --name my-vim --memory 2048 --disk size=20 --cdrom my-os.iso
    

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

The #!/bin/bash (or #!/bin/sh) at the beginning of a script is called a shebang or hashbang. It is a special line that informs the system which interpreter should be used to execute the script. In the context of your question:

  1. #!/bin/bash

    This shebang specifies that the Bash shell should be used to interpret and execute the script. Bash is a popular and feature-rich shell, and it is the default shell for many Linux distributions.

      #!/bin/bash
      echo "Hello, this script is written in Bash."
    
  2. #!/bin/sh:

    This shebang indicates that the Bourne shell should be used. The Bourne shell is a simpler shell and is often used for writing more portable scripts that should work on a variety of Unix-like systems.

      #!/bin/sh
      echo "Hello, this script is written in the Bourne shell."
    

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

#!/bin/bash

# Taking user input
echo "Enter your name:"
read username

# Taking input from command-line arguments
arg1=$1
arg2=$2

# Printing the variables
echo "User input: $username"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"

Save this script in a file, for example, input_script.sh. Give execute permissions to the script using the chmod +x input_script.sh command.

You can then run the script and provide command-line arguments like this:

COPY

./input_script.sh argument1 argument2

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

COPY

#!/bin/bash

# Prompt the user to enter two numbers
echo "Enter the first number:"
read num1

echo "Enter the second number:"
read num2

# Compare the numbers
if [ "$num1" -eq "$num2" ]; then
    echo "The numbers are equal."
else
    echo "The numbers are not equal."
fi

Save this script in a file, for example, compare_numbers.sh, and give execute permissions using chmod +x compare_numbers.sh. Run the script, and it will prompt you to enter two numbers, then compare and print the results based on the if-else conditions.

🔎Conclusion:

Shell scripting is a powerful tool for DevOps professionals. It enables automation, simplifies tasks, and helps maintain consistency across various systems. With a strong foundation in shell scripting, you can take your DevOps skills to the next level and make your operations more efficient and reliable.

I'm confident that this blog will prove to be valuable, helping you discover new insights and learn something enriching .🙏

😊Happy Learning : )