Skip to main content

Command Palette

Search for a command to run...

🐧 Dive into Shell Scripting: Unleashing the Power of Linux! 🌟

#Day4ofDevOpsBlog

Updated
β€’5 min read
🐧 Dive into Shell Scripting: Unleashing the Power of Linux! 🌟
T

Hey there! πŸ‘‹ 🌟 Welcome to my DevOps journey! 🌟

I'm Tanmaya Arora, an enthusiastic DevOps Engineer. Currently, on a learning adventure, I'm here to share my journey and Blogs about DevOps.

I believe in fostering a culture of resilience, transparency, and shared responsibility. Embracing agility and flexibility, in this adventure let's grow together in this vibrant DevOps space.

Join me in transforming software delivery through collaboration, innovation, and excellence! πŸš€πŸ”§πŸ’‘

🌐 Connect with me for friendly chats, group discussions, shared experiences and learning moments.

Welcome, DevOps enthusiasts! Today, we're diving into the fascinating world of Linux. We'll explore the kernel, different types of shells, and the power of shell scripting. This guide is packed with examples for a better understanding. Let's get started! πŸš€

πŸ”§ What is a Kernel?

The kernel is a computer program which is the heart (core component) of an operating system with complete control over the OS. It acts as a bridge between software applications and the hardware of a computer. The kernel manages system resources, allowing multiple programs to run simultaneously without interfering with each other. All important files required to run an OS successfully are there with the Kernel.

Key Functions of the Kernel:

  • Process Management: Handles the execution of processes.

  • Memory Management: Manages the system's memory.

  • Device Management: Controls hardware devices.

  • System Calls: Provides an interface for applications to interact with the hardware.

🐚 What is a Shell?

A shell is a special computer program that provides a command-line interface that allows users to interact with the operating system. It interprets the commands entered by the user, converts them to instructions (which are easy to understand for Kernel) and executes them.

In simple terms, Shell is a gateway between the Kernel and the Users.

Types of Shells:

  • Bourne Shell (sh): The original Unix shell.

  • Bash (Bourne Again Shell): An enhanced version of the Bourne Shell, widely used in Linux and industry as well.

  • C Shell (csh): Known for its C-like syntax.

  • Korn Shell (ksh): Combines features of the Bourne and C shells.

  • Z Shell (zsh): An extended Bourne shell with many improvements.

πŸ“œ What is Shell Scripting and Why Do We Need It?

Shell scripting is writing a series of commands in an order in a file that can be run by a Linux shell like Bash etc, to automate tasks, perform system administration tasks and make the interaction of users with operating system easier. It saves time and effort by executing repetitive tasks with a single script.

Benefits of Shell Scripting:

  • Automation: Automate repetitive tasks.

  • Efficiency: Execute complex commands quickly.

  • Flexibility: Easily modify and extend scripts.

  • Integration: Integrate different programs and utilities.

πŸ› οΈ Shell Scripting Basics

Variables

Variables store data that can be used later in the script. In scripting they can either be declared in the script with the value or can also be taken as input from the user.

Example:

#!/bin/bash -> This is called SHE BANG which basically is written in Script
#              to tell that which shell are we using. Here we are using BASH.
#----WAY 1-----
# In this case, we will simply run the script and no input from user required. 
name="Alice"
echo "Hello, $name!"

#----WAY 2------
# In this case we will take the input from the user during running the script
read -p "Enter the name:" name
echo "Hello, $name!"

Loops

Loops allow you to execute a block of code repeatedly.

Example:

#!/bin/bash
#---- WAY 1-----
for i in {1..5}
do
   echo "Iteration $i"
done

#----Way 2----
for (( i=1; i<=5; i++))
do
   echo "Iteration $i"
done

Conditional Statements

Conditional statements execute different commands based on conditions provided.

Example:

#!/bin/bash
if [ $1 -gt 10 ]
then
   echo "The number is greater than 10." #This statement is used for Printing
else
   echo "The number is 10 or less."
fi

Functions

A function in shell scripting is a reusable block of code that performs a specific task, which can be defined once and called multiple times. Functions help in making scripts modular and easier to manage.

Example:

#!/bin/bash
#Making a function called greet() which prints Hello Alice.
greet() {
    echo "Hello, $1!"
}

greet "Alice" #This is function calling

In this example, the greet function takes one argument (defined by $1 in the script) and prints a greeting message using that argument.

πŸ› οΈπŸ› οΈTasks:

Since we have learned the Basics of Shell Scripting, lets dive in to solve some small tasks to make yourself more confident!!

Ques 1: What is #!/bin/bash? Can we write #!/bin/sh as well?

Ans 1: #!/bin/bash is known as "shebang" or "hashbang" which is written as the first line of the script to tell the system about the shell are we using to write the script. It specifies the path to the interpreter that should be used to execute the script.

Yes, we can write #!/bin/sh as well if we are using the sh shell. It specifies that the script should be executed using the sh shell, which is typically a more minimal and traditional shell compared to bash.

Ques 2: Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.

Ans 2:

#!/bin/bash
echo "I will complete #90DaysofDevOps challenge."

Important:

1) We always make a file for writing the script with a .sh extension.

Example: vim myfirstshellscript.sh

In the above example, a file named myfirstshellscript is created and since it has a .sh extension so we recognize that it is a shell script.

2) To run a script we use the command as : ./<script_name>

Example: ./myfirstshellscript

Here myfirstshellscript is the name of the script, it can be different for your case.

Ques 3: Write a Shell Script that takes user input, input from arguments, and prints the variables.

Ans 3:

#!/bin/bash
read -p "Enter your name:" name #Taking input from user
# Taking input from arguments
a=$1
b=$2
#Printing the variables
echo "User Input is: $name"
echo "Arguments are: $a, $b"

Run the script as below:

./script.sh "Argument1" "Argument2" #These arguments will be provided by you.

Ques 4: Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.

Ans 4: My file name is compare_numbers.sh

#!/bin/bash
#Prompt the user to enter two numbers
read -p "Enter the 1st number:" num1
read -p "Enter the 2nd number:" num2
#Comparing the numbers
if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
elif [ $num2 -gt $num1 ]; then
    echo "$num2 is greater than $num1"
else 
    echo "$num1 is equal to $num2"
fi

Example Execution:

bashCopy code$ ./compare_numbers.sh
Enter the 1st number:
10
Enter the 2nd number:
20
20 is greater than 10

πŸš€ Conclusion

Understanding the kernel, shell, and shell scripting is fundamental to mastering Linux. With these basics, you're well on your way to automating tasks and making your work more efficient. Happy scripting! πŸ§πŸ’»

T

Welcome :) Glad you liked it .

A

Very informative and all the examples are on point. Thanks for sharing, tanmaya!

1