Hello everyone ;
I am Teertha Darekar
Welcome back to my day 7 blog of linux Workshop conducted by Pranav Jambare sir.
CONTENTS :-
Shell Script Introduction
Filteration Tool
If - Else - If
let's start :-
--> Shell Scripting
A shell script is a type of computer program developed to be executed by a Unix shell.
Several shell script dialects are treated as scripting languages.
Shabang :-
On Linux, a shabang (#!) is a special line at the beginning of a script that tells the operating system which interpreter to use when executing the script.
Shabang line is important because it allows you to run scripts written in in any language.
#!/bin/bash
Types of Shell Script :-
- non Interactive :-
A non-interactive shell is a type of shell that doesn’t interact with the user. We can run it through a script or similar.
This shell is generally a non-login shell because the calling user has logged in already.
A shell that runs a script is always considered a non-interactive shell.
- Interactive :-
An interactive shell is defined as the shell that simply takes commands as input from the user and acknowledges the output to the user.
Interactive scripts couldn’t run in the background .
--> Filteration
Filters are programs that take plain text(either stored in a file or produced by another program) as standard input, transforms it into a meaningful format, and then returns it as standard output.
There are Various types of filters in linux .
1) Sort :-
- Sorts the lines alphabetically by default but there are many options available to modify the sorting mechanism.
Syntax:-
sort options [filename]
Options :-
Sorting by numbers :-
sort -n filename
Sorting by reverse numbering :-
sort -nr filename
Checks whether files are sorted or not :-
sort -c filename
Sort and remove duplicate entry :-
sort -u filename
Sorting by months :-
sort -M filename
2) Tail :-
It returns the lines from bottom to up.
If the number of lines is not specified then by default prints last 10 lines.
Syntax :–
tail options [filename]
Options :-
‘n’ stands for number and prints last entries :-
tail -n filename
To get dynamic output :-
tail -f filename1 filename2
More than one file :-
tail -q filename
3 ) Head :-
Displays the first n lines of the specified text files.
If the number of lines is not specified then by default prints first 10 lines.
Syntax :-
head options filename
‘n’ stands for number and prints top number of entries :-
head -n filename
4) wc [Word count] :-
- wc command gives the number of lines, words and characters in the data.
Syntax :
– wc options [filename]
Options :-
Counts number of lines :-
wc -l filename
Counts number of characters :-
wc -m filename
Counts number of bytes :-
wc -c filename
Counts number of words :-
wc -w filename
5 ) uniq :-
Removes duplicate lines.
uniq has a limitation that it can only remove continuous duplicate lines.
Syntax :–
uniq options filename
Options :-
Counts the number of occurrences of words :-
uniq -c filename
Prints repeated lines :-
uniq -d filename
Prints the words that occur more than once in a individual line :-
uniq -D filename
Print unique lines :-
uniq -u filename
6) comm :-
Compare two sorted files line by line and prints the lines that are unique to each file .
Syntax :–
comm Fileno.1 Fileno.2
7) diff :-
Compare the contents of two files and prints the differences between them.
Syntax :–
diff Fileno.1 Fileno.2
8) grep :-
grep is used to search a particular information from a text file.
Options :-
Search case insensitive occurrence of the specified word :-
grep -i word filename
Prints occurrences of words in the lines :-
grep -o word filename
After, before or both :-
grep -a or -b or -c word filename
Counts number of line occurrences :-
grep -c word filename
Highlights the exact word specified :-
grep -w word filename
Highlights multiple words specified:-
grep -e word1 -e word2 filename
Prints the line, highlights the word along with the line number :-
grep -n word filename
9) awk :-
Options :-
Prints all the lines :-
awk ‘{print}’ filename
Prints the lines in which the specified word is there
awk ‘/word/’’{print}’ filename
Prints the 1st and 4th word of each line :-
awk ‘{print $1,$4}’ filename
Prints the 1st and last word of each line:-
awk ‘{print $1,$NF}’ filename
Pattern will match lines from line 3 to line 6 print that lines :-
awk ‘NR==3,NR==6’‘{print NR,$0}’ filename
10) cut :-
Prints the field 2 :-
cut -d” ” -f2 filename
11) sed :-
sed stands for stream editor.
It allows us to apply search and replace operation on our data effectively.
Options :-
Replace the second occurrence of the word to be replaced :-
sed ‘s/word to be replaced/replacing word/2’ filename
Replaces all occurrences of the word to be replaced :-
sed ‘s/word to be replaced/replacing word/g’ filename
‘3’ represents the line number and replace the word to be replaced at 3rd line :-
sed ‘3 s/ word to be replaced/replacing word/g’ filename
‘1,3’ is line range and this is deleted as there is ‘d’ means delete and other lines are printed :-
sed ‘1,3d’ filename
Prints all the entries along with 1-3 range again after it :-
sed ‘1,3p’ filename
Delete entries with the specified word :-
sed ‘/word/d’ filename
--> If - Else - If
1) Simple If
Syntax :-
if [expression]
then
Statement
fi
Example :-
if [ ${A} -gt ${B} ]
then
echo "${A} is greater than ${B}"
fi
2) If - Else
Syntax :-
if [expression]
then
Statement
else
Statement
fi
Example :-
if [ ${name} == Teertha ]
then
echo "${name} is correct"
else
echo "${name} is incorrect"
fi
3) Multiple If
Syntax :-
if [expression]
then
Statement
elif [expression]
then
Statement
elif [expression]
then
Statement
else
Statement
fi
Example :-
#!/bin/bash
echo "Enter your age"
read age
if [ ${age} -ge 0 -a ${age} -lt 15 ]
then
echo "You are a kid"
elif [ ${age} -ge 16 -a ${age} -lt 25 ]
then
echo "You are a teenager"
elif [ ${age} -ge 25 -a ${age} -lt 50 ]
then
echo "You are an adult"
elif [ ${age} -ge 50 -a ${age} -lt 100 ]
then
echo "You are a Senior citizen"
else
echo "Invalid age"
fi
4) Nested If
Syntax :-
if [expression]
then
Statement
if [expression]
then
Statement
else
Statement
fi
else
Statement
fi
#Example
#!/bin/bash
echo "Enter your first name"
read firstname
echo "Enter your Surname"
read surname
if [ ${firstname} == Teertha ]
then
echo "first name is matched"
if [ ${surname} == Darekar ]
then
echo "first name and surname both matched"
else
echo "first name matched but surname is mismatched"
fi
else
echo "first name mismatched so not continuing"
fi
Thankyouu ....