Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.

The architecture of Linux os

Think of the architecture of a Linux operating system (OS) as a building with different layers. Each layer has a specific job that contributes to making the whole system work smoothly.

  1. Hardware Layer:

    • This is the physical layer, like the foundation of a building.

    • It includes your computer's components: processor, memory, hard drive, and more.

    • Just as a building's foundation supports everything above it, the hardware layer supports the entire system.

  2. Kernel Layer:

    • Like the building's structural framework, the kernel provides the core functions of the OS.

    • It manages hardware resources, memory, and communication between software and hardware.

    • It's the heart of the system, ensuring that everything runs correctly.

  3. System Libraries:

    • Think of these as the utilities and tools that help with specific tasks.

    • They're like the plumbing and wiring in a building that enables various functions.

    • These libraries provide pre-built functions that software programs can use.

  4. System Utilities:

    • These are like the various rooms and facilities in a building.

    • They include tools for managing files, setting up networks, and handling system administration.

    • Just as rooms serve different purposes in a building, utilities serve different functions in the OS.

  5. Shell:

    • The shell is like the interface that lets you give instructions to the system.

    • It's your way of communicating with the OS using commands.

    • Think of it as the reception desk of the building where you interact with the staff.

  6. User Applications:

    • These are the software programs you use daily, like browsers, word processors, and games.

    • They're like the furniture and decorations in a building that makes it functional and enjoyable.

    • User applications run on top of the layers below them, using their resources and services.

All these layers work together to create the Linux operating system, much like how different components in a building contribute to its overall functionality and purpose.

What is Kernel?

