Basic Linux Shell Scripting for DevOps Engineers.

Basic Linux Shell Scripting for DevOps Engineers.

What is kernel?

In the context of Linux and other Unix-like operating systems, the term "kernel" refers to the core component of the operating system. The Linux kernel is responsible for managing hardware resources, providing essential services to other parts of the operating system, and facilitating communication between software and hardware components.

Here are some key features and responsibilities of the Linux kernel:

  1. Process Management: The kernel manages processes, which are running instances of programs. It handles process creation, scheduling, and termination.

  2. Memory Management: The kernel is responsible for managing the system's memory, including allocation and deallocation of memory for processes, virtual memory, and page caching.

  3. Device Drivers: The kernel provides a framework for device drivers, which are software components that enable the operating system to communicate with hardware devices such as hard drives, network interfaces, and graphics cards.

  4. File System Management: The kernel handles file systems, providing the necessary functionality for reading, writing, and organizing data on storage devices.

  5. System Calls: System calls are interfaces between user-space applications and the kernel. They allow applications to request services from the kernel, such as file operations, process control, and communication.

  6. Security: The kernel enforces security policies and controls access to system resources to ensure the integrity and confidentiality of the system.

  7. Networking: The kernel manages network communication, including protocols, sockets, and network device drivers.

The Linux kernel is open source, meaning its source code is freely available and can be modified and distributed by users. Different Linux distributions may use the same Linux kernel but include different user-space components, such as utilities, libraries, and graphical interfaces, to create a complete operating system. The development and maintenance of the Linux kernel are overseen by Linus Torvalds and a community of developers around the world.

Example:

Imagine your computer as a well-organized city with lots of buildings (applications and software) and various services (hardware like printers, storage, etc.). The kernel is like the mayor or central authority that ensures everything runs smoothly and that the buildings can communicate with the services effectively.

Why kernel is so important?

The kernel is crucial in Linux (as well as in any operating system) for several fundamental reasons:

  1. Hardware Interaction: The kernel acts as an intermediary between software and hardware. It manages communication between the applications you run and the physical hardware components of your computer, such as the CPU, memory, storage devices, and peripherals. This interaction is essential for any computer to function properly.

  2. Resource Management: The kernel is responsible for efficiently allocating and managing system resources like memory and CPU time among different applications and processes. It ensures that each program gets its fair share of resources, preventing one misbehaving program from causing the entire system to crash.

  3. Process Scheduling: The kernel is in charge of scheduling processes (individual tasks or programs) to run on the CPU. It decides which processes get CPU time and in what order, managing the execution of multiple tasks concurrently.

  4. Device Drivers: The kernel includes device drivers, which are software components that allow the operating system to communicate with and control hardware devices. These drivers enable the proper functioning of devices like printers, graphics cards, network interfaces, and more.

  5. System Calls: The kernel provides a set of system calls, which are interfaces that allow applications to request services from the operating system. This includes actions like reading or writing to files, creating new processes, and managing memory.

  6. Security and Access Control: The kernel plays a critical role in enforcing security policies and access controls. It ensures that different processes and users have appropriate permissions to access resources, preventing unauthorized actions and protecting the overall system integrity.

  7. Error Handling: The kernel monitors the system for errors and exceptions. When issues arise, the kernel is designed to handle them gracefully, preventing crashes or data corruption as much as possible.

  8. Stability and Reliability: The stability and reliability of the entire operating system depend on the robustness of the kernel. A well-designed and well-maintained kernel contributes to a stable and reliable computing environment.

In summary, the Linux kernel is the core component that enables the operating system to function by managing hardware resources, facilitating communication between software and hardware, and providing essential services to user applications. Its importance lies in its role as the foundation upon which the entire operating system operates.

What is shell?

In Linux, the term "shell" refers to the command-line interface that allows users to interact with the operating system. The shell is a program that interprets user commands and executes them. It acts as an intermediary between the user and the operating system's kernel.

Here are some key aspects of the shell in Linux:

  1. Command Interpreter: The shell interprets the commands entered by the user and converts them into a format that the kernel can understand and execute. Users can interact with the system by typing commands into the shell.

  2. Scripting: Shells in Linux support scripting, which involves writing a sequence of commands in a file (known as a script) to perform tasks automatically. This is particularly useful for automating repetitive tasks.

  3. Command Execution: When a user enters a command, the shell searches for the corresponding program or executable in the system directories and then runs that program. If the command involves a system call or kernel operation, the shell communicates with the kernel to perform the requested action.

  4. Variable and Environment Management: Shells allow the use of variables to store and retrieve information. They also manage the environment in which commands run, controlling aspects such as file paths, user permissions, and other settings.

  5. Redirection and Pipes: Shells provide mechanisms for redirecting input and output. This allows users to save the output of a command to a file or use the output of one command as input for another through the use of pipes.

  6. Job Control: Shells support job control, allowing users to run processes in the background, bring them to the foreground, pause, resume, or terminate them.

