What is Shell Scripting
Shell scripting is an important part for process automation in Linux. Shell scripting is a program which contains series of commands which are used to automate your day to day tasks . These commands are written in a file and they are executed one by one. This helps to save time when you are automating you daily operations. You can schedule your shell scripts using cron jobs too .
What is #!/bin/bash? Can we write #!/bin/sh as well?
Any Shell script can be identified as "Shebang". Its a combination of bash(#) and bang(!) then followed by your path or shell path. Bash(#) is the main core of the script which tells to system that its ready to take commands.
Lets have a look at a simple example of Shell script now
To create first shell we will use nano command here and type command as nano firstShell.sh . Here nano is nothing but an editor just like vim where we can create shell scripting commands lets have a look
Now in the nano editor we will use shebang to execute our shell script shebang is nothing but " #!bin/bash " and the echo command to print hello world . To save the file click ctrl +x and enter Yes .you will be redirected to terminal again .
Now lets execute our first Shell script as :- sh "firstshellScipr_name".sh
3.Write a Shell Script which prints I will complete #90DaysOfDevOps challenge.
Now we will create another script to print 90 days of Devops challenge . Lets start
4.Write a Shell Script to take a user input, input from arguments and print the variables.
Lets create a interactive shell script which takes age from user . Read the value from user and print it
Lets start the Shell script with shebang , then lets print the sentence which we want to display here in this case " Enter your age " .Now to read the input from user we will use read command and store in variable called as age and the display the actual value as $age in echo command . The output will be :
5.Write an Example of If else in Shell Scripting by comparing 2 numbers
Lets start with another shell script to compare 2 numbers
Lets understand the shell script program here First we willm echo the first number , read that first number and store in variable as a , similarly, for second number we will fo that .Now we want to compare those 2 numbers for this we will use if condition here so
If [ $a -gt $b ] . Here spacing is important where we are comapring $a and $ b the echo " a is greater the b else b is greater then a " then to end the if condition we are using fi command here .