Day-4 Basic Linux Shell Scripting for DevOps EngineeršŸ’»āš™ļø

Day-4 Basic Linux Shell Scripting for DevOps EngineeršŸ’»āš™ļø

Ā·

7 min read

  1. What is a Kernel?

  2. What is a Shell?

  3. What is Linux Shell Scripting?

  4. Tasks?

What is a kernel?

The kernel is the heart of an operating system, controlling everything from hardware interaction to system processes. It's what makes your computer function.

Think of the kernel as the "engine" of a car. Just like how the engine controls all the essential functions of a carā€”fueling the movement, managing the brakes, and interacting with other componentsā€”the kernel controls and manages everything inside a computer, including how it talks to hardware like the CPU, memory, and storage.

For example:

  • When you press a key on your keyboard, the kernel ensures that the input reaches the right program, such as your browser or text editor.

  • When you open a file, the kernel decides how the data from your hard drive gets loaded into your computerā€™s memory so the file can be displayed.

In short, the kernel ensures that the hardware and software communicate smoothly, making your computer function efficientlyā€”just like the engine provides a smooth drive!

What is a shell?

A shell is a special user program that provides an interface for users to interact with the operating system. Essentially, it converts human-readable commands into instructions the kernel can process. The shell bridges you and the system's core, whether you're typing commands in the terminal or running scripts.

Imagine the shell as a remote control for your TV. Just like how you press buttons on the remote to control the TVā€”like changing the channel or adjusting the volumeā€”the shell lets you control the computer by typing commands.

For example, if you want to see a list of files in a folder, you would type this command:

ls

The shell then tells the computer to show you the files, similar to how the remote sends signals to the TV to change channels.

In simple terms, the shell is your tool for telling the computer what to do, just like the remote is your tool for controlling the TV!

What is Linux Shell Scripting?

Linux shell scripting allows you to automate tasks, manage system processes, and interact with the operating system in an efficient and programmable way. For DevOps engineers, shell scripting is crucial as it helps in automating repetitive tasks, managing server operations, and creating scalable deployment pipelines.

Here are some simple examples of how Linux shell scripting helps DevOps engineers by automating tasks and managing systems:

1. Automating a Backup Task

Instead of manually copying important files to a backup folder every day, you can create a shell script that does it automatically:

#!/bin/bash
cp /home/user/documents/* /home/user/backup/
echo "Backup completed!"

This script copies all files from the documents folder to the backup folder. You can schedule this script to run daily using a cron job, automating the backup process.

2. Managing Server Operations

If you need to restart a service like Apache on a server, you can use a shell script to do it automatically:

#!/bin/bash
sudo systemctl restart apache2
echo "Apache server restarted!"

This script restarts the Apache server. Instead of logging into the server and manually running commands, the script handles it for you.

3. Deploying Code to Production

Instead of manually deploying code, you can create a script that automates the process:

#!/bin/bash
git pull origin main
docker-compose up -d
echo "Deployment completed!"

This script pulls the latest code from the repository and runs the application using Docker. It saves time by automating the deployment process.

4. Checking Disk Space

You can automate the monitoring of disk space on a server:

#!/bin/bash 
df -h > disk_usage.txt echo "Disk space report generated."

This script checks the disk space and saves the output to a text file. You can automate it to run daily and keep track of your serverā€™s storage.

In short, shell scripting simplifies repetitive tasks, ensures consistency, and saves time for DevOps engineers, allowing them to focus on more critical work!

TASKS?

  1. Explain in your own words and with examples what Shell Scripting means for DevOps.

    shell scripting in DevOps is like giving the computer a set of written instructions to automate repetitive tasks. Instead of typing commands every time we need to operate, we write a scriptā€”a small programā€”that can execute those commands automatically.

    Why is it Important in DevOps?

    In DevOps, automation is crucial for improving efficiency and consistency. Here are some key ways DevOps engineers use shell scripts:

    • Automate Deployments: Shell scripts can streamline the deployment process. For example, when a developer pushes new code to a repository, a script can automatically pull that code to the server and restart the application.

    • Manage Servers: Routine server management tasks can be automated. For instance, if a service needs to be restarted, a shell script can handle this without manual intervention.

    • Monitor Systems: Shell scripts can be used to check the health of servers. For example, a script can verify if a web server is running smoothly and send alerts if it isnā€™t.

Simple Company Example

Imagine you work at a company that hosts a web application, and every day you need to perform several tasks:

  1. Check if the web server is running.

  2. Clean up old log files to free up disk space.

  3. Take a backup of the database.

Instead of manually doing these tasks each day, you can create a shell script that automates the process:

This script runs all three tasks automatically, saving time and reducing the chance of human error.

  1. What is #!/bin/bash? Can we write #!/bin/sh as well?

    The line #!/bin/bash (or #!/bin/sh) at the beginning of a script is known as a shebang. It indicates to the operating system which interpreter to use to execute the script.

    What is #!/bin/bash?

    • Purpose: This line tells the system to run the script using the Bash shell, which is a widely used shell for scripting on Unix-like operating systems.

    • Location: The path /bin/bash specifies where the Bash interpreter is located on the system.

What about #!/bin/sh?

  • Purpose: Similarly, #!/bin/sh indicates that the script should be executed using the sh shell (often referred to as the Bourne shell). This is a more basic shell and typically has fewer features than Bash.

  • Usage: Using #!/bin/sh is sometimes preferred for compatibility, especially for simple scripts that donā€™t require Bash-specific features. It ensures the script can run on systems where Bash may not be available.

Example

Using #!/bin/bash:

    #!/bin/bash
    echo "This script uses Bash features."

Using #!/bin/sh:

    #!/bin/sh
    echo "This script uses basic shell commands."

Both scripts will run, but the first can utilize more advanced features available in Bash, while the second is more universally compatible.

  1. Shell Script that Prints "I will complete #90DaysOfDevOps challenge."

     #!/bin/bash # This script prints a motivational message
      echo "I will complete #90DaysOfDevOps challenge."
    
  2. Shell Script that Takes User Input and Input from Arguments

     #!/bin/bash
     # This script takes user input and command-line arguments and prints them
    
     # Taking user input
     read -p "Enter your name: " name
    
     # Taking input from command-line arguments
     arg1=$1
     arg2=$2
    
     # Printing the variables
     echo "Hello, $name! You passed the following arguments: $arg1 and $arg2."
    

    Usage: You can run this script from the terminal like this:

      script.sh argument1 argument2
    
    1. Example of an If-Else Statement in Shell Scripting
    #!/bin/bash
    # This script compares two numbers and prints a message based on the comparison

    # Taking user input for two numbers
    read -p "Enter the first number: " num1
    read -p "Enter the second number: " num2

    # Comparing the numbers using an if-else statement
    if [ "$num1" -gt "$num2" ]; then
        echo "$num1 is greater than $num2."
    elif [ "$num1" -lt "$num2" ]; then
        echo "$num1 is less than $num2."
    else
        echo "$num1 is equal to $num2."
    fi

We appreciateā¤ļø you taking the time to read and connect with us! Your engagement means a lot to us, and we look forward to hearing more from youšŸ“

Ā