Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers

In this blog, I will start with the basics of Shell scripting along with Labs #DevOps #TrainwithShubham

What is Kernel?

The kernel is one of the most important parts of the Operating system and it is the main interface between Hardware and its processing. The name of the Linux Operating system's Kernel is also Linux. It has complete control over everything in the system

What is Shell?

A shell is a special user program that provides an interface for users to use operating system services. Shell accepts human readable commands from a user and converts them into something which Kernel can understand. It is a CLI (Command Language Interpreter) that executes commands read from input devices such as a keyboard or from files. The shell gets started when the user logs in or starts the terminal.

If we want to interact with Hardware in Linux, we will interact with Shell via a Terminal in which we tell Kernel about what function the hardware should perform and then the kernel passes the message to the hardware to perform that requested action.

What is Linux Shell Scripting?

Shell scripts are interactive which means when the user gives input, they accept the command from the user and execute them but in some cases, we want to execute a bunch of commands routinely so we have to type those commands each time in the terminal.

A shell can also take commands as input from a file, so we can write all these bunches of commands in a file and we can execute them in the shell to avoid this repetitive work. These files are called Shell Scripts and the extension for shell scripts ends at ".sh"

There are many reasons why we write Shell Scripts, a few of them are the following:

  • To avoid repetitive tasks and perform automation

  • System Monitoring

  • System Admins use shell scripting for routine backups

  • We can also add new functionality to the shell script at a later stage etc.

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

The shebang, #!/bin/bash when used in scripts is used to instruct the operating system to use bash as a command interpreter. Each of the systems has its own shells which the system will use to execute its own system scripts. This system shell can vary from OS to OS(most of the time it will be bash). Whereas, when the shebang, #!/bin/sh used in scripts instructs the internal system shell to start interpreting scripts.

Below are the Shebangs used for different purposes in shell scripts:

  • #!/bin/sh: It is used to execute the file using sh, which is a Bourne shell, or a compatible shell

  • #!/bin/csh**:** It is used to execute the file using csh, the C shell, or a compatible shell.

  • #!/usr/bin/perl -T**:** It is used to execute using Perl with the option for taint checks.

  • #!/usr/bin/php**:** It is used to execute the file using the PHP command-line interpreter.

  • #!/usr/bin/python -O: It is used to execute using Python with optimizations to code.

  • #!/usr/bin/ruby: It is used to execute using Ruby.

Write a Shell Script that prints I will complete the #90Days0fDevOps challenge

Below is an example of this script that prints the message "I will complete the #90DaysOfDevOps challenge" on the terminal.

and we have written a small piece of code in the "90DaysofDevops.sh" file as shown below:

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

and in "user_input.sh", I have written the following script:

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

Following is the piece of script in which we can see how we are comparing 2 numbers.

and here is the output:

I hope you have enjoyed reading my Blog on my #DevOps journey. If you like the blog, Please don't forget to reshare it so we all can learn and enjoy the World of DevOps and automation.

#Devops #TrainwithShubham