Automate Your Linux Tasks With Shell Scripting

Automate Your Linux Tasks With Shell Scripting

Before we start learning shell scripting, we should learn about the basics of operating systems and kernels so that when it comes to shell scripting, we can understand and relate most easily.

What Is An Operating System?
An operating system (OS) is a software program that manages the computer's hardware and software resources and provides common services for computer programs. It acts as a mediator between the user and the computer hardware.

The operating system is responsible for controlling and allocating memory, managing input and output devices, managing file systems and user accounts, and providing security services such as virus protection and user authentication.

The most common operating systems are Microsoft Windows, macOS, and Linux, but many other operating systems are used for specific purposes or in specialized industries.

What is a kernel in the operating system?

The kernel is the heart and main component of operating systems. It is the kernel through which OS can perform different tasks and control the system.

In another word, we can say the kernel is the bridge between the hardware and the software layers of the operating system, providing a consistent and reliable interface for software applications to interact with the underlying hardware.

Functions Of Kernel:

  1. Scheduling Processes

  2. Resource Allocation

  3. Device Management

  4. Memory Management

  5. Interrupt Handling and System Calls

  6. Process Management

Types Of Kernels:

  1. Monolithic

  2. Microkernel

  3. Hybrid

  4. Exokernel

  5. Nano

Our today's focus is on the Linux Operating system which uses Shell as a command language interpreter that the kernel can understand.

What is a Shell?

A shell is a program that acts as an interface between a user and the kernel. It allows a user to give commands to the kernel and receive responses from it. Through a shell, we can execute programs and utilities on the kernel. Hence, at its core, a shell is a program used to execute other programs on our system.

What are the different types of shells?

  1. The Bourne Shell: Bourne shell was developed at AT&T Bell Labs by Steve Bourne and was primarily the first UNIX shell. It was a default shell for Solaris OS. It is denoted by sh.

    However, Bourne shell has some disadvantages. It lacks the functionality of handling logical and arithmetic operations followed by not being able to recall the previous commands.

  2. The GNU Bourne Again Shell (bash): More popularly known as the Bash shell, the GNU Bourne-Again shell was designed to be compatible with the Bourne shell. It incorporates useful features from different types of shells in Linux such as the Korn shell and C shell.

    It allows us to automatically recall previously used commands and edit them with help of arrow keys, unlike the Bourne shell.

    The complete path name for the GNU Bourne-Again shell is /bin/bash. By default, it uses the prompt bash-VersionNumber# for the root user and bash-VersionNumber$ for the non-root users.

  3. The C Shell (csh): It has a similar syntax to the C programming language and can understand arithmetic operations. Another feature of C shell is "Aliases"

    The complete path name for the C shell is /bin/csh. By default, it uses the prompt hostname# for the root user and hostname% for the non-root users.

  4. The Korn Shell (ksh):

  5. The Z Shell (ssh):

What is shell scripting?

Shell scripting is the process of creating scripts or programs using shell commands and utilities that are interpreted by a shell. It automates repetitive tasks, facilitates system administration and application development, and enables users to interact with the operating system through a command-line interface.

Hands-On Tasks To Understand Shell Scripting Practically:

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

#!/bin/bash is called a shebang or a hashbang, and it's the first line of a Bash shell script that specifies the interpreter or the program that should be used to execute the script. In this case, #!/bin/bash specifies that the script should be run using the Bash shell.

Yes, you can also write #!/bin/sh instead, which specifies that the script should be run using the Bourne shell or a compatible shell. However, Bash is a superset of the Bourne shell and provides more advanced features and capabilities, so many scripts are written using Bash instead of sh.

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

#step 1: created an echo.sh file with the content below:

#step 2: Run the shell script using "./echo.sh"

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

Output :

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

Output:

#linux #linuxshellscripting #linuxbasics #linuxfundamentals #devops #unix #shellscript #scripting #bash #shellscripting #devopsengineer #community #trainwithshubham #90daysdevopschallenge