Basic Linux Shell Scripting for DevOps Engineers.

Basic Linux Shell Scripting for DevOps Engineers.

Day4 Task of #90daysofdevops

What is Kernel?

The kernel is a central component of an operating system that manages the operations of the computer and hardware. It manages operations of memory and CPU time. It is a core component of an operating system. Kernel acts as a bridge between applications and data processing performed at the hardware level using inter-process communication and system calls.

Objectives of Kernel :

  • To establish communication between user-level applications and hardware.

  • To decide the state of incoming processes.

  • To control disk management.

  • To control memory management.

  • To control task management.

What is Shell?

The shell is a program that takes commands from the keyboard and gives them to the operating system to perform a certain task. Whenever a user logs in to the system or opens a console window, the kernel runs a new shell instance.

In other words, 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 is Linux Shell Scripting?

Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts.

Shell can also take commands from an executable file, which has a line of commands written inside the file that we can create to avoid repetitive tasks. These files are called Shell Scripts or Shell Programs. Each shell script is saved with a .sh file extension e.g. execute_a_task.sh

A shell script comprises the following elements –

  • Shell Keywords – if, else, break, read, etc.

  • Shell commands – cd, ls, echo, pwd, touch etc.

  • Functions

  • Control flow – if..then..else, case and shell loops etc.

Shell Scripting for DevOps

For a DevOps Engineer, it is very important to know shell scripting which can save them so much time doing certain repetitive tasks by writing the simple script and executing it when in need. Suppose you need to create a backup of your data every day, you need not do it manually every time you want to. Despite you can create a script for the same which you can execute anytime you want to create your backup. Creating a backup is just one example of the many tasks you can do using shell scripting.

Shell scripts can be used for the following applications:

  • Automation of the code compiling process.

  • Run a program or create an environment for programming.

  • Complete batch processing and file manipulation.

  • Integrate existing programs.

  • Perform routine backups.

  • Keeping an eye on a system.

  • System administration tasks

  • Creating, maintaining, and implementing system boot scripts.

  • Creating compressed files

What is "#!/bin/bash"?

The operator #! is known as the shebang operator, which is used to define the shell in which the script is going to run.

Hence, when we write #!/bin/bash

Then, that would mean the script will run in a Bash shell.

And, when we write #!/bin/sh

Then, that would mean the script will run in a Bourne shell.

Let's write a simple shell script

Step 1: Create a directory "myscripts".

mkdir myscripts

Step 2: Change directory to myscripts.

cd myscripts

Step 3: Create a new file with .sh extension

vim script1.sh

Step 4: Write your command

Step 5: Change the file type to an executable file by using chmod command.

chmod 764 script1.sh

Step 6: Execute your file

./script1.sh

Now, Let's do some fun with what we learnt today.

Let's write a Shell Script to take user input.

Results:

Congrats! You've created your Virtual Assistant!

Now, Let's create an example of If else in Shell Scripting

Results: