Shell Scripting
DAY 04 Q. What is a shell script?
Ans : In the simplest terms, a shell script is a file containing a series of commands.The shell reads this file and carries out the commands as though they have been entered directly on the command line. Normally the shell scripts has the file extensions. sh nano devops_first.sh
Q. What is #!/bin/bash? can we write #!/bin/sh as well?
Ans : Yes, #!/bin/bash and #!/bin/sh are both called shebangs, and they are used at the beginning of a script file to specify the interpreter that should be used to execute the script. #!/bin/bash: This shebang specifies that the script should be executed using the Bash shell. Bash (short for Bourne Again SHell) is a popular and powerful shell that is available on most Unix-like operating systems, including Linux. #!/bin/sh: This shebang specifies that the script should be executed using the system's default Bourne shell or a compatible shell.
Q. Write a Shell Script which prints I will complete #90DaysofDevOps challenge
Ans : nano devops_first.sh (create file with sh extension )
Q.Write a Shell Script to take user input, input from arguments and print the variables.
Ans : #!/bin/bash
echo " I will complete #90DaysofDevops challenge"
./ devops_first.sh
Q. Write a Shell Script to take user input, input from arguments and print the variables.
Ans : ==> #!/bin/bash
Taking user input echo "Enter a value user want to print: "
read userInput
Taking input from arguments arg1="$1"
Printing variables echo "User input is: $userInput"
echo "Argument is: $arg1"
Q. Write an Example of If else in Shell Scripting by comparing 2 numbers.
Ans : ==> #!/bin/bash Define two numbers for comparison
number1=10
number2=20
Compare the two numbers
if [ $number1 -eq $number2 ] then
echo "The numbers are equal."
elif [ $number1 -lt $number2 ]
then
echo "Number 1 is less than Number 2." else echo "Number 1 is greater than Number 2."
fi
Comments
Join the discussionNo comments yet. Be the first to comment.


