Shell Scripting Examples -Charan.pptx

charan10 30 views 14 slides Jun 01, 2023
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Some examples of Shell Scripting


Slide Content

SOME EXAMPLES OF SHELL SCRIPTING By Charan

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 L owest 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
Tags