Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

What is Kernel?

In simple terms, the kernel is like the engine of your computer's operating system. It’s the core part that acts as a bridge between the hardware (like your CPU, memory, and devices) and the software (programs and applications you run).

Here’s how it works in layman’s language:

  • Imagine your computer is like a car, and the kernel is the engine.

  • It controls the “communication” between different parts of the car (or the computer's hardware) and makes sure everything works smoothly.

  • When you click on an application (like a web browser), the kernel tells the CPU to give it the power to run and manages resources like memory or storage so things don’t crash or overload.

So, the kernel is vital for making sure your computer runs efficiently by managing all the low-level stuff behind the scenes.

What is Shell?

The shell is like a translator between you and your computer.

Here’s a simple breakdown:

  • When you type commands or click on things in your computer, the shell is what takes those actions and tells the operating system (through the kernel) what you want it to do.

  • It’s like a messenger: you give it instructions, and it passes them to the deeper parts of the system to make things happen.

If you’ve ever used a command line or terminal (where you type commands instead of clicking), that’s a type of shell. It helps you communicate directly with the computer by typing commands instead of using a mouse or icons.

So, while the kernel runs the system, the shell helps you talk to the kernel.

What is Linux Shell Scripting?

Linux shell scripting is like giving your computer a list of tasks to do automatically, instead of doing them one by one.

In simple words:

  • A shell script is a file with a series of commands written inside it.

  • These commands tell the computer what to do, like copying files, moving data, or checking for updates.

  • Instead of typing commands manually each time, you can put them all in a script, and the computer will follow the steps in the order you’ve written them.

It’s like writing a recipe for the computer, where each step in the recipe is a command, and once the script runs, the computer follows your instructions exactly, saving you time and effort.

Tasks:

[if !supportLists]1. [endif]Explain in your own words and with examples what Shell Scripting means for DevOps.

In DevOps, shell scripting is a way to automate repetitive tasks and processes to make everything run faster and more consistently.

For example:

  • A DevOps engineer might write a script to automatically deploy code to a server, check system health, or back up files.

  • Instead of doing each task manually, the script runs the steps in one go, saving time and reducing human error.

In short, shell scripting in DevOps helps streamline workflows, making processes more efficient and reliable.

  • It controls the “communication” between different parts of the car (or the computer's hardware) and makes sure everything works smoothly.

[if !supportLists]2. [endif]What is #!/bin/bash? Can we write #!/bin/sh as well?

The line #!/bin/bash or #!/bin/sh at the top of a script is called a shebang. It tells the system which shell to use to run the script.

  • #!/bin/bash means the script should run using the Bash shell, which is a popular shell with more features and flexibility.

  • #!/bin/sh means the script should run using the sh shell, which is usually a simpler, more basic shell (often a link to a smaller version of Bash).

Yes, you can write #!/bin/sh instead of #!/bin/bash, but keep in mind that some commands or features available in Bash may not work in sh.

In short:

  • Bash is more feature-rich and commonly used.

  • sh is lighter and may be available on all systems, but it has fewer features.

  1. Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.

Create a new file, e.g., Devops_Script.sh

Give it execute permissions using: chmod 700 (+x) Then Run the script.

  1. Write a Shell Script that takes user input, input from arguments, and prints the variables.

    Give it execute permissions using: chmod 700 (+x) Then Run the script.

    Run the script, passing arguments (e.g., "DevOps" and "Challenge")

  2. Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.

    Here's a simple example of an if-else statement in shell scripting that compares two numbers:

    Explanation:

    • -gt checks if the first number is greater than the second.

    • -lt checks if the first number is less than the second.

    • If both conditions are false, it means the numbers are equal

      It will prompt you to enter two numbers and then display the comparison result!