The kernel is like the "heart" of your computer's operating system. It's the core software that manages the communication between the hardware (like your computer's memory, processor, and devices) and the software applications you use.

Imagine your computer is in a busy city. The kernel is the mayor who ensures that all the different parts of the city (programs, files, devices, etc.) work together smoothly. It handles tasks such as managing memory, controlling how programs access the processor, and handling input and output from devices like keyboards, mice, and printers.

Whenever you run a program or perform any action on your computer, the kernel is there, making sure everything happens in the right order and that different programs don't interfere with each other. It's an essential part of your computer's operating system, quietly doing its job to keep everything running smoothly.

What is Shell?

A shell in Linux is a command-line interface that lets you interact with the operating system by typing text-based commands. It acts as a bridge between you and the computer, translating your commands into actions that the computer understands. Just like talking to a personal assistant, you tell the shell what you want, and it communicates with the computer's core to make things happen.

What is Linux Shell Scripting?

Linux shell scripting involves creating sequences of commands in a text file that can be executed by the shell (command-line interface). It's like giving the computer a list of instructions to automate tasks. This scripting simplifies repetitive tasks, file management, and system administration. It's like creating custom tools to make your computer do things automatically.

What are the different types of shells in Linux?

In Linux, there are different "shells," which are like flavors of the command-line interface. Each shell offers slightly different features and ways to interact with the system. Here are a few common ones explained in simple terms:

  1. Bash (Bourne Again Shell):

    • This is one of the most popular and widely used shells.

    • It's known for its flexibility and extensive features.

    • Bash is often the default shell on many Linux systems.

    • Think of it as the versatile all-rounder.

  2. sh (Bourne Shell):

    • The original shell that inspired many others.

    • It's simple and efficient but lacks some advanced features.

    • Think of it as the classic and reliable choice.

  3. zsh (Z Shell):

    • A modern and feature-rich shell.

    • It offers helpful features for customization and productivity.

    • Think of it as the stylish and efficient option.

  4. fish (Friendly Interactive Shell):

    • Designed to be user-friendly with auto-suggestions and colourful prompts.

    • It's great for beginners and those who appreciate a more intuitive interface.

    • Think of it as a user-friendly and supportive buddy.

  5. csh (C Shell):

    • Offers a syntax similar to the C programming language.

    • It's good for specific tasks, but not as widely used.

    • Think of it as the C-programmer's shell.

  6. Dash:

    • Dash is minimalistic and efficient, often used for system scripts and boot scripts.

    • It's lightweight and suitable for quick operations, but not ideal for interactive use due to limited features.

    • Think of Dash as a speedy shell for system-level tasks.

Each shell has its strengths and purposes. It's like choosing between different tools for different jobs. You can pick the one that suits your style and the tasks you frequently do in the command line.

What is Shell Scripting for DevOps?

Shell scripting for DevOps, in simple terms, is a way to automate and manage various tasks related to software development and IT operations using text-based commands. It involves writing sequences of commands in a file (the script) that can be executed by a shell interpreter (like Bash) to perform tasks such as setting up servers, deploying applications, managing files, and more.

Think of it like creating a set of instructions that a computer can follow to perform repetitive or complex tasks. Instead of manually typing commands one by one, you write them in a script, making it easier to repeat those tasks accurately and quickly. This is particularly useful in the world of DevOps, where teams need to manage infrastructure, deploy applications, and handle configurations efficiently and reliably.

In essence, shell scripting for DevOps is about simplifying and automating the tasks involved in building, deploying, and maintaining software systems, ultimately saving time and reducing the chance of human errors. It's a fundamental skill for anyone working in the DevOps field.

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

#!/bin/bash and #!/bin/sh are called "shebang" lines in shell scripting. They indicate which shell interpreter should be used to run the script. Let's see what they mean:

  1. #!/bin/bash:

    • This line is called a shebang or hashbang.

    • It's the first line of a shell script, and it tells the operating system which interpreter to use to execute the script.

    • In this case, #!/bin/bash specifies that the Bash shell should be used to interpret and execute the script.

    • Bash is a popular and powerful shell, commonly found on Unix-like systems (Linux, macOS, etc.).

    • Using #!/bin/bash at the beginning of your script ensures that it's interpreted using the features and syntax of the Bash shell.

  2. #!/bin/sh:

    • This is another shebang line.

    • However, #!/bin/sh specifies the default system shell as the interpreter for the script.

    • The system shell might be Bash, but it could also be another compatible shell-like Dash (a minimal shell) or another shell specified by the system.

    • Using #!/bin/sh provides more portability across different systems, as it relies on the basic features that most shell interpreters share.

For beginners, it's generally good practice to use #!/bin/bash because it ensures that you have access to a wider range of scripting features that can make your tasks easier. However, if you're writing very basic scripts and want to ensure portability across different Unix-like systems, using #!/bin/sh might be a better choice. Just keep in mind that some advanced features might not be available in simpler shell interpreters.

The shebang line is like a roadmap that tells your system which shell interpreter should be used to understand and execute the commands in your script. It's an essential starting point for any shell script.

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge

To create and run this script:

  1. Open a text editor (like Notepad on Windows, or any text editor on Linux/macOS).

  2. Write the command into the text editor.

  3. Save the file with a .sh extension, such as devops_challenge.sh.

  4. Open a terminal.

  5. Navigate to the directory where you saved the script.

  6. Make the script executable by running the command in the terminal: chmod +x devops_challenge.sh

  7. Run the script by typing: ./devops_challenge.sh in the terminal.

The script will then display the message "I will complete #90DaysOfDevOps challenge" on the terminal when executed.

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

A shell script that takes user input, and command-line arguments, and then prints the variables:

Save this script to a file, for instance, input_and_arguments.sh. Make the file executable using:

You can run the script in a terminal and provide command-line arguments like this:

The script will prompt you for input as well. After running the script, it will display:

In this example, argument1 and argument2 are the command-line arguments you provided. The script also waits for you to enter your name and then displays it as the user input.

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

Save this script to a file, such as compare_numbers.sh. Make the file executable:

Run the script and provide two numbers when prompted. The script will compare the numbers and provide an appropriate output.

For example, if you input 10 and 15, the script will output:

This script uses the if, elif (else if), and else statements to compare the numbers and print out the appropriate message based on their relationship.

Conclusion

Linux shell scripting is a powerful way to automate tasks and interact with the operating system using text-based commands. It empowers users to create custom tools, enhance efficiency, and manage various aspects of their computer easily.

I appreciate your time in reading this blog. Thank you for engaging with the content.

I trust you found the information both helpful and insightful. Enjoy your learning journey, and feel free to reach out with any further questions!