0% found this document useful (0 votes)
4 views3 pages

Foss Exp-4

Uploaded by

cherrybhai.gff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Foss Exp-4

Uploaded by

cherrybhai.gff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

a) Write a shell script (small calculator) that adds, subtracts, multiplies and
divides the two
given numbers. There are two division options: one returns the quotient and the
other remainder.
The script requires three arguments: the operation to be used and the two integers.
The operation
is specified by options:
Add -a
Subtract-s
Multiply -m
Quotient -c
Remainder –r

# !/bin/bash
# Take user Input
echo "Enter Two numbers : "
read a
read b
# Input type of operation
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch
# Switch Case to perform
# calulator operations
case $ch in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a - $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo "scale=2; $a / $b" | bc`
;;
esac
echo "Result : $res"

4b.Write a shell script to accept student number, name, and marks in five subjects.
Find total,
average and grade.
Rules:
Avg>=80 then grade A
Avg<80&&Avg>=70 then grade B
Avg<70&&Avg>=60 then grade C
Avg<60&&Avg>=50 then grade D
Avg<50&&Avg>=40 then grade E
Else grade F

echo -----------------------------------
echo '\tStudent Mark List'
echo -----------------------------------
echo Enter the Student name
read name
echo Enter the Register number
read rno
echo Enter the Mark1
read m1
echo Enter the Mark2
read m2
echo Enter the Mark3
read m3
echo Enter the Mark4
read m4
echo Enter the Mark5
read m5
tot=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
avg=$(expr $tot / 5)
echo -----------------------------------
echo '\tStudent Mark List'
echo -----------------------------------
echo "Student Name : $name"
echo "Register Number : $rno"
echo "Mark1 : $m1"
echo "Mark2 : $m2"
echo "Mark3 : $m3"
echo "Mark4 : $m4"
echo "Mark5 : $m5"
echo "Total : $tot"
echo "Average : $avg"
if [ $m1 -ge 35 ] && [ $m2 -ge 35 ] && [ $m3 -ge 35 ] && [ $m4 -ge 35 ] && [ $m5 -
ge 35 ]
then
echo "Result : Pass"

if [ $avg -ge 90 ]
then
echo "Grade : S"
elif [ $avg -ge 80 ]
then
echo "Grade : A"
elif [ $avg -ge 70 ]
then
echo "Grade : B"
elif [ $avg -ge 60 ]
then
echo "Grade : C"
elif [ $avg -ge 50 ]
then
echo "Grade : D"
elif [ $avg -ge 35 ]
then
echo "Grade : E"
fi
else
echo "Result : Fail"
fi
echo -----------------------------------

4c.Write a shell script to find the GCD of two given numbers.


echo "Enter first number"
read n1
echo "Enter the second number"
read n2
gcd=0
if test $n1 -gt $n2
then
i=1
while test $i -le $n1
do
a=`expr $n1 % $i`
b=`expr $n2 % $i`
if test $a -eq 0 -a $b -eq 0
then
if test $gcd -lt $i
then
gcd=$i
fi
fi
i=`expr $i + 1`
done
fi
if test $n2 -gt $n1
then
i=1
while test $i -le $n2
do
a=`expr $n1 % $i`
b=`expr $n2 % $i`
if test $a -eq 0 -a $b -eq 0
then
if test $gcd -lt $i
then
gcd=$i
fi
fi
i=`expr $i + 1`
done
fi
echo GCD of $n1 and $n2 = $gcd

You might also like