"Triumph over Shell Scripting for DevOps - From Basics to Practical Examples"
Table of contents
- What is Kernel?
- What is Shell?
- What is shell scripting?
- What is #!/bin/bash?
- Write a Shell Script that 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
- Conclusion
What is Kernel?
In Linux, the kernel is like the brain of the operating system. It acts as the intermediary between the hardware and the software, making it possible for us to interact with our computers and perform various tasks.
The kernel handles tasks such as memory management, process scheduling, device communication, and file system access. It ensures that different software applications can work together and use the computer's resources efficiently.
What is Shell?
In Linux, the shell is a command-line interface that allows users to interact with the operating system through text commands. It's like a translator between you and the Linux kernel.๐ป
The shell takes your commands and sends them to the kernel for execution. It provides a way to navigate the file system, run programs, manage processes, and perform various tasks. You can think of it as a powerful tool that allows you to control your Linux system by typing commands instead of using a graphical interface. โจ๏ธโจ
There are different shell options available in Linux, such as Bash (Bourne Again SHell), which is the most common one. Each shell may have its own features and capabilities, but they all share the same purpose of facilitating interaction with the operating system through commands.
What is shell scripting?
Shell scripting for DevOps enables automation of deployment, configuration management, CI/CD pipelines, monitoring, logging, and performance optimization tasks. Shell scripting languages, such as Bash, are used for automating tasks in DevOps workflows.
Linux Shell Scripting refers to the process of writing and executing scripts using a shell in Linux. A shell script is a sequence of commands that perform various operations in the Linux environment. ๐๐๐ป
What is #!/bin/bash
?
The line #!/bin/bash
at the beginning of a shell script is called a shebang or hashbang. It specifies the interpreter that should be used to execute the script.
In the case of #!/bin/bash
, it indicates that the script should be interpreted and executed using the Bash shell, which is a popular and widely used shell in Linux and other Unix-like operating systems.
Can we write #!/bin/sh
as well ?
Yes, you can use #!/bin/sh
as the shebang line in a shell script.
When you specify #!/bin/sh
, it indicates that the script should be interpreted and executed using the system's default shell, often referred to as the "Bourne shell" or "sh." The Bourne shell is a widely supported shell that provides basic shell functionalities.
However, it's important to note that using #!/bin/sh
may limit you to the features and functionalities provided by the Bourne shell. If you require advanced features specific to a particular shell, such as Bash, you should use the corresponding shebang line (#!/bin/bash
in the case of Bash).
Write a Shell Script that prints I will complete #90DaysOofDevOps challenge
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
To run this script, follow these steps:
Open a text editor and paste the above code into a new file.
Save the file with a ".sh" extension, for example, "shell_scripting.sh".
Open a terminal and navigate to the directory where the script is saved.
Make the script executable by running the command:
chmod +x
shell_scripting.sh
.Execute the script by running the command:
./
shell_scripting.sh
.
After executing the script, you will see the desired statement printed in the terminal: "I will complete #90DaysOfDevOps challenge".
Write a Shell Script to take user input, input from arguments and print the variables.
#!/bin/bash
# Take user input
echo "Enter your name:"
read name
# Access input from arguments
arg1=$1
arg2=$2
# Print the variables
echo "User input: $name"
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
To use this script, follow these steps:
Open a text editor and paste the above code into a new file.
Save the file with a ".sh" extension, for example, "input_script.sh".
Open a terminal and navigate to the directory where the script is saved.
Make the script executable by running the command:
chmod +x input_script.sh
.Execute the script by running the command with arguments:
./input_script.sh argument1 argument2
.
When you run the script, it will prompt you to enter your name. After providing the input, it will access the input provided as arguments (argument1 and argument2). Finally, it will print the variables showing the user input and arguments.
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
# Define the two numbers
num1=10
num2=5
# Compare the numbers
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
Conclusion
We have embarked on a hands-on journey in the world of Linux, gaining practical experience and expanding our skills.
The journey does not end here. Linux is a big ecosystem, and there is always more to explore and learn. By continually practicing, experimenting, and staying curious, we can further deepen our understanding of Linux and uncover new possibilities.