You are on page 1of 25

Shell Scripts

1. Area and Perimeter of a Circle................................................................................................2


2. Menu driven Calculator..........................................................................................................3
3. Largest of three numbers.........................................................................................................4
4. Mean and Standard Deviation.................................................................................................5
5. Reverse a Number and Sum of digits......................................................................................6
6. List n Fibonacci numbers........................................................................................................7
7. Check whether a number is Armstrong or not........................................................................8
8. Factorial of a Number.............................................................................................................9
9. List Prime numbers upto a given number.............................................................................10
10. Reverse a String and Check whether Palindrome or not.....................................................11
11. Number of lines, words and characters in a file..................................................................12
12. Calculate Gross Salary........................................................................................................13
13. Average of numbers – command line arguments................................................................14
14. Greeting Message depending on current time.....................................................................15
15. File test operators................................................................................................................16
16. Count Vowels, Consonants, Numbers in a string................................................................18
17. Phonebook – add/display/search/delete..............................................................................19
18. Compute and display Grade based on marks......................................................................21
19. Sort Numbers in ascending order........................................................................................23
20. System/Login Details..........................................................................................................25
Shell Scripts Page 2 of 25
Date :

1. Area and Perimeter of a Circle
Aim : Write a shell script to find the area and perimeter of a circle

echo "Area and Circumference of a Circle"


echo -n "Enter the radius of the circle : "
read r
a=$(echo "scale=4;3.142857*$r*$r" | bc)
c=$(echo "scale=4;3.142857*$r*2" | bc)
echo "Radius of the circle = $r"
echo "Area of the circle = $a"
echo "Circumference of the circle = $c"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 3 of 25
Date :

2. Menu driven Calculator
Aim : Write a shell script to make a menu driven calculator using case

echo "Menu driven Calculator"


echo -n "Enter the first number : "
read n1
echo -n "Enter the second number : "
read n2
echo -n "Enter your choice (1. Add, 2. Subtract, 3. Multiply, 4. Divide) : "
read opt
case $opt in
1) res=`expr $n1 + $n2`
echo "$n1 + $n2 = $res"
;;
2) res=`expr $n1 - $n2`
echo "$n1 - $n2 = $res"
;;
3) res=`expr $n1 \* $n2`
echo "$n1 * $n2 = $res"
;;
4) res=`expr $n1 / $n2`
echo "$n1 / $n2 = $res"
;;
*) echo "Invalid Choice"
;;
esac

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 4 of 25
Date :

3. Largest of three numbers
Aim : Write a shell script to find the greatest of three numbers

echo "Find the greatest/largest of three numbers"


echo -n "Enter the first number : "
read a
echo -n "Enter the second number : "
read b
echo -n "Enter the third number : "
read c
if [ $a -gt $b ]
then
if [ $a -gt $c ]
then
d=$a
else
d=$c
fi
else
if [ $b -gt $c ]
then
d=$b
else
d=$c
fi
fi
echo "Largest among ($a, $b, $c) is $d"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 5 of 25
Date :

4. Mean and Standard Deviation
Aim : Write a shell script to compute mean and standard deviation of three numbers

echo "Mean and Standard Deviation of three numbers"


echo -n "Enter the first number : "
read a
echo -n "Enter the second number : "
read b
echo -n "Enter the third number : "
read c
s=$(($a+$b+$c))
m=$(echo "scale=4;$s/3" | bc)
var=$(echo "scale=4;((($a-$m)*($a-$m))+(($b-$m)*($b-$m))+(($c-$m)*($c-
$m)))/3" | bc)
std=$(echo "scale=4;(sqrt($var))" | bc)
echo "Arithmetic Mean of $a, $b and $c = $m"
echo "Variance of $a, $b and $c = $var"
echo "Standard deviation of $a, $b and $c = $std"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 6 of 25
Date :

5. Reverse a Number and Sum of digits
Aim : Write a shell script to find reverse of a number

echo "Reverse the digits of a number, and its sum of digts"


echo -n "Enter the number : "
read n
s=0
r=0
m=$n
while [ $m -ne 0 ]
do
d=`expr $m % 10`
s=`expr $s + $d`
r=`expr $r \* 10 + $d`
m=`expr $m / 10`
done
echo "Original Number = $n"
echo "Reversed Number = $r"
echo "Sum of digits = $s"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 7 of 25
Date :

6. List n Fibonacci numbers
Aim : Write a shell script to find n fibonacci numbers

echo "generate N Fibonacii numbers"


echo -n "Enter the limit N : "
read N
c=0
t1=0
t2=1
while [ $c -lt $N ]
do
echo -n "$t1, "
t3=`expr $t1 + $t2`
t1=$t2
t2=$t3
c=`expr $c + 1`
done
echo

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 8 of 25
Date :

7. Check whether a number is Armstrong or not
Aim : Write a shell script to check whether a given number is Armstrong or not

