
Shell scripting session 1
What is shell
- Shell is an interpreter between kernal and user
Types of shell
1. Sh shell
2. BASH shell
3. Cshell
4. kshell
For out identitiy will use .sh extension
#!/bin/bash -- Shebang (It is always the first line of script)
chmod a+x abc.sh --- adding execute permission on abc.sh
There are 2 ways to run bash script
1- ./abc.sh (x permission required)
2- bash abc.sh
Assigning variable
var_name=value
name=Aaftab
echo "My name is $name"
- here $ name is replace with Aaftab
#!/bin/bash
IP=$(ip a s | tail -4 | head -1 | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)
echo "My IP address is -----> $IP"
note - if we want to assign the value of executed command in variable the always used $()
Bash script
#!/bin/bash
IP=$(ip a s | tail -4 | head -1 | tr -s " " | cut -d " " -f3 | cut -d "/" -f1)
MEMORY=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2)
FMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f4)
MAC=$(ip a s | head -8 | tail -1 | tr -s " " | cut -d " " -f3)
DISK=$(lsblk | head -2| tail -1 | tr -s " " | cut -d " " -f4)
echo "My IP address is -----> $IP"
echo "My IP Total memory is -----> $MEMORY"
echo "My Free memory is -----> $FMEM"
echo "My MAC is -----> $MAC"
echo "My Disk size is -----> $DISK"
output -->
My IP address is -----> 172.31.47.231
My IP Total memory is -----> 761Mi
My Free memory is -----> 319Mi
My MAC is -----> 02:5e:b5:ba:41:26
My Disk size is -----> 10G
Scripting Session 2
read -- when we want to take input from user then we use read
ex-
#!/bin/bash
read -p "Enter your name -->" NAME
echo "My name is $NAME"
expr -- use to perform addition, substraction , multiplication adn division
#!/bin/bash
read -p "Enter first number -->" N1
read -p "Enter second number -->" N2
SUM=$(expr $N1 + $N2)
echo "Addition of two number is $SUM"
command line argument
It is use to pass the argument at run time
ex-
#!/bin/bash
SUM=$(expr $1 + $2)
echo "Addition of two number is $SUM"
$1 - first argument
$2 - second
$3- third
etc....
#!/bin/bash
MEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2 | cut -d "M" -f1)
UMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f3 | cut -d "M" -f1)
UTI=$(expr 100 \* $UMEM / $MEM)
echo "Memory utilization is $UTI%"
output -- Memory utlization is 33%
Task -
Free memory %
Addition,multiplication, deivision, substraction
Buffer cache in %
Scripting Session 3
Condition - It is used to execute as per the situation
if [];then
Syntax
if [ condition ];then
statement1
statement2
else
Statement 3
fi
Syntx - if with elif
if [ condition ];then
statement1
statement2
elif [ condition ];then
statement3
else
Statement 4
fi
Syntx with multiple conditions using operator
if [ condition1 ] || [ condition2 ];then
statement1
else
statement2
fi
operators
|| (or) -- it is used when either first or second condition should be match
&& (and) -- it is used when both condition should be match
#!/bin/bash
read -p "Please enter valid name-->" NAME
if [ "$NAME" == "aaftab" ];then
echo "Welcome to Pune"
else
echo "Invalid user"
fi
#!/bin/bash
read -p "Please enter valid name-->" NAME
if [ "$NAME" == "aaftab" ];then
echo "Welcome to Pune"
elif [ "$NAME" == "moin" ];then
echo "welcome to Pune"
else
echo "Invalid user"
fi
#!/bin/bash
read -p "Please enter valid name-->" NAME
if [ "$NAME" == "aaftab" ] || [ "$NAME" == "moin" ];then
echo "Welcome to Pune"
else
echo "Invalid user"
fi
Write script to verify if input value is more than 80 then print first class between 60 to 80 second class and less than 60 third class
#!/bin/bash
read -p "Please enter Percentage-->" PER
if [ $PER -gt 80 ];then
echo "First class"
elif [ $PER -gt 60 ] && [ $PER -lt 80 ];then
echo "Second class"
else
echo "Third class"
fi
Write script to verify if input value is more than 80 then print memroy alert between 60 to 80 memory warning and less than 60 normal memory utlization
#!/bin/bash
MEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f2 | cut -d "M" -f1)
UMEM=$(free -h | head -2 | tail -1 | tr -s " " | cut -d " " -f3 | cut -d "M" -f1)
UTI=$(expr 100 \* $UMEM / $MEM)
if [ $UTI -gt 80 ];then
echo "Memory Alert and utilization is $UTI%"
elif [ $UTI -gt 60 ] && [ $UTI -lt 80 ];then
echo "Memory warning and utilization is $UTI%"
else
echo "Memory is normal and utlization is $UTI%"
fi
lt -- less than
gt -- greater than
le -- less than equal to
ge -- greater than equal to
! $var -- not equal to
Scripting Session 4
Use if condition to check wether file is exist or not
#!/bin/bash
if [ -f $1 ];then
echo "File is exist"
else
echo "File is not exist"
fi
Use if condition to check wether file is exist or not
#!/bin/bash
if [ -d $1 ];then
echo "Directory is exist"
else
echo "Directory is not exist"
fi
Control the input statement
#!/bin/bash
if [ $# -eq 2 ];then
add=$(expr $1 + $2)
echo "Addition is $add"
else
echo "Please enter two numbers"
fi
$# - number of argument is passed
$? - exit state of previous command (0- sucess, non-zero - fail)
$0 - name of shell script we have written
$1 - first argument
$2 - second argument
#!/bin/bash
tar -cvf /home/ec2-user/abc.tar /home/ec2-user &>/dev/null
if [ $? -eq 0 ];then
echo "Backup sucessfull"
else
echo "Backup fail"
fi
#!/bin/bash
if [ $# -eq 1 ];then
if [ -f $1 ];then
echo "File is exist"
else
echo "File is not exist"
fi
else
echo "Please enter file name"
fi
Scripting Session 5
note- While loop is used to read line by line and for loop is used to read word by word
Loops - It is used to write stetemt multiple times until condition is matched
Types of loops
While
for
Until
1. While loop - It will execute condition line by line until given condition in bracket is not wrong
while [ condition ]
do
statement
done
#!/bin/bash
NUM=0
read -p "Enter number to print line-->" count
while [ $NUM -ne $count ]
do
echo "My name is Aaftab"
NUM=$(expr $NUM + 1)
done
2. Until loop - It will execute condition line by line until given condition in bracket is wrong
until [ condition ]
do
statement
done
#!/bin/bash
NUM=0
read -p "Enter number to print line-->" count
until [ $NUM -eq $count ]
do
echo "My name is Aaftab"
NUM=$(expr $NUM + 1)
done
Combination of while loop with if condition using infinity loop
#!/bin/bash
while [ 1 == 1 ]
do
read -p "Enter your name -->" NAME
if [ "$NAME" == "exit" ];then
exit;
else
echo "Your name is $NAME"
fi
done
To take the file input using while loop always used read
syntax
while read var
do
statement
done < file.txt
example
#!/bin/bash
while read file
do
echo "My name is -->" $file
done < name.txt
Scripting Session 9
sed - it is a stream editor (use to change the content in stdout or else in file)
echo "My name is Aaftab" |sed 's/Aaftab/Aarti/'
My name is Aarti
sed 's/Aaftab/Aarti/' test.txt
cat test.txt
My name is Aaftab Aaftab
My name is Aaftab
ex-- sed 's/Aaftab/Aarti/' test.txt
My name is Aarti Aaftab
My name is Aarti
sed 's/Aaftab/Aarti/g' test.txt
My name is Aarti Aarti
My name is Aarti
use -i in sed command to changes in file
sed -i 's/Aaftab/Aarti/g' test.txt
cat test.txt
My name is Aarti Aarti
My name is Aarti
sed -i '/How/d' test.txt
It will delete all the lines containing How
cat test2.txt
Apple is red.
Mango is yellow.
your dress color is Red,
Red color suits on all.
sed -e 's/red/blue/; s/yellow/black/' test2.txt --- e is used for multiple string changes
cat sedcommmand.txt
s/red/blue/;
s/yellow/black/
ex sed -f sedcommmand.txt test2.txt
Apple is blue.
Mango is black.
your dress color is Red,
Red color suits on all.
specific line replacement
sed '3s/Red/Blue/' test2.txt
check in which line string is present
sed -n '/Mango/=' test2.txt
2
Scripting Session 10
AWK
cat abc.txt
Aaftab DevOps Engineer 600000
Aarti Cloud Enginner 5000000
Dawood AWS Engineer 700000
Nizam Devops Developer 900000
awk '{print}' abc.txt
awk '/Dawood/ {print}' abc.txt
output- Dawood AWS Engineer 700000
awk '/Dawood/ {print $1,$4}' abc.txt (alternate- cat abc.txt |grep Dawood | cut -d" " -f1,4)
Dawood 700000
awk '{print NR,$0}' abc.txt (NR- Display line number)
1 Aaftab DevOps Engineer 600000
2 Aarti Cloud Enginner 5000000
3 Dawood AWS Engineer 700000
4 Nizam Devops Developer 900000
awk '{print $1,$NF}' abc.txt (print 1st and last column)
Aaftab 600000
Aarti 5000000
Dawood 700000
Nizam 900000
awk 'NR==2, NR==4 {print NR,$0}' abc.txt (print from line no. 2 to line no 4)
2 Aarti Cloud Enginner 5000000
3 Dawood AWS Engineer 700000
4 Nizam Devops Developer 900000
awk '{print NR "--->"$1}' abc.txt
1--->Aaftab
2--->Aarti
3--->Dawood
4--->Nizam
awk '{print NR "--->"$1 "--" $2}' abc.txt
1--->Aaftab--DevOps
2--->Aarti--Cloud
3--->Dawood--AWS
4--->Nizam--Devops
sed - it is a stream editor (use to change the content in stdout or else in file)
echo "My name is Aaftab" |sed 's/Aaftab/Aarti/'
My name is Aarti
sed 's/Aaftab/Aarti/' test.txt
cat test.txt
My name is Aaftab Aaftab
My name is Aaftab
ex-- sed 's/Aaftab/Aarti/' test.txt
My name is Aarti Aaftab
My name is Aarti
sed 's/Aaftab/Aarti/g' test.txt
My name is Aarti Aarti
My name is Aarti
use -i in sed command to changes in file
sed -i 's/Aaftab/Aarti/g' test.txt
cat test.txt
My name is Aarti Aarti
My name is Aarti
sed -i '/How/d' test.txt
It will delete all the lines containing How
cat test2.txt
Apple is red.
Mango is yellow.
your dress color is Red,
Red color suits on all.
sed -e 's/red/blue/; s/yellow/black/' test2.txt --- e is used for multiple string changes
cat sedcommmand.txt
s/red/blue/;
s/yellow/black/
ex sed -f sedcommmand.txt test2.txt
Apple is blue.
Mango is black.
your dress color is Red,
Red color suits on all.
specific line replacement
sed '3s/Red/Blue/' test2.txt
check in which line string is present
sed -n '/Mango/=' test2.txt
2
Have you tried to learn shell scripting on your own, but lack the structure you need to really improve your skills?
Are you tired of picking up bits and pieces of information that you can't just seem to put together?
Do you learn best by doing?
If so, you're going to love this course.
One of the biggest complaints I hear from students is that most of the courses they've taken in the past simply provide information without any context and without any idea of how to put that information to use!
This course turns that old, frustrating, and outdated way of learning on its head.
It's project-based, which means instead of learning bits and pieces of information, you'll write actual shell scripts that you can use in real-world situations. You get the chance to immediately put what you learn to use so that you fully understand and remember it.
In this Linux shell scripting course you will learn how to:
Name your shell scripts.
Use the proper permissions on your shell scripts.
Create and use variables in your scripts.
Use shell built-in commands and operating system commands.
Make the most out of special variables that are available to you in your scripts.
Make decisions by using if statements and performing several different kinds of tests
Check the exit statuses of commands and why you need to.
Use cryptographic hash functions
Create random data so you can do things like automatically generate strong passwords for user accounts.
Perform the same action or set of actions over a series of data utilizing for loops, while loops, and infinite loops.
Control all types of input and output.
Accept standard input from a user as well as from another program.
Redirect standard output and standard error.
Combine standard output and standard error
Use command pipelining
Perform text and string manipulation.
Process command line arguments
Creating functions and when to do so.
Parsing, analyzing, and reporting on log files, CSV files, and other data.
Writing scripts that execute commands on other systems.
Much, much, more...
When you enroll, you get lifetime access to the course. Your course never expires. You can study at your own pace and refer back to the lessons whenever you want!
I stand behind my courses and care about your goals. That's why this course comes with an unconditional 30-day money-back guarantee. It's my personal promise of your success! So...
If you're ready to level-up your shell scripting skills, enroll now!