Basic Shell Scripting for Devops
Table of contents
- ✨What is Kernel🧠?
- ✨What is Shell🐚?
- ✨What is Linux Shell Scripting💻?
- ✨Why do we need shell scripts🤔**?**
- ✨What is #!/bin/bash? Can we write #!/bin/sh as well? 🤔
- ✨Command to list all the shells type📋:
- ✨Location of the bash:
- ✨Write a Shell Script that prints I will complete #90DaysOofDevOps challenge
- Step 1: 📄 Create the file with .sh extension.
- Step 2:🔒 Give the executable permission to the file.
- Step 3: Write the script using the vim editor. ✍️
- Step 4:To run the script 🏃♂️📜
- ✨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 DevOps, the kernel 🧠 refers to the core component of an operating system that directly interacts with the hardware 🖥️ and manages essential resources, such as CPU 💻, memory 🧠, and devices 📱.
It's like the brain of the system! 🧠💡.
It plays a crucial role in ensuring the proper functioning of the operating system and enables various applications and services to run efficiently 🛠️🚀.
✨What is Shell🐚?
Command-line-interface. 💻🔧
Allow user to interact with the hardware. 💻🛠️
It is a program that interprets user input and communicates with the Linux kernel to perform various tasks. 📜💬💻🚀
The shell provides a text-based environment where users can run commands to manage files 📁, directories 📂, processes 🔄, and execute programs 🚀. It also supports scripting 📜, allowing users to create and run scripts to automate repetitive tasks 🤖.
✨What is Linux Shell Scripting💻?
Shell scripting for DevOps involves writing scripts in shell programming languages (like Bash, PowerShell, etc.) to automate various tasks in the DevOps workflow. These tasks can include automating software builds, deployments, server configurations, cloud infrastructure provisioning, monitoring, and more.🚀
Shell scripts streamline repetitive tasks, reduce manual errors, and help maintain consistency in the software delivery process. They enable DevOps teams to efficiently manage and automate their infrastructure, deployment pipelines, and application workflows. With shell scripting, DevOps practitioners can achieve faster, more reliable, and scalable software delivery, making their processes more efficient and effective.💻🤖🔧
✨Why do we need shell scripts🤔**?**
There are many reasons to write shell scripts:
🚀 Shell scripts automate repetitive tasks in DevOps and system administration.
🎯 They ensure consistency and accuracy in task execution.
💡 Shell scripts improve efficiency and overall productivity.
🔧 They offer flexibility to handle various tasks and configurations.
🛡️ Shell scripts reduce human errors and provide reliable and scalable automation.
✨What is #!/bin/bash? Can we write #!/bin/sh as well? 🤔
📜 #!/bin/bash
is called the "shebang" or "hashbang" and is used at the beginning of a shell script to specify the interpreter that should be used to execute the script. In this case, #!/bin/bash
specifies that the script should be interpreted and executed using the Bash shell.
👉 Yes, you can write #!/bin/sh
as well. The sh
in the shebang refers to the Bourne shell, which is a simpler and more basic shell compared to Bash.
✨Command to list all the shells type📋:
cat /etc/shells
You will get all the shells that are installed in your system
✨Location of the bash:
which bash
✨Write a Shell Script that prints I will complete #90DaysOofDevOps challenge
Step 1: 📄 Create the file with .sh
extension.
touch file.sh
Step 2:🔒 Give the executable permission to the file.
chmod +x file.sh
Step 3: Write the script using the vim editor. ✍️
#!/bin/bash
echo "I will complete #90DaysOofDevOps challenge"
Step 4:To run the script 🏃♂️📜
./file.sh
✨Write a Shell Script to take user input, input from arguments, and print the variables.
#!bin/bash
echo "Enter your name";
read name;
age = $1;
random_message = $2;
echo "Hey $name, you are of $age age";
echo "$random_message";
Note:
$0
: The name of the script or the path to the script itself.$1
: The first argument passed to the script.$2
: The second argument passed to the script.$3
: The third argument passed to the script.And so on...
How to give the argument as the input to the file
./file.sh "10" "boooooooom";
Give the above script answer in the remark.
✨Write an Example of If else in Shell Scripting by comparing 2 numbers.
#!/bin/bash
# Taking user input for two numbers
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
# Comparing 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 "Both numbers are equal."
fi
Here's
read -p
help to take the input in the same line.-gt
means greater than.-lt
means less than.