Day 4 - Basic Linux Shell Scripting for DevOps Engineers

Introduction:
Welcome back to the 90DaysOfDevOps Challenge, where each day brings us closer to mastering the art of DevOps. On Day 4, we're diving into the world of Linux shell scripting—a crucial skill for any DevOps engineer. But before we jump into scripting, let's start by understanding two fundamental concepts: the Kernel and the Shell.
What is Kernel?
The kernel is the heart of an operating system, possessing complete control over every aspect of the system. It's like the conductor of an orchestra, directing the hardware, processes, and resources. Without the kernel, your computer wouldn't be able to function.
What is Shell?
Think of the shell as the bridge between you and the kernel. It's a user program that provides a human-friendly interface for interacting with the operating system. The shell accepts your commands in plain language, making it easy to communicate with the kernel. The shell is your interpreter whether you're typing on a keyboard or running commands from a file.

What is Linux Shell Scripting?
Now, let's get into the heart of Day 4: Linux shell scripting. A shell script is a computer program designed to be run by a Linux shell, such as Bash. These scripts are written in a scripting language that the sNowhell can understand. Shell scripts perform versatile tasks like file manipulation, program execution, and text printing.
Tasks for Today:
Explain in your own words and examples, what is Shell Scripting for DevOps.
Shell Script is a program that is run by a UNIX/LINUX shell. It is a file that contains a series of commands that are executed sequentially as if they were entered on the command line interface (CLI) or terminal. Now let's see why Shell Script is so important.
Automation of Repetitive task
In DevOps, there are many routine tasks, such as deploying code, configuring servers, or managing containers. Shell scripts enable automation of these tasks, reducing the need for manual interventionConsistency and Reliability
Shell scripts ensure consistency in task execution. When tasks are automated through scripts, they are performed in the same way every time, eliminating human errors and reducing the risk of misconfiguration.
Time and Resource Efficiency
By automating tasks, DevOps teams save valuable time and resources.
Integration and Orchestration
In a DevOps pipeline, various tools and systems need to work together seamlessly. Shell scripts serve as glue code, enabling the integration and orchestration of different tools and processes.
Logging and Monitoring
Shell scripts can be used to set up logging and monitoring systems. They can regularly check system health, generate log files, and send alerts when issues arise.
In simple terms, shell scripting gives the power to automate, manage, and optimize the entire software development lifecycle and infrastructure management, making processes more efficient, reliable, and scalable.
What #!/bin/bash? can we write #!/bin/sh as well?
The main difference between #!/bin/sh and #!/bin/bash is the shell that is used to interpret the script. #!/bin/sh refers to the system shell, which can vary depending on the system, while #!/bin/bash specifically using the bash shell. Understanding this difference is important for writing scripts that are compatible and portable across different systems.
Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
#!/bin/bash
# This is a simple shell script to print a message
echo " I will complete #90DaysOfDevops Challenge !!!!"
To use this script:
Create a new file, for example,
challenge.shand open it in a text editor.Copy and paste the script into the file.
Save the file.
Make the file executable using the following command:
chmod 750 challenge.sh
- Run the script using:
./challenge.sh
You will see the message printed to the terminal.

Write a Shell Script to take user input, input from arguments, and print the variables.
#!/bin/bash # Method 1: Capture input from the user echo "Enter your name: " read userName # Method 2: Capture input from command-line arguments arg1=$1 # Method 3: Set variables directly variable1="Hello" variable2="World" # Print the values echo " User Input - Hello, $userName!" echo " Command-line Arguments - arg1: $arg1" echo " Directly Set Variables - $variable1 $variable2"To use this script:
Create a new file, for example,
user.sh, and open it in a text editor.Copy and paste the script into the file.
Save the file.
Make the file executable using the following command:
chmod 766 user.sh
- Run the script using:
./user.sh
You can provide command-line arguments when running the script, like this:
./user.sh Saurav
The script will capture the user's name from the terminal, the command-line arguments, and use the variables set directly to display the values as shown in the echo statements.
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
# Prompt the user to enter the first Number
echo "Enter the First Number:"
read number1
# Prompt the user to enter the second Number
echo "Enter the Second Number:"
read number2
# Compare the numbers
if [ $number1 -eq $number2 ]; then
echo "$number1 is equal to $number2"
elif [ $number1 -lt $number2 ]; then
echo "$number1 is less than $number2"
else
echo "$number1 is greater than $number2"
fi
In this script:
We use the
readcommand to take input from the user for bothnumber1andnumber2.We then use an
if-elif-elsestructure to compare the two numbers using the-eq,-lt, and-gtoperators.Depending on the comparison result, the script prints an appropriate message.

Conclusion:
Day 4 has introduced us to the world of Linux shell scripting, an essential skill for DevOps engineers. We've explored the kernel, the shell, and the significance of scripting in the DevOps context. With practical tasks, we've begun our journey into writing shell scripts, capturing input, and using conditional statements. Stay tuned for Day 5 as we continue to unravel the mysteries of DevOps. If you have questions or insights to share, feel free to leave a comment. Together, we'll conquer the #90DaysOfDevOps challenge!
#DevOpsScripting #LinuxShell #90DaysDevOps #Day04-90DaysDevOps


