Day 4 - What is Kernal?
A kernel is a crucial part of a computer's operating system (OS) that serves as a bridge between the OS and the hardware. It plays a central role in managing key functions such as system calls, disk operations, and memory utilization.
The kernel is the first component that loads into memory during the boot process and remains active until the system is shut down. Essentially, it is the core element necessary for the proper functioning of an OS on hardware. The kernel is responsible for coordinating CPU cores, allocating memory, and overseeing various hardware-related tasks.
There are two main types of kernels:
Monolithic Kernel:
The most widely used type found in operating systems like Linux, Unix, Windows, and mobile phones.
Manages diverse functions, including memory, disk management, RAM allocation, CPU clock speed, and battery management.
Contains a complex and extensive codebase.
Microkernel:
A lightweight kernel that performs a limited set of tasks, such as virtual memory management, thread handling, and scheduling.
Focuses on keeping its functions minimal and modular.
What is Shell?
A shell is a common interface that allows users to interact with the computer's hardware. The kernel, which is human-readable, acts as an intermediary, translating user commands into a format that the kernel can understand; this translation process is known as interpretation.
The shell serves as a bridge between the user and the kernel, with the kernel handling low-level tasks. There are two main types of shells:
Command Line Shell:
In this type, users input commands like "ls," "cd," or "mkdir," commonly used in Unix and Linux systems. Windows users may be familiar with it as the command prompt.
The interaction involves typing commands, and the results are displayed after hitting the enter key.
Graphical Shell:
This shell operates with a graphical user interface (GUI), allowing users to interact with it using a mouse instead of solely relying on a keyboard.
Users can click on icons to interact with the shell. An example of an operating system that incorporates both a graphical and command line shell is Ubuntu.
What is Linux Shell Scripting?
Shell scripting is a crucial component in automating processes on Linux systems. It involves a sequence of Linux commands that execute in a specified order when a command is initiated.
The primary purpose of shell scripting is to eliminate repetitive tasks in Linux, ultimately saving a significant amount of time. For instance, when creating a large number of files or folders, shell scripts can be employed to streamline the process efficiently.
Shell scripting utilizes a high-level language that is easily understandable. Scripts typically have a ".sh" extension, and before running them, executable permissions must be granted. This ensures that the Linux shell recognizes and executes the script properly.
In the Linux environment, the shell provides a program aptly named the shell, which plays a pivotal role in executing these scripts. Shell scripting empowers users to automate complex or repetitive tasks, contributing to enhanced efficiency and productivity in Linux-based systems.
What is
#!/bin/bash?
can we write#!/bin/sh
as well?
The #!/bin/bash
(shebang) is a special line at the beginning of a shell script that tells the system which interpreter should be used to execute the script. In this case, it specifies the Bash shell.
Yes, you can use #!/bin/sh
as well. This shebang directs the script to be executed by the system's default shell, which might be Bash or another compatible shell like Dash.
Let's break it down:
#!/bin/bash:
This shebang indicates that the script should be interpreted and executed using the Bash shell.
Example:
#!/bin/bash echo "Hello, world!"
#!/bin/sh:
This shebang is more general, as it uses the system's default shell (commonly Bash or Dash).
Example:
#!/bin/sh echo "Hello, world!"
In many cases, using #!/bin/bash
is preferred if your script specifically requires Bash features. However, if your script uses only basic shell features that are common across various shells, #!/bin/sh
provides more portability. The choice depends on your script's requirements and the compatibility you need with different shell environments.
Write a Shell Script which prints
I will complete #90DaysOofDevOps challenge
Here's a simple shell script that prints the statement "I will complete #90DaysOfDevOps challenge":
#!/bin/bash echo "I will complete #90DaysOfDevOps challenge"
Save this script in a file, let's say
challenge.sh
. To run the script, make it executable with the following command:chmod 700 challenge.sh
Then, execute the script:
./challenge.sh
OR
bash challange.sh
This will output:
I will complete #90DaysOfDevOps challenge
Write a Shell Script to take user input, input from arguments and print the variables.
Certainly! Here's a simple shell script that takes input from the user and command-line arguments, then prints the variables:
#!/bin/bash # Taking user input echo -n "Enter your name: " read userName # Taking input from command-line arguments scriptArgument=$1 # Printing variables echo "User input: $userName" echo "Script argument: $scriptArgument"
Save this script in a file, let's say
input_
script.sh
. To run the script, make it executable:chmod +x input_script.sh
Now, you can run the script in two ways:
Providing command-line arguments:
./input_script.sh ArgumentValue
Output:
User input: Script argument: ArgumentValue
Running the script and entering user input when prompted:
./input_script.sh
Output:
Enter your name: [Enter your name] User input: [Your entered name] Script argument:
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
# Assigning values to variables
number1=5
number2=8
# Comparing the two numbers
if [ $number1 -eq $number2 ]; then
echo "The numbers are equal."
elif [ $number1 -lt $number2 ]; then
echo "Number 1 is less than Number 2."
else
echo "Number 1 is greater than Number 2."
fi
In this example:
We have two variables,
number1
andnumber2
, each assigned a numerical value.The script uses the
if-elif-else-fi
structure to compare the two numbers.The
-eq
operator checks if the numbers are equal,-lt
checks if the first number is less than the second, and the finalelse
statement covers the case where the first number is greater than the second.
When you run this script, it will output:
Number 1 is less than Number 2.
Feel free to modify the values of number1
and number2
to observe different outcomes based on the comparison.