Table of contents
- 🫀What is Kernel
- 💻What is Shell
- 📜What is Linux Shell Scripting?💻
- Tasks
- Explain in your own words and examples, what is Shell Scripting for DevOps.
- What is#!/bin/bash?can we write#!/bin/shas well
- Write a Shell Script which 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
🫀What is Kernel
In Linux, the kernel is the core component of the operating system. It acts as an intermediary between the hardware and the software, managing system resources, providing essential services, and enabling communication between hardware and software components.
The kernel handles tasks such as process management, memory management, device management, and file system management.
For instance, in a Linux system, the kernel manages memory allocation, and scheduling of processes, and interacts with hardware devices like the CPU, memory, disks, and accessories. It also facilitates communication between user applications and hardware by providing a standardized interface through system calls.
Here's a simple example of how the kernel operates:
Process Management: The kernel schedules tasks, allocates CPU time to various processes, and manages their execution. It ensures fair access to resources and handles multitasking.
Memory Management: It manages system memory, allocating and deallocating memory for running processes, swapping data to disk when needed, and handling memory protection.
Device Management: The kernel interacts with hardware devices such as printers, keyboards, and network cards. It handles device drivers, allowing applications to communicate with hardware without needing to know specific hardware details.
💻What is Shell
In Linux, a shell is a command-line interface that allows users to interact with the operating system by entering commands. It's both an interface and a scripting language that interprets user inputs and executes commands.
One of the most commonly used shells in Linux is the Bash shell (Bourne Again SHell). It provides features such as command-line editing, history, and job control.
Here's a perfect example:
Command Execution: Users can enter commands directly into the shell prompt. For instance, typing
ls
and hitting Enter will list the contents of the current directory.Scripting: Shells allow users to write scripts—sequences of commands—to automate tasks. For instance, a simple Bash script to greet someone might look like this:
Bash
#!/bin/bash
echo "Hello, how are you?"
Saving this as a file (e.g., greeting.sh
) and give the file permission using chmod 777 and
executing it with ./
greeting.sh
will display Hello, how are you?
in the terminal.
- Variables and Control Structures: Shells support variables and control structures like loops and conditionals, allowing for more complex scripting. For example:
Bash
#!/bin/bash
for i in {1..5}
do
echo "Counting: $i"
done
This script will count from 1 to 5 and display each number.
📜What is Linux Shell Scripting?💻
Linux shell scripting refers to writing scripts or programs using shell commands and syntax to automate tasks, perform system administration, or execute a series of commands in sequence.
Shell scripts in Linux are text files containing a series of commands that the shell interprets and executes. These scripts leverage the capabilities of the shell to perform various tasks, such as file manipulation, system configuration, process management, and automation of repetitive tasks.
Key aspects of Linux shell scripting include:
Automation: Shell scripts automate tasks that might otherwise require manual execution of multiple commands.
Portability: Shell scripts can often run on different Linux distributions and Unix-like operating systems with minor adjustments.
For instance, a shell script could automate the backup of files, installation of software packages, or monitoring system performance.
The most common shell used for scripting in Linux is the Bash shell (Bourne Again Shell), but other shells like sh, csh, ksh, and zsh also support scripting.
Shell scripting is a powerful tool for both beginners and experienced Linux users, providing a flexible and efficient way to interact with the system and perform a wide range of tasks through automation.
Tasks
Explain in your own words and examples, what is Shell Scripting for DevOps.
Shell scripting in DevOps involves creating automated sequences of commands for managing and streamlining software development and operations tasks.
These scripts use shell languages like Bash to interact with operating systems, configure environments, deploy applications, and perform routine tasks.
For instance, a shell script could automate the deployment of a web application by pulling code from a repository, setting up the environment, and starting the server. This enables DevOps teams to save time, reduce errors, and ensure consistency in their workflows.
What is#!/bin/bash?
can we write#!/bin/sh
as well
#!/bin/bash
is called a "shebang" and appears at the beginning of a shell script. It tells the operating system which interpreter to use for executing the script, in this case, the Bash shell.Yes, you can use
#!/bin/sh
as well. It refers to the system's default shell interpreter. However, keep in mind that the functionalities might be limited compared to Bash. So, choosing between them depends on your script's requirements and desired features.
Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
To create and run this script, follow these steps:
Open a text editor (e.g.,
nano
,vim
, or a code editor like VSCode).Copy and paste the script into the editor.
Save the file with a
.sh
extension, for example,challenge_
script.sh
.Give permission to that file using
chmod 777 challenge_
script.sh
Open your terminal and navigate to the directory where the script is saved.
Run the script using the command:
./challenge_
script.sh
.
Bash
# vim challenge_script.sh
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
Output:
Write a Shell Script to take user input, input from arguments and print the variables
To create and run this script, follow these steps:
Open a text editor (e.g.,
nano
,vim
, or a code editor like VSCode).Copy and paste the script into the editor.
Save the file with a
.sh
extension, for example,input_
script.sh
.Give permission to that file using
chmod 777 input_
script.sh
Open your terminal and navigate to the directory where the script is saved.
Run the script using the command:
./input_
script.sh
.
Bash
# vim input_script.sh
#!/bin/bash
# Taking user input
echo -n "Enter your name: "
read username
# Using command-line argument
arg_value=$1
# Printing variables
echo "User input: $username"
echo "Argument input: $arg_value"
The script will display the provided user input and argument value:
Write an Example of If else in Shell Scripting by comparing 2 numbers
In this example, the script compares
num1
andnum2
using if-else statements. It checks whether they are equal or which one is greater. The-eq
flag is used for equality and-gt
for greater than. The script then prints the appropriate message based on the comparison.To create and run this script, follow these steps:
Open a text editor (e.g.,
nano
,vim
, or a code editor like VSCode).Copy and paste the script into the editor.
Save the file with a
.sh
extension, for example,ifelse_
script.sh
.Give permission to that file using
chmod 777 ifelse_
script.sh
Open your terminal and navigate to the directory where the script is saved.
Run the script using the command:
./ifelse_
script.sh
.
Bash
#!/bin/bash
num1=10
num2=20
if [ "$num1" -eq "$num2" ]; then
echo "Both numbers are equal"
elif [ "$num1" -gt "$num2" ]; then
echo "$num1 is greater than $num2"
else
echo "$num2 is greater than $num1"
fi
Explanation of this script:
#!/bin/bash
: This line is called the shebang. It specifies the interpreter that will execute the script.num1=10
andnum2=20
: These lines assign values to two variables,num1
andnum2
.num1
is assigned the value10
, andnum2
is assigned the value20
.if [ "$num1" -eq "$num2" ]; then
: This line begins anif
statement. It checks if the value ofnum1
is equal to the value ofnum2
using the-eq
operator for numeric comparison. The square brackets[ ]
are used for the test command in shell scripting.echo "Both numbers are equal"
: If the condition in theif
statement is true (ifnum1
is equal tonum2
), this line will be executed, printing the message "Both numbers are equal" to the terminal.elif [ "$num1" -gt "$num2" ]; then
: Theelif
(else if) statement allows for another condition to be checked if the first condition is false. This line checks ifnum1
is greater thannum2
using the-gt
(greater than) operator.echo "$num1 is greater than $num2"
: If theelif
condition is true (ifnum1
is greater thannum2
), this line will be executed, printing the message "$num1 is greater than $num2" to the terminal.else
: If none of the above conditions are true, theelse
block is executed.echo "$num2 is greater than $num1"
: In this case, if neithernum1
is equal tonum2
nornum1
is greater thannum2
, this line will be executed, printing the message "$num2 is greater than $num1" to the terminal.fi
: This line marks the end of theif
statement, closing the conditional block.
When you run the script, it will output:
Happy Learning
Thanks For Reading! :)
-DevOpsParthu💝💥