echo "Check whether a given number is Armstrong or Not"


echo -n "Enter a number : "
read N
n=$N
s=0
while [ $n -ne 0 ]
do
d=`expr $n % 10`
s=`expr $s + $d \* $d \* $d`
n=`expr $n / 10`
done
if [ $N -eq $s ]
then
echo "$N is an Armstrong Number"
else
echo "$N is NOT an Armstrong Number"
fi

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 9 of 25
Date :

8. Factorial of a Number
Aim : Write a shell script find the factorial of a given number

echo "Factorial of a given number"


echo -n "Enter a number : "
read N
t=1
f=1
while [ $t -le $N ]
do
f=`expr $f \* $t`
t=`expr $t + 1`
done
echo "Factorial of $N = $f"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 10 of 25
Date :

9. List Prime numbers upto a given number
Aim : Write a shell script to find prime numbers up to a given number

echo "Prime Numbers up to a given number"


echo -n "Enter the limit : "
read n
m=2
while [ $m -le $n ]
do
i=2
f=0
l=`expr $m / 2`
while [ $i -le $l ]
do
x=`expr $m % $i`
if [ $x -eq 0 ]
then
f=1
break
fi
i=`expr $i + 1`
done
if [ $f -eq 0 ]
then
echo -n "$m "
fi
m=`expr $m + 1`
done
echo

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 11 of 25
Date :

10. Reverse a String and Check whether Palindrome or not
Aim : Write a shell script to reverse a string and check whether a given string is palindrome or not

echo -n "Enter the string : "


read str
rstr=""
i=1
l=`expr length "$str"`
while [ $i -le $l ]
do
c=`expr substr "$str" $i 1`
rstr="$c$rstr"
i=`expr $i + 1`
done
echo "Reverse of the given string is '$rstr'"
if [ "$str" = "$rstr" ]
then
echo "the string '$str' is a palindrome"
else
echo "the string '$str' is not a palindrome"
fi

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 12 of 25
Date :

11. Number of lines, words and characters in a file
Aim : Write a shell script to count no of line, words and characters of a input file

echo "Count the number of lines, words and characters in a file"


echo -n "Enter the file name : "
read fname
echo -n "Number of lines : "
wc -l $fname
echo -n "Number of words : "
wc -w $fname
echo -n "Number of charactors : "
wc -c $fname

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 13 of 25
Date :

12. Calculate Gross Salary
Aim : An employee Basic salary is input through keyboard where da is 40% of basic salary and hra is
20% of basic salary. Write a shell script to calculate gross salary

echo "computer Gross Salary"


echo -n "Enter basic salary : "
read salary
da=`expr $salary \* 40 / 100`
hra=`expr $salary \* 20 / 100`
gross=`expr $salary + $hra + $da`
echo "Basic Salary: $salary"
echo "DA: $da"
echo "HRA: $hra"
echo "Gross Salary: $gross"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 14 of 25
Date :

13. Average of numbers – command line arguments
Aim : Write a shell script to find the average of the number entered as command line arguments

echo "Sum and Average of numbers given as commandline arguments"


sum=0
for n in $*
do
sum=`expr $sum + $n`
done
avg=$(echo "scale=2; $sum / $#" | bc -l)
echo "Sum of $# numbers : $sum"
echo "Average of $# numbers : $avg"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 15 of 25
Date :

14. Greeting Message depending on current time
Aim : Write a Shell script which whenever gets executed displays the message “Good Morning/Good
afternoon /Good Evening “ depending on the time it get executed"

echo "Display greeting based on current system time"


echo -n "Crrent Date and Time : "
date
if [ $(date +%H) -le 12 ]
then
echo "Good Morning"
elif [ $(date +%H) -le 16 ]
then
echo "Good Afternoon"
elif [ $(date +%H) -le 20 ]
then
echo "Good Evening"
else
echo "Good Night"
fi

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 16 of 25
Date :

15. File test operators
Aim : Write a shell script which uses all the file test operators

echo "Demonstration of file test operators"


echo -n "Enter a file name : "
read fil
if [ -e $fil ]
then
echo "File named '$fil' exist"
if [ -r $fil ]
then
echo "File named '$fil' has read access"
else
echo "File named '$fil' has not read access"
fi
if [ -w $fil ]
then
echo "File named '$fil' has write access"
else
echo "File named '$fil' has not write access"
fi
if [ -x $fil ]
then
echo "File named '$fil' has execute access"
else
echo "File named '$fil' has not execute access"
fi
if [ -f $fil ]
then
echo "File named '$fil' is an ordinary file"
else
echo "File named '$fil' is a special file"
fi
if [ -d $fil ]
then
echo "File named '$fil' is a directory"
else
echo "File named '$fil' is not a directory"
fi
if [ -s $fil ]
then
echo "File named '$fil' has file size greater than zero"
else
echo "File named '$fil' has file size zero"
fi
else
echo "File named '$fil' does not exist"
fi

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 17 of 25
Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 18 of 25
Date :

