π What is Shell?
A shell interprets commands from the user and executes them by making system calls to the operating system kernel. shells include bash (the Bourne Again Shell), zsh, tcsh, and PowerShell.
The shell acts as an intermediate between the user and the operating system kernel, and the functionality of the kernel in an accessible way
Kernel
The kernel is a computer program that is the core of a computerβs operating system, with complete control over everything in the system.
How is Shell scripting different from traditional programming?
Shell scripting and traditional programming serve different purposes and are suited for different tasks. Shell scripting is designed for automating system-related tasks and working with the command line, while traditional programming involves building complete software applications with a broader range of features and complexity. The choice between them depends on the specific requirements of the task at hand.
#!/bin/bash vs. #!/bin/sh
The #!/bin/bash
is called a shebang or hashbang. It tells the system which interpreter to use for the script. You can also use #!/bin/sh
, but it might use a different interpreter. bash
is more common and provides additional features.
Steps to Create a Shell Script :
Step 1: Use any editor vim or nano script
Syntax: vim first_
script.sh
Step 2: Permit to execute the file
Syntax: chmod <<permission>> <<
filename.sh
\>>
chmod 755 first_
script.sh
This will set the permission read, write, execute (7), for group and others permission is being read and execute (5)
Shell Script Example
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
The above script will print the message "I will complete #90DaysOfDevOps challenge"
when executed.
- Write a Shell Script to take user input, input from arguments and print the variables.
#!/bin/bash
echo "This is some text"
To Execute Script:
. <file-name>.sh
bash <file-name>.sh
./<file-name>.sh (forthis we need change file permission to execute)
πβ¨ Write a Shell Script π:
1οΈβ£ Accept user input πββοΈπ¬
read -p "Enter your name: " name
2οΈβ£ Take input from arguments π °οΈπ ±οΈ
#!/bin/bash
echo "Your name is $1"
echo "Your age is $2"
3οΈβ£ Display the variables π₯οΈπ€
echo "Your name is $variable"
writing complete code using the above three syntaxes:
#!/bin/bash
read -p "Enter your details: " name
age= $1
city= $2
echo " your Name is $name "
echo "your age is $age"
echo "your city is $city"
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
read -p "Enter Your Marks:- " marks
if [[ $marks -gt 40 ]]
then
echo "Your are PASS:)"
else
echo "Your are FAIL!!!!!"
fi