SlideShare a Scribd company logo
1 of 14
Script to Print 1 to 10
#!/bin/bash
i=1
while [ $i -le 10 ]
do
echo $i
i=$(($i+1))
Done
Output:
Script to find Factorial of the given number
#!/bin/bash
echo "Enter the number"
read n
fact=1
for ((i=1;i<=n;i++))
do
fact=`expr $fact * $i`
Done
The Factorial of $n is $fact
Output:
Script to Add two numbers using function
#!/bin/bash
function add()
{
sum=$(($1 + $2))
echo "Sum = $sum"
}
a=10
b=20
add $a $b
Output
Script to Display Multiplication table of Given Number using function
#!/bin/bash
tables()
{
i=1
while [ $i -le 10 ]
do
echo " $1 * $i =`expr $1 * $i ` "
i=`expr $i + 1`
done
}
echo "Enter the number to Generate Multiplication table : "
read n
tables $n
Output
Script to Find Highest and Lowest value in 3 numbers using functions
#!/bin/bash
greatest()
{
if [ $1 -gt $2 ] && [ $1 -gt $3 ]
then
echo "$1 is Greatest Number"
elif [ $2 -gt $1 ] && [ $2 -gt $3 ]
then
echo "$2 is Greatest Number"
else
echo "$3 is Greatest Number"
fi
}
Continued……..
lowest()
{
if [ $1 -lt $2 ] && [ $1 -lt $3 ]
then
echo "$1 is Lowest Number"
elif [ $2 -lt $1 ] && [ $2 -lt $3 ]
then
echo "$2 is Lowest Number"
else
echo "$3 is Lowest Number"
fi
}
echo "Enter Num1"
read num1
echo "Enter Num2"
read num2
echo "Enter Num3"
read num3
greatest $num1 $num2 $num3
lowest $num1 $num2 $num3
Continued……..
Output
Script to Test if File Exists are not
#!/bin/bash
filename=$1
if [ -f "$filename" ]; then
echo "File exists"
else
echo "File does not exist"
fi
Output
Script to Print File with Line Count
#!/bin/bash
myfile=$1
i=1
while read lines;
do
echo "$i : $lines"
i=$((i+1))
done < $myfile
Output
Script to Remove Duplicate lines from File
#! /bin/bash
echo -n "Enter Filename-> "
read filename
if [ -f "$filename" ]; then
sort $filename | uniq | tee sorted.txt
else
echo "$filename is not found...try again"
fi
Continued……..
Output
Script to remove all empty files
#! /bin/bash
for x in *
do
if [ -s $x ]
then
continue
else
rm -rf $x
fi
done
Output

More Related Content

Similar to Shell Scripting Examples -Charan.pptx

ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docxAdd functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
WilliamZnlMarshallc
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
Hung-yu Lin
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Masahiro Nagano
 
Complete the implementation of the binary search tree started in cla.pdf
Complete the implementation of the binary search tree started in cla.pdfComplete the implementation of the binary search tree started in cla.pdf
Complete the implementation of the binary search tree started in cla.pdf
sanuoptical
 

Similar to Shell Scripting Examples -Charan.pptx (19)

Mips1
Mips1Mips1
Mips1
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
 
De 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWKDe 0 a 100 con Bash Shell Scripting y AWK
De 0 a 100 con Bash Shell Scripting y AWK
 
SL-2.pptx
SL-2.pptxSL-2.pptx
SL-2.pptx
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
Programada chapter 4
Programada chapter 4Programada chapter 4
Programada chapter 4
 
python file for easy way practicle programs
python file for easy way practicle programspython file for easy way practicle programs
python file for easy way practicle programs
 
03 tk2123 - pemrograman shell-2
03   tk2123 - pemrograman shell-203   tk2123 - pemrograman shell-2
03 tk2123 - pemrograman shell-2
 
Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2Solution manual of shell programming assignment 2
Solution manual of shell programming assignment 2
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docxAdd functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Complete the implementation of the binary search tree started in cla.pdf
Complete the implementation of the binary search tree started in cla.pdfComplete the implementation of the binary search tree started in cla.pdf
Complete the implementation of the binary search tree started in cla.pdf
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Shell Scripting Examples -Charan.pptx

  • 1.
  • 2. Script to Print 1 to 10 #!/bin/bash i=1 while [ $i -le 10 ] do echo $i i=$(($i+1)) Done Output:
  • 3. Script to find Factorial of the given number #!/bin/bash echo "Enter the number" read n fact=1 for ((i=1;i<=n;i++)) do fact=`expr $fact * $i` Done The Factorial of $n is $fact Output:
  • 4. Script to Add two numbers using function #!/bin/bash function add() { sum=$(($1 + $2)) echo "Sum = $sum" } a=10 b=20 add $a $b Output
  • 5. Script to Display Multiplication table of Given Number using function #!/bin/bash tables() { i=1 while [ $i -le 10 ] do echo " $1 * $i =`expr $1 * $i ` " i=`expr $i + 1` done } echo "Enter the number to Generate Multiplication table : " read n tables $n
  • 7. Script to Find Highest and Lowest value in 3 numbers using functions #!/bin/bash greatest() { if [ $1 -gt $2 ] && [ $1 -gt $3 ] then echo "$1 is Greatest Number" elif [ $2 -gt $1 ] && [ $2 -gt $3 ] then echo "$2 is Greatest Number" else echo "$3 is Greatest Number" fi } Continued……..
  • 8. lowest() { if [ $1 -lt $2 ] && [ $1 -lt $3 ] then echo "$1 is Lowest Number" elif [ $2 -lt $1 ] && [ $2 -lt $3 ] then echo "$2 is Lowest Number" else echo "$3 is Lowest Number" fi } echo "Enter Num1" read num1 echo "Enter Num2" read num2 echo "Enter Num3" read num3 greatest $num1 $num2 $num3 lowest $num1 $num2 $num3 Continued……..
  • 10. Script to Test if File Exists are not #!/bin/bash filename=$1 if [ -f "$filename" ]; then echo "File exists" else echo "File does not exist" fi Output
  • 11. Script to Print File with Line Count #!/bin/bash myfile=$1 i=1 while read lines; do echo "$i : $lines" i=$((i+1)) done < $myfile Output
  • 12. Script to Remove Duplicate lines from File #! /bin/bash echo -n "Enter Filename-> " read filename if [ -f "$filename" ]; then sort $filename | uniq | tee sorted.txt else echo "$filename is not found...try again" fi Continued……..
  • 14. Script to remove all empty files #! /bin/bash for x in * do if [ -s $x ] then continue else rm -rf $x fi done Output