Shell scripting is a text file with a list of commands that instruct an operating system that is run by UNIX shell which is a command line interpreter. Shell scripting helps to automate day-to-day manual tasks and reduce human intervention. This also helps to reduce human errors and time.
Note: Extension of shell script must be ".sh"
Let's look at the basic example
Steps
Create a new file and open the file
vi echo.sh
Enter the following lines of code
#!/bin/bash #Author: Tejal Girme #Date: 04/01/2024 #Purpose: This script will print some statements on terminal echo "I will be a great DevOps Engineer"
Save the file
Esc + :wqHere, the "echo" command will print "I will be a great DevOps Engineer" on the terminal.
Give execute permission to the file
chmod 777 echo.sh
Execute a shell script
./echo.sh
What is #!/bin/bash?
can we write #!/bin/sh
as well?
#!/bin/bash
tells the interpreter which shell should be used to execute the shell script#!/bin/bash
is called a shebang line
Yes, We can use either
#!/bin/bash
#!/bin/sh
Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
#!/bin/bash
#Author: Tejal Girme
#Date: 04/01/2024
#Purpose: This script will print some statements on the terminal
echo "I will complete #90DaysOofDevOps challenge"
Write a Shell Script to take user input, input from arguments and print the variables
#!/bin/sh
#Author:Tejal Girme
#Date: 01/04/2024
#Purpose: This shell script will accept 2 Arguments from user and displays it
#Defining Variables
Arg1=$1
Arg2=$2
#Printing Variables
echo "First argument: $Arg1"
echo "Second argument: $Arg2"
Here
Arg1 --> Variable1
Arg2 --> Variable2
To Execute it
./input.sh tejal girme
and boom unfold the magic by running this command in the terminal.
Write an Example of If else in Shell Scripting by comparing 2 numbers.
#!/bin/sh
#Author: Tejal Girme
#Date: 01/04/2024
#Purpose: This shell script will compare 2 numbers using if else
#Defining Variables
Num1=8
Num2=10
#Comaring Variables
if [ $Num1 -lt $Num2 ]
then
echo "$Num1 is greater than $Num2"
elif [ $Num1 -eq $Num2 ]
then
echo "$Num1 is equal to $Num2"
else
echo "$Num1 is lesser than $Num2"
fi
Happy Learning!!
Thanks For Reading! :)