Linux Shell Scripting Basics Every DevOps Engineer Should Know
Table of contents
- What is Kernal?
- What is Shell?
- Linux Shell Scripting:
- What is #!/bin/bash? can we write #!/bin/sh as well?
- Shell Script that prints I will complete the #90DaysofDevOps challenge
- Shell Script to take user input, input from arguments, and print the variables.
- If else in Shell Scripting by comparing 2 numbers
What is Kernal?
The kernel is the core component of an operating system (OS), acting as a bridge between software applications and the computer's hardware. It manages system resources such as memory, CPU, and devices, and facilitates tasks like process management, memory management, and device communication.
There are different types of kernels:
Monolithic Kernel: The entire operating system runs within a single kernel space. Examples of such operating systems include Linux and Unix.
Microkernel: In kernel space, only essential components run, while others run in user space. This design aims for a smaller, more modular OS. Examples include Minix and QNX.
Hybrid Kernel: A combination of both monolithic and microkernel designs. Examples include Windows NT and macOS.
What is Shell?
A shell is a user interface that allows interaction with the operating system, enabling users to execute commands, run programs, and manage files.
The Command-Line Shell (CLI) is a text-based interface that interacts with the system using commands. It's commonly used by developers, system administrators, and power users for tasks like file management, system monitoring, and executing scripts. Some common command-line shells include Bash, Zsh, and PowerShell.
The Graphical Shell (GUI) is a visual interface with elements like windows, icons, and menus. It's what most users interact with on desktop environments like Windows Explorer or macOS Finder.
How a Shell Works:
The shell acts as an interpreter, translating user commands into instructions that the operating system can understand and communicates with the kernel to carry out tasks such as file manipulation, process control, or executing programs.
Linux Shell Scripting:
Linux Shell Scripting involves writing commands for the Linux shell to execute automatically. It's a text file containing commands for tasks like file manipulation, program execution, and system administration.
Key Points of Linux Shell Scripting:
Automation: Shell scripts automate repetitive tasks for complex jobs without manual intervention.
Scripting Languages: Bash is the most commonly used shell for scripting, but other shells like Zsh, Ksh, and Tcsh can also be used.
Syntax: Shell scripts use syntax similar to command-line instructions and include logic elements like loops, conditionals, and functions.
Execution: You can run a shell script by making the file executable and running it directly or by using the shell to interpret the script.
Example:
#!/bin/bash
# A simple shell script
echo "Hello, World!"
This script will print "Hello, World!" to the terminal when executed.
Shell scripting is widely used for system administration, task scheduling, software installation, and more.
What is #!/bin/bash? can we write #!/bin/sh as well?
The line #!/bin/bash
at the beginning of a shell script is known as a shebang (or hashbang). It specifies the path to the interpreter that will execute the script.
Example:
#!/bin/bash
# Bash script using arrays, a feature not supported by sh
arr=("one" "two" "three")
echo ${arr[0]}
In contrast, if you switch to #!/bin/sh
and try to use Bash-specific features like arrays, the script may fail.
You can write #!/bin/sh
, but it depends on whether your script requires Bash-specific features or needs to be more portable.
Shell Script that prints I will complete the #90DaysofDevOps challenge
Shell Script
#!/bin/bash
# A simple shell script to print a message
echo "I will complete #90DaysOfDevOps challenge"
Steps to Run the Script:
Open a text editor and paste the script above.
Save the file as
devops_
challenge.sh
(or any other name you like).Make the script executable by running:
chmod +x devops_challenge.sh
Run the script:
./devops_challenge.sh
The script will print:
I will complete #90DaysOfDevOps challenge
Shell Script to take user input, input from arguments, and print the variables.
Shell Script
#!/bin/bash
# A script to take user input and arguments, then print the values
# Taking input from the user
echo "Please enter your name:"
read user_name
# Taking input from command-line arguments
arg1=$1
arg2=$2
# Printing the values
echo "User Input: Your name is $user_name"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
Steps to Run the Script:
Open a text editor and paste the script above.
Save the file as
input_
script.sh
.Make the script executable by running:
chmod +x input_script.sh
Run the script with arguments:
./input_script.sh arg1_value arg2_value
Example:
./input_script.sh DevOps Linux
Output Example:
Please enter your name:
John
User Input: Your name is John
Argument 1: DevOps
Argument 2: Linux
This script:
Prompts the user to enter their name.
Takes two arguments from the command line.
Prints the entered name and the provided arguments.
If else in Shell Scripting by comparing 2 numbers
Shell Script
#!/bin/bash
# A script to compare two numbers
# Taking two numbers as input from the user
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Comparing the numbers using if-else
if [ $num1 -gt $num2 ]; then
echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
echo "$num1 is less than $num2"
else
echo "$num1 is equal to $num2"
fi
Steps to Run the Script:
Open a text editor and paste the script above.
Save the file as
compare_
numbers.sh
.Make the script executable by running:
chmod +x compare_numbers.sh
Run the script:
./compare_numbers.sh
Example Output:
Enter the first number:
5
Enter the second number:
10
5 is less than 10
In this script:
The user enters two numbers.
The script compares them using the
if-else
construct.It prints whether the first number is greater than, less than, or equal to the second number.
Subscribe to my newsletter
Read articles from Vanshika Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vanshika Sharma
Vanshika Sharma
I am currently a B.Tech student pursuing Computer Science with a specialization in Data Science at I.T.S Engineering College. I am always excited to learn and explore new things to increase my knowledge. I have good knowledge of programming languages such as C, Python, Java, and web development.