Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers
Table of contents
- What is kernel
- What is shell scripting
- What is shell
- Task:
- Explain in your own words and with examples what Shell Scripting means for DevOps.
- What is #!/bin/bash? Can we write #!/bin/sh as well?
- 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.
What is kernel
The kernel is a computer program at the core of a computer’s operating system that manages operations of computer and hardware.
What is shell scripting
Shell scripting is a series of Linux commands written in a file in sequence. It is a way to automate repetitive tasks, saving time and effort. Shell scripts are commonly used to manage systems, infrastructure, and handle batch processing.
What is shell
Shell is a program and command line interpreter. It is an interface between user and kernel.
Why Learn Shell Scripting?
To avoid repetitive work.
System admins use shell scripting for routine backups.
System monitoring
Adding new functionality to the shell etc.
Task:
Explain in your own words and with examples what Shell Scripting means for DevOps.
Shell scripting in DevOps is a powerful tool for automating tasks, managing infrastructure, maintaining consistency, and integrating various tools and processes to achieve efficient and reliable software delivery.
What is #!/bin/bash? Can we write #!/bin/sh as well?
#!/bin/bash - #!/bin/bash in shell script is called shabang & its use to tell the system which interpreter/command to use to execute the commands written inside the scripts.
#!/bin/bash echo "Hello World!"
#!/bin/sh - It is used to execute the file using sh, which is a Bourne
shell, a compatible shell.
#!/bin/sh echo "Hello World!"
How to execute a shell script -
# How to execute the shell script ./hello.sh
Write a Shell Script that prints I will complete #90DaysOfDevOps challenge.
#!/bin/bash --------> Shebang
echo "I will complete 90daysofdevops challenge"
Write a Shell Script that takes user input, input from arguments, and prints the variables.
#!/bin/bash
#Variables which takes input from users
read -p "Please Enter your Name: " Name
read -p "Please Enter your Age: " Age
if [ $Age -gt 18 ]; then
echo "Your Age is ${Age} and you are Eligible for voting. Happy Voting!!!"
fi
-
Before running scripts we need to ensure that we have executable permission for the shell file.
As we can see in shell below, we only have read and write permission for input.sh file i.e, rw- , we have to make it rwx for executing.
$ ls -l Input.sh -rw-rwxr-x 1 ubuntu ubuntu 243 Aug 21 05:31 Input.sh
-
To execute this file we need to give executable permission.
$chmod +x Input.sh
$ls -l Input.sh -rwxrwxr-x 1 ubuntu ubuntu 243 Aug 21 05:31 Input.sh
As we see after giving permission script got executed and give output as it is.
Input as an arguments -
#!/bin/bash Name=$1 Age=$2 echo "Your Name is ${Name}" echo "Your Age is ${Age}" if [ $Age -gt 18 ]; then echo "Your age is ${Age} and you are eligible for Vote." echo "Happy Voting!!!" else echo "Your age is ${Age} and you aren't eligible for Vote." fi
-
For arguments there is a different way to give input, in argument scripts we give input along with our script execution command. Like below:
$ ./arguments.sh Vibhuti 20
Provide an example of an If-Else statement in Shell Scripting by comparing two numbers.
Number1=$1
Number2=$2
if [ $Number1 -gt $Number2 ]; then
echo "${Number1} is greater than ${Number2} "
elif [ $Number1 -lt $Number2 ]; then
echo "${Number1} is less than ${Number2} "
else
echo "${Number1} and ${Number2} are equal "
fi
Subscribe to my newsletter
Read articles from Vibhuti Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vibhuti Jain
Vibhuti Jain
Hi, I am Vibhuti Jain. A Devops Tools enthusiastic who keens on learning Devops tools and want to contribute my knowledge to build a community and collaborate with them. I have a acquired a knowledge of Linux, Shell Scripting, Python, Git, GitHub, AWS Cloud and ready to learn a lot more new Devops skills which help me build some real life project.