Day 4: Linux Shell Scripting

Day 4: Linux Shell Scripting

So, this is the Day 4 of the #90DaysOfDevOps challenge. In previous blogs I covered Getting Started With DevOps, What is Linux, Linux Basic Commands, and much more. You can check out the blogs and take part in also #90DaysOfDevOps challenge.

In today's blog, we are discussing about :

  • What is Kernel?

  • What is Shell?

  • What is Linux Shell Scripting?

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

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

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

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

Let's start the blog !!

What is Kernel?

The kernel is the core of Linux operating system that manages everything in the system like:

  • File Management

  • Device Management

  • I/O (Input/Output) Management

  • Process Management etc.

What is Shell?

It is a special user program that provides an interface for users to interact with other OS services. It takes input from the user and converts it into something that the kernel can understand or we can say it is a language interpreter that takes input from the keyboard that executes commands and gives the output.

There are two types of shell are :

  1. Command Line Shell

  2. Graphical Shell

  • Command Line Shell: It is basically a terminal that takes commands as input like a cat, ls, history, etc, and prints the output. In Linux, and macOS this is called Terminal and in Windows, it is called Command Prompt.

  • Graphical Shell: It is a GUI where you can do the operations of closing the window, and resizing the window by just simply dragging the mouse or clicking the mouse.

Shell Scripting:

Now you all know about what is shell. Shell scripting means writing all the commands in one file as a script and executing at just once. It reduces the time for executing the commands repeatedly. The extension of Shell script files ends with a .sh extension.

There are many scripting languages in the market like JavaScript, PHP, Perl, Python, etc. Python is the best scripting language to automate anything by writing just a few lines of code that's why DevOps engineers prefer mostly Python that fits into the space.

The scripting file starts with #!/bin/bash .

What is #!/bin/bash ?

The first line of Shell scripting is #!/bin/bash which is known as shebang ( '#' symbol means hash and the '!' symbol means bang). This will tell the system that this is Bourne Again Shell (sh).

We can write also #!/bin/sh in place of #!/bin/bash, the difference is just in shells 'sh' means Bourne Shell and 'bash' means Bourne Again Shell.

Example 1:

Let us understand shell scripting with an example where we write the script in which it prints I will complete #90DaysOofDevOps challenge

NOTE: Try to make all script files in the script folder.

Step 1: Create the file by touch command with .sh extension.

touch challenge.sh

Step 2: Open the file by executing the vi command (Press 'i' to insert )with the script file and print the line by executing the echo command.

vi challenge.sh
#!/bin/bash
echo "I will complete #90DaysOofDevOps challenge"
:wq!

Step3: Run this script by executing :

sh challenge.sh

It will print the echo line like this:

Example 2:

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

Here in the first line, I give the variable NAME to value in " ".

In the second line, it will print the echo line. $ is used to access the value that is stored in the variable.

In the third line, the read command will take the input from the user.

In the fourth line, it will simply echo the line after taking the input from the user for the AGE variable, and then it will print the echo line.

Example 3:

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

If you are familiar with programming languages then you definitely know about if-else statements. It has a similar syntax to other programming languages but here after the if condition 'then' line is there and at last 'fi' is there.

So, let's understand this program.

In the first two lines read will take input from users for A and B.

In the third line, if-else syntax begins if statement will check the condition that the numbers are equal or not, if they are not equal then it will print the other else statement i.e "The numbers are not matched."

That's it about Shell Scripting, about Day 4 task.

Hope you all will learn something new from this blog and if you have any queries feel free to ask and share with other Linux and DevOps folks.

THANK YOU :)

Connect with me on Twitter and follow me for amazing Linux and DevOps upcoming interesting blogs.