Table of contents
No headings in the article.
Hello Everyone!
This is Day 4 learning. It is a very Beginner friendly Note. Give it a read :)
Please find below the concepts this article covers on Shell Scripting.
Few Basic Commands
Checking file and directory exists or not
Writing a script
Comments
Arguments
File Creation
7. if, elif, and for loop
#To check which flavor of Linux you are using
cat /etc/os-release
#To know which bash you are using
echo $SHELL or echo $BASH
#who are the people who logged in
$who
$who -H (-H displays header)
#Checking if a file exists or not
The ‘-f’ option in brackets tells the command to check whether the specified file exists.
#Checking if a directory exists or not
#$1 in the script represent the command line argument that we will pass while ging execute command.
#!/bin/bash
dir=$1 if [ -d $1 ]
then echo "$1 directory exists"
else
echo "Sorry $1 directory doesnt exists"
fi
Writing a Script:
Editors to write the script:
There are different editors where we can write our script. Ex: vim, vi, nano, pico etc
Vim is a mode-based text editor while Nano is modeless. Mode-based editor means that you need to enter INSERT mode before you can write text to the file.
Nano is an improvement over the Pico text editor, whereas Vim is an improved version of the Vi editor
We will use vim editor.
#create a file called sample.sh, .sh represents it is a shell script
vim sample.sh
Executing a file:
we can execute in 2 ways
In the second method, we need to assign execute permission to the file
chmod 777 sample.sh
ls -l
this file becomes executable and will be in green color
Inside vim:
Press i key to insert
Esc :wq to save
Getting Started with Script:
#!/bin/bash
# = sharp
! = bang
#! = shebang
Comments:
single line comment - #
Multiline Comment -
<< 'COMMENT'
--
--
COMMENT
Arguments:
$1 represents command line argument, when you use $1 in the script it mean you need to give argument after filename. $2 - two arguments, $3 - three arguments.
$# - count of arguments
$$-PID of the script
$*-Represent all the arguments as a single string
$@ - Same as $∗, but differ when enclosed in (")
$?-Represent last return code
Example:
Sample file creation :
We will create a directory - (mkdir)
move to that directory - (cd directory name )
create a file in that dir - touch filename
Give data into it - echo "" > filename
if statement:
Note: Linux is space sensitive.
There should be space after "if" and after "[[" and before "]]"
n=1
if [ $n -eq 1 ]
then echo "This is true" fi
elif statement:
#!/bin/sh
a=10 b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
for loop:
#!/bin/bash
echo "Lines from $1"
for (( i=1; i<=$1; i++ ))
do
echo "$i"
done