Common shells in Linux include:

  • Bash (Bourne Again SHell): This is the default shell for most Linux distributions and is widely used.

  • Zsh (Z Shell): A feature-rich shell with advanced capabilities.

  • Fish (Friendly Interactive SHell): Known for its user-friendly and interactive features.

  • Dash: A lightweight shell, often used in system scripts.

The choice of shell can depend on user preference, system requirements, and specific features needed for scripting or interactive use.

Example:

Linux shell as the "command center" or "control hub" for your computer. It's like a user interface that allows you to communicate with your computer by typing commands in plain text.

Tasks:

  • Explain in your own words and examples, what is Shell Scripting for DevOps.

Shell scripting in DevOps is the practice of writing scripts to automate tasks related to software development, deployment, and system management. It involves using shell languages like Bash to create reusable scripts that execute a series of commands or operations.

For example, consider a DevOps task of deploying a web application. Instead of manually executing each step, a shell script can be created to automate the process. The script might include commands to pull the latest code from a version control system, install dependencies, build the application, and deploy it to a server. By running this script, the entire deployment process can be automated, saving time and ensuring consistency.

In essence, shell scripting in DevOps is about streamlining and automating routine tasks to enhance efficiency and maintain a standardized and reproducible environment throughout the software development lifecycle.

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

    #!/bin/bash is called a shebang, and it is placed at the beginning of a shell script to indicate the path to the interpreter that should be used to execute the script. In this case, it specifies that the Bash shell should be used.

    Yes, you can also use #!/bin/sh in the shebang line. This specifies the system's default shell, which may or may not be Bash. Using #!/bin/sh is more portable as it ensures compatibility with a broader range of Unix-like systems. However, keep in mind that Bash provides some features and syntax extensions beyond the POSIX standard, so if you rely on Bash-specific features, it's better to use #!/bin/bash.

    In summary, #!/bin/bash specifies Bash, and #!/bin/sh specifies the system's default shell, which is often Bash or another POSIX-compliant shell. The choice depends on your script's requirements and the level of portability you want to maintain.

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

    First of all we create a file that is File1.sh in ubuntu because we use the ubuntu linux machine. After that I join to the ubuntu linux machine and create the file.For create the File we use vi editor.

    So what is the vi editor please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2.

    When I type the vi File1.sh and click the enter button, so the editor is open after that I press i button and write something.

    For print the any statement you can use echo command in linux. So what is echo command please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2

    After that press the esc button and type :wq and click the enter button. So what is :wq, It is write and quit.After that type the bash<filename> command to print the statement.

  • Write a Shell Script to take user input print the variables.

    First of all we create a file that is File1.sh in ubuntu because we use the ubuntu linux machine. After that I join to the ubuntu linux machine and create the file.For create the File we use vi editor.

    So what is the vi editor please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2.

    When I type the Vi File1.sh and click the enter button, so the editor is open after that I press i button and write shell script file.

    After that press the esc button and type :wq and click the enter button. So what is :wq, It is write and quit.After that type the bash<filename> command.

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

    First of all we create a file that is File1.sh in ubuntu because we use the ubuntu linux machine. After that I join to the ubuntu linux machine and create the file.For create the File we use vi editor.

    So what is the vi editor please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2.

    When I type the Vi File1.sh and click the enter button, so the editor is open after that I press i button and write shell script file.

    After that press the esc button and type :wq and click the enter button. So what is :wq, It is write and quit.After that type the bash<filename> command.

  • Bash For Loop 1 to 10

    First of all we create a file that is File1.sh in ubuntu because we use the ubuntu linux machine. After that I join to the ubuntu linux machine and create the file.For create the File we use vi editor.

    So what is the vi editor please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2.

    When I type the Vi File1.sh and click the enter button, so the editor is open after that I press i button and write shell script file.

    After that press the esc button and type :wq and click the enter button. So what is :wq, It is write and quit.After that type the bash<filename> command.

  • Functions with Passing Arguments:

    First of all we create a file that is File1.sh in ubuntu because we use the ubuntu linux machine. After that I join to the ubuntu linux machine and create the file.For create the File we use vi editor.

    So what is the vi editor please check my previous blog that is https://udayyysharma.hashnode.dev/50-basic-linux-commandslinux-part-2.

    When I type the Vi File1.sh and click the enter button, so the editor is open after that I press i button and write shell script file.

    After that press the esc button and type :wq and click the enter button. So what is :wq, It is write and quit.After that type the bash<filename> command.

I hope you enjoyed this blog on learning Shell scripting for DevOps engineers. I have completed basic command and basic script in this blog. For Linux blog we will continue next blog.