16. Count Vowels, Consonants, Numbers in a string
Aim : Write a shell script to find number of vowels, consonants, numbers in a given string.

echo "Number of Vowels, Consonants and Digits in a string"


echo -n "Enter a string : "
read s
l=`expr length "$s"`
m=$l
nv=0
nc=0
nd=0
nz=0
while [ $l -gt 0 ]
do
c=`expr substr "$s" $l 1`
case $c in
[AaEeIiOoUu]) nv=`expr $nv + 1`
;;
[BbCcDdFfGgHhJjKkLlMmNn]) nc=`expr $nc + 1`
;;
[PpQqRrSsTtVvWwXxYyZz]) nc=`expr $nc + 1`
;;
[0-9]) nd=`expr $nd + 1`
;;
*) nz=`expr $nz + 1`
;;
esac
l=`expr $l - 1`
done
echo "Length of the string = $m"
echo "Number of vowels = $nv"
echo "Number of consonents = $nc"
echo "Number of digits = $nd"
echo "Number of Other characters = $nz"

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 19 of 25
Date :

17. Phonebook – add/display/search/delete
Aim : Write a shell script to create a file phonebook and do the following,
add name and phonenumber, display all records, search for a record, delete a record

ch=0
FILE="phonebook.txt"
while [ $ch -le 3 ]
do
echo "Phone book - Add/Search/Display"
echo "1:ADD NEW RECORD 2:DISPLAY 3:SEARCH 4: EXIT"
echo -n "Enter your choice : "
read ch
case $ch in
1) echo -n "Enter name : "
read nm
echo -n "Enter phone no : "
read pn
echo "$nm $pn" >> phonebook.txt
;;
2) echo "Contents of PHONE BOOK :"
cat $FILE
;;
3) echo -n "Enter name to search : "
read nam
phn=`grep $nam $FILE | cut -f2 -d" "`
if [ "$phn" != "" ]
then
echo "Phone no of '$nam' is '$phn'"
else
echo "Name '$nam' is not found in phone book"
fi
;;
4) break;
;;
*) echo "invalid choice!!"
;;
esac
echo ""
done

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 20 of 25
Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 21 of 25
Date :

18. Compute and display Grade based on marks
Aim : Write a shell script to accept student number, name, marks in 5 subjects. Find total, average and
grade.
Rules: avg>=90 then grade A
Avg<90 && Avg>=80 then grade B
Avg<80 && Avg>=60 then grade C
Avg<60 && Avg>=40 then grade D
Avg<40 then grade E

echo -n "Enter student's Register Number : "


read regno
echo -n "Enter student's Name : "
read sname
sum=0
avg=0
i=1
while [ $i -le 5 ]
do
echo -n "Enter Marks in subject $i : "
read marks[$i]
sum=`expr $sum + ${marks[$i]}`
i=`expr $i + 1`
done
avg=`expr $sum / 5`
if [ $avg -ge 90 ]
then
grade="A"
elif [ $avg -ge 80 ]
then
grade="B"
elif [ $avg -ge 60 ]
then
grade="C"
elif [ $avg -ge 40 ]
then
grade="D"
else
grade="E"
fi
echo "Total Marks Scored = $sum"
echo "Average = $avg"
echo "Grade = $grade"

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 22 of 25
Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 23 of 25
Date :

19. Sort Numbers in ascending order
Aim : Write a shell script to read 10 numbers in a array and sort the number in ascending order

echo "Read and Sort 10 numbers in acending order"


i=0
while [ $i -le 9 ]
do
echo -n "Enter number ( $i ) : "
read arr[$i]
i=$(($i+1))
done
i=0
while [ $i -le 9 ]
do
j=$(($i+1))
while [ $j -le 9 ]
do
if [ ${arr[$i]} -gt ${arr[$j]} ]
then
temp=${arr[$i]}
arr[$i]=${arr[$j]}
arr[$j]=$temp
fi
j=$(($j+1))
done
i=$(($i+1))
done
echo -n "Sorted Array is :: "
i=0
while [ $i -le 9 ]
do
echo -n ${arr[$i]} " "
i=$(($i+1))
done
echo ""

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 24 of 25

B. Sc. Computer Science College of Applied Science, Vadakkencherry


Shell Scripts Page 25 of 25
Date :

20. System/Login Details
Aim : Write a shell script to display current date and time, number of users, terminal name, login date
and time

echo "Display System / Login details"


echo -n "Current Date and Time : "
date
echo -n "Number of Logged-in Users : "
who | wc -l
echo "User tty name Login Date and Time"
who

Output :

B. Sc. Computer Science College of Applied Science, Vadakkencherry

You might also like