Table of contents
- What is Kernel?
- What is Shell?
- What is Linux Shell Scripting?
- What is #!/bin/bash? can we write #!/bin/sh as well?
- Wite a Shell Script which 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
What is Kernel?
๐ Kernel: The kernel is like the heart of a computer's operating system. It is a computer program that has full control over all the system resources and manages hardware and software interactions. ๐ง ๐ป
What is Shell?
๐ Shell: A shell is a special user program that acts as an interface between the user and the operating system services. It interprets human-readable commands and converts them into instructions that the kernel can understand and execute. ๐๐ฅ๏ธ
What is Linux Shell Scripting?
๐ Linux Shell Scripting: Linux shell scripting refers to the practice of writing scripts using a shell language, typically for the command-line interpreter in Linux systems. These scripts allow users to automate tasks, perform file manipulations, run programs, and display text on the terminal. ๐๐ง
What is #!/bin/bash?
can we write #!/bin/sh
as well?
๐ The #!/bin/bash
and #!/bin/sh
are like special codes at the start of a script. They tell the computer which interpreter to use. It's like picking a language for the computer to understand! ๐
#!/bin/bash
: This one says, "Hey, use the powerful Bash shell to run the script!" ๐ช Bash has lots of cool features and is great for complex tasks.#!/bin/sh
: This one tells the computer to use the basic Bourne shell. It's simple and works on most systems. ๐
You can choose either #!/bin/bash
for more capabilities, or #!/bin/sh
for better compatibility. It's up to you! ๐๐
In short, both shebangs are valid, and you can pick the one that suits your needs and the system you're working on! ๐งโจ
Wite a Shell Script which prints I will complete #90DaysOofDevOps challenge
Here's a shell script that prints the sentence "I will complete #90DaysOfDevOps challenge" along with the output:
#!/bin/bash
# Print the message
echo "I will complete #90DaysOfDevOps challenge"
To use this script, follow these steps:
Open a text editor (e.g., nano, vim, or gedit).
Copy and paste the above script into the text editor.
Save the file with a ".sh" extension, for example, "devops_challenge.sh".
Open a terminal and navigate to the directory where you saved the script.
Make the script executable with the following command:
chmod +x devops_challenge.sh
.Finally, run the script by typing
./devops_challenge.sh
in the terminal.
When you run the script, it will print the sentence "I will complete #90DaysOfDevOps challenge" on the terminal, like this:
I will complete #90DaysOfDevOps challenge
That's it! The script has executed successfully and displayed the desired output.๐
Write a Shell Script to take user input, input from arguments and print the variables.
Below is a shell script that takes user input and command-line arguments, and then prints the variables:
#!/bin/bash
# Taking user input
echo "Please enter your name:"
read user_input
# Accessing command-line arguments
argument1=$1
argument2=$2
# Printing the variables
echo "User input: $user_input"
echo "Command-line argument 1: $argument1"
echo "Command-line argument 2: $argument2"
To use this script, follow these steps:
Open a text editor (e.g., nano, vim, or gedit).
Copy and paste the above script into the text editor.
Save the file with a ".sh" extension, for example, "input_script.sh".
Open a terminal and navigate to the directory where you saved the script.
Make the script executable with the following command:
chmod +x input_script.sh
.To run the script and provide command-line arguments, use:
./input_script.sh argument1 argument2
.When you run the script, it will prompt you to enter your name. After entering your name, it will display the user input as well as the command-line arguments you provided.
For example, if you run ./input_script.sh Hello World
, the output will be:
Please enter your name:
Shahaji K
User input: Shahaji K
Command-line argument 1: Hello
Command-line argument 2: World
This script allows you to interact with the script by providing user input and command-line arguments, and then displays the values of the variables in the output. ๐
Write an Example of If else in Shell Scripting by comparing 2 numbers
Below is an example of a shell script that compares two numbers using an if-else statement and provides output based on the comparison:
#!/bin/bash
# Prompt user to enter two numbers
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Compare the numbers
if [ "$num1" -eq "$num2" ]; then
echo "Both numbers are equal."
elif [ "$num1" -gt "$num2" ]; then
echo "The first number ($num1) is greater than the second number ($num2)."
else
echo "The second number ($num2) is greater than the first number ($num1)."
fi
To use this script, follow these steps:
Open a text editor (e.g., nano, vim, or gedit).
Copy and paste the above script into the text editor.
Save the file with a ".sh" extension, for example, "compare_numbers.sh".
Open a terminal and navigate to the directory where you saved the script.
Make the script executable with the following command:
chmod +x compare_numbers.sh
.Finally, run the script by typing
./compare_numbers.sh
in the terminal.
When you run the script, it will prompt you to enter two numbers. After entering the numbers, it will display the comparison result. Here's an example of the output for different inputs:
Example 1:
Enter the first number:
10
Enter the second number:
5
The first number (10) is greater than the second number (5).
Example 2:
Enter the first number:
7
Enter the second number:
7
Both numbers are equal.
Example 3:
Enter the first number:
2
Enter the second number:
8
The second number (8) is greater than the first number (2).
The script compares the two numbers and provides the appropriate output based on the comparison result. ๐
Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.
HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning