✍️Day 4: Basic Linux Shell Scripting for DevOps Engineer

✍️Day 4: Basic Linux Shell Scripting for DevOps Engineer

Table of content

  • What is Kernel?

  • What is Shell?

  • What is Linux Shell Scripting?

  • What is Shell Scripting for DevOps with Examples?

  • Getting started with a simple script 🚀

  • Basic Shell scripting concepts📚

  • Real-world examples🌍

  • Conclusion🎯

  • ✍️What is Kernal?

    Kernel is the core of an operating system. It acts as a bridge between the hardware and software, managing system resources and allowing communication between them. Think of it as the brain 🧠 of your computer, handling all the essential tasks to keep everything running smoothly.

    ✍️What is Shell?

    Shell is a command-line interface (CLI) that allows users to interact with the operating system by typing commands. It acts as a mediator between the user and the kernel, translating user commands into actions performed by the kernel.

    ✍️What is Linux Shell Scripting?

    Shell script is a program written for the shell, or command line interpreter, of an operating system. Think of it as a list of commands that you can save in a file and execute whenever you need to. It's like writing a recipe 📝 for the computer to follow.

    ✍️What is Shell Scripting for DevOps with Examples?

    Shell scripting for DevOps is like having a digital assistant that helps you automate repetitive tasks and manage your software development and operations more efficiently. It's a way to write small programs (scripts) using commands that your computer understands, allowing you to automate tasks such as setting up servers, deploying software, or checking system health.

    Here are some simple examples:

    1. Automating Deployment: Imagine you have to deploy your application to several servers every time you make changes. Instead of doing it manually each time, you can write a script that does it for you automatically.

    2. Infrastructure Setup: If you need to create virtual machines or set up cloud resources frequently, a script can handle that process, saving you time and effort.

    3. Backup and Maintenance: You can write scripts to back up your databases or files regularly, ensuring that your data is safe. You can also create scripts to monitor system health and alert you if something goes wrong.

    4. CI/CD Pipelines: In software development, continuous integration and continuous deployment (CI/CD) pipelines automate the building, testing, and deployment of code changes. Shell scripts can help orchestrate these pipelines, making the process smooth and efficient.

    5. System Monitoring: Scripts can continuously check system performance metrics and notify you if any thresholds are exceeded, allowing you to take proactive action to avoid potential issues.

Getting Started with a Simple Script 🚀

Let's write a simple script that prints "Hello, DevOps World!" to the terminal.

  1. Open your favorite text editor (like nano or vim) 📝

  2. Type the following script:

      #!/bin/bash
      echo "Hello, DevOps World! 🌍"
    
  3. Save the file as hello.sh 💾

  4. Make the script executable by running:

      chmod +x hello.sh
    
  5. Execute the script by running:

      ./hello.sh
    

And there you go! You’ve just written and executed your first shell script. 🎉

Basic Shell Scripting Concepts 📚

Variables 📦

Variables store data that can be used and manipulated throughout your script. Here’s how to use them:

    #!/bin/bash
    name="Ritesh"
    echo "Hello, $name! 👋"

Loops 🔄

Loops help you repeat tasks efficiently. Here’s an example of a for loop:

    #!/bin/bash
    for i in 1 2 3 4 5
    do
      echo "Welcome $i times! 🙌"
    done

Conditional Statements 🔍

Conditional statements allow you to make decisions in your script:

    #!/bin/bash
    num=10
    if [ $num -gt 5 ]; then
      echo "$num is greater than 5 👍"
    else
      echo "$num is not greater than 5 👎"
    fi

Real-World Examples 🌍

1) Backup Script 🗂️

Here’s a script to back up a directory:

    #!/bin/bash
    src="/home/user/documents"
    dest="/home/user/backup"
    cp -r $src $dest
    echo "Backup completed successfully! ✅"

2) Monitoring Script 📊

A script to check if a service is running:

    #!/bin/bash
    service="nginx"
    if pgrep -x "$service" >/dev/null
    then
      echo "$service is running 🟢"
    else
      echo "$service is not running 🔴"
    fi

Conclusion 🎯

Shell scripting is an essential skill for DevOps engineers. It allows you to automate and streamline your workflows, making you more efficient and productive. 🚀 Keep experimenting, and soon you’ll be writing scripts to handle all sorts of tasks! 💪

Stay tuned for Day 5, where we’ll explore more exciting DevOps topics. 📅

Happy scripting! 😊