Know about Kernel & Shell Scripting

Know about Kernel & Shell Scripting

What is Kernal?

It's nothing but a simple program that is to be loaded into memory when a computer starts up, and it remains in memory throughout the computer's operation. It provides essential services to other programs running on the system, such as managing process scheduling, memory allocation, and device drivers.

In short, " the kernel is the essential component of an operating system that enables the communication between software and hardware components and provides a platform for software to run on".

Shell

A shell is a program that serves as a command-line interface between the user and the operating system. It allows users to interact with the system by typing commands and receiving output in response. It presents on various CLI Os like Linux, and MacOS, as well as in Windows too.

The shell provides a way to interact with the operating system and execute various commands and scripts. It can also be used to automate repetitive tasks by creating scripts that execute a series of commands automatically.

Let's take an example of a shell script that is very basic to understand -

Make one directory in instance and from that create one bash file and mention some scripts inside it let's suppose:

vim shell1.sh #make sure becomming a shell file we need to add .sh after filename therefore it will execute as bash script
#!/bin/bash

# This is a comment
echo "I will complete 90DaysOfDevops Challenge"

Now, what you see here let's brief it....!

  • #!/bin/bash specifies the interpreter that will be used to execute the script. In this case, we're using the Bash shell.

  • # This is a comment in the script. Anything after the # character is ignored by the interpreter.

  • echo "I will complete 90DaysofDevops Challenge" is a command that prints the text "Hello, world!" to the console.

Now if we want to run the bash script or shell script we do -

  • To run our first script

      $ bash hello.sh
      I will complete 90DaysofDevops Challenge
    

    In such cases, we as DevOps engineers used to execute the bash file in another way i.e.,

Check the file permissions if in case "x" i.e execute is not present then run simple command i.e

$ chmod 777 hello.sh #to allow whole "rwx" permission in this file to all user,usergroup & others
$ ./hello.sh #this is the cmd to execute the script 
I will complete 90DaysofDevOps Challenge #console output

Script using condition statements

Here's the basic if-else condition bash script :

#!/bin/bash

num1=10
num2=20

if [ $num1 -gt $num2 ]; then
  echo "$num1 is greater than $num2"
else
  echo "$num1 is less than or equal to $num2"
fi

Now let's elaborate on the whole thing that is mentioned above-

  • num1 and num2 are variables that hold the values of the two numbers we want to compare.

  • The if the statement checks if $num1 is greater than $num2 using the -gt operator. If the condition is true, the first block of code (i.e., echo "$num1 is greater than $num2") is executed. If the condition is false, the second block of code (i.e., echo "$num1 is less than or equal to $num2") is executed.

  • The fi statement marks the end of the if block.

Run the script:

$ ./compare.sh
10 is less than or equal to 20

In this case, the if the condition is false, so the second block of code is executed. If the values change of $num1 and $num2 , could see a different output based on the comparison.

Thanks for reading this blog till now...!

Will meet soon with a new topic till then follow and keep learning and keep sharing :)