Table of contents
- What is Kernel?
- What is Shell?
- Linux Shell Scripting...
- Why do we use Shell Script?
- Advantages of Shell Scripting...
- Disadvantages of Shell Scripting...
- Shell scripting for DevOps Engineer...
- What is #!/bin/bash? Can we write #!/bin/sh as well?
- #!/bin/bash ->
- #!/bin/sh ->
- Difference ->
- Write a Shell Script that prints "I will complete #90DaysOfDevOps challenge".
- Write a Shell Script that takes user input, input from arguments, and prints the variables.
- Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
- The End
What is Kernel?
Kernel is the core component of an Operating System. It is a bridge between applications and data processing performed at the hardware level.
What is Shell?
Shell is interface for users/human to interact with the Operating Systems. It accepts commands from the users and translates into instructions for Kernel.
Linux Shell Scripting...
Shell can take commands from the files or can accept input from the files, the scripts or written codes in the files are called shell script. We use shell scripting for executing tasks for repetitive manner.
The shell script files are having extension as: ".sh"
Why do we use Shell Script?
Avoiding repetitive works
Automation
Admin uses for system backups
System monitoring
Advantages of Shell Scripting...
Writing shell scripting is much easier and quicker
interactive debugging
Syntax are easy to learn
Disadvantages of Shell Scripting...
Single mistake can change the whole script, which might be dangerous
Slow execute
Not for large and complex process
Shell scripting for DevOps Engineer...
Fundamental skill in DevOps is automation routine tasks, manage system operations and streamline workflow.
Shell scripts automate repetitive tasks such as backups, file transfers, and software installations.
By scripting these tasks, DevOps engineers can save time and reduce the risk of human error.
They help in maintaining consistent environments across different servers.
They are often integrated into CI/CD pipelines to streamline the software delivery process.
- Shell scripting can be a part of IaC(Infrastructure as Code) tools to provision and manage infrastructure. Because it allows for the creation, updating, and management of infrastructure components programmatically.
Scripts are used to monitor system performance, check logs, and handle alerts.
Shell scripts often interface with other DevOps tools (e.g., Docker, Kubernetes, Jenkins) to execute commands and orchestrate workflows.
It is valuable for troubleshooting system issues, as scripts can be used to quickly diagnose and resolve problems.
What is #!/bin/bash
? Can we write #!/bin/sh
as well?
#!/bin/bash ->
This line tells the system to use the Bash (Bourne Again Shell) interpreter to execute the script.
Scripts starting with
#!/bin/bash
can use Bash-specific features and syntax.
#!/bin/sh ->
This line tells the system to use the
sh
(Bourne Shell) interpreter to execute the script.sh
is the original Unix shell and is more basic and portable than Bash.Scripts starting with
#!/bin/sh
are generally more portable across different Unix-like systems, but they may not have access to all the advanced features of Bash.
Difference ->
#!/bin/sh | #!/bin/bash |
Scripts using #!/bin/sh are more portable across different Unix-like systems. | Bash-specific features used in #!/bin/bash scripts may not work if sh is not Bash. |
Bash includes additional features and enhancements not available in traditional sh . | |
sing #!/bin/sh limits the script to POSIX-compliant shell features, ensuring broader compatibility but fewer features. | Using #!/bin/bash ensures that the script will be interpreted by Bash, allowing the use of all Bash features. |
Write a Shell Script that prints "I will complete #90DaysOfDevOps challenge".
Create a Script file with extension
.sh
nano
script.sh
Write the Script
echo "I will complete #90DaysOfDevOps challenge."
Make the Script executable
chmod +x
script.sh
Run the Script
Output ->
I will complete #90DaysOfDevOps challenge.
Write a Shell Script that takes user input, input from arguments, and prints the variables.
Take user input
echo "Please enter your name:"
read userName
Read input from argument
arg1=$Hello
Print the variables
echo "User input (interactive): $userName"
echo "First argument: $arg1"
Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
Create a script file:
nano compare_
numbers.sh
Write script:
Taking two numbers as input
echo "Enter the first number:" read num1
echo "Enter the second number:" read num2
Comparing the numbers
if ["$num1" -gt "$num2"]; then
echo "(num1) is greater than (num2)"
elif ["$num1" -lt "$num2"]; then
echo "(num2) is greater than (num1)"
else
echo "both the numbers are equal"
fi
Save and exit the editor:
press
Ctrl+O
to save andCtrl+X
to exitRun the code:
./compare_
numbers.sh
Output:
Enter the first number: 10
Enter the second number: 20
The (20) is greater than the (10).
The End
#