Basic shell scripting
#90daysdevops
#day4
Shell is an interface between the user and the kernal . So the commands that we write inside our shell to communicate with the kernal is known as shell scripts . There are different types of shell out there eg , bash , c shell , z shell etc. Shell scripts mainly help you with automation .
Let's start with our first shell script program
The first step is to create a file with extension .sh
using the touch
command and then open it with any text editor of your choice .Here i will be using vim text editor.
once you have opened the vim , press i
to go into the insert mode.Now we are ready to write our very first line of shell scripts command .
So what is this line means ?
#! stands for shebang . #!/bin/bash is basically telling your system to use bash as a default shell.
once you are done writing your command , press esc
and write :wq
and then enter . well you have created your first shell script file. Now to run that either use bash filename.sh or ./filename.sh both will execute your file. Use chmod u+x
filename.sh to give executable permission in case it doesnt have one.
Using if else statement
syntax :
make sure to give space between if
and [ ]
and also between the operators . read
command is use to take inputs from the user . a and b are variable here and to use the value of variable we use $ symbol.
This script will take 2 numbers from the user and compare it .
How to pass argument to a bash shell script :
Here $1 and $2 will contain the values of the argument passed . We can pass any no of argument to a file . Here sujata and devops are two arguments that we are passing to this file. script.sh is the name of the file.
Difference between #!bin/bash and #!/bin/sh :
sh is a symlink to the currently configured system shell, which on Linux systems is usually either bash
or dash
.So we can write #!/bin/sh in place of #!/bin/bash it will work fine.
Thank you for reading!!
Great initiative by the #trainwithshubham community.