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

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

📍What is Kernel?

Kernel is the core component of the operating system. The kernel acts as an intermediator between the hardware and the user-level applications. It's primary responsibilities include managing system resources, providing secure and stable execution environment and facilitating 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 is a CLI (Command Line Interface) that allows the users to interact with the operating system by typing text-based commands. Means, It is like the friendly middle-person between you and the computer, helping you communicate and get things done with text-based commands. It is also a scripting language, allowing users to write scripts to automate 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 the realm of DevOps, Shell Scripting plays a crucial role in automating and streamlining various tasks involved in the development and operations lifecycle. It is an essential scripting tool for DevOps practitioners to automate routine tasks, manage infrastructure, and orchestrate 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 which prints "I will complete #90DaysOfDevOps challenge" .

devops.png

✅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:

./input_script.sh argument1 argument2

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

#!/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 : )

Did you find this article valuable?

Support Pratik R. Mandge by becoming a sponsor. Any amount is appreciated!