**Shell Script** For your final Linux project, you will create a menu driven, us
ID: 3579856 • Letter: #
Question
**Shell Script**
For your final Linux project, you will create a menu driven, user-friendly shell script that allows a user to calculate the results for several common mathematical expressions. In each case, the script will prompt the user for the required input and the provide the output.
The mathematical expressions to be implemented include:
a. Slope of a line given two points (m = (y2-y1)/(x2-x1)
b. average for an undetermined number of input integers (avg = (x1+x2 + x3 + x4 ... +xn)/n
c. Simple interest (Interest = Principle * Rate * time in years)
d. Area of a rectangle (area = length * width)
e. Your mathematical formula
Submit a phot of a working program.
Explanation / Answer
Assuming by option e, you mean any mathematical formula, i chose perimeter of rectangle.
Along with that, as shell doesnt support float multiplication, I use python to output the results, for correction of the program.
#!/bin/bash
a=1;
while [ $a -ne 0 ]
do
echo 'Enter 1 to find slope of line given two points';
echo 'Enter 2 to find average of some numbers';
echo 'Enter 3 to find simple interest';
echo 'Enter 4 to find area of rectangle';
echo 'Enter 5 to find perimeter of rectangle'; #assuming we can write our own formula
echo 'Enter 0 to quit';
read -p 'Enter you input: ' a;
if [ $a -eq 1 ]
then
echo 'Ente first point';
read -p 'Enter x: ' x0;
read -p 'Enter y: ' y0;
echo 'Ente second point';
read -p 'Enter x: ' x1;
read -p 'Enter y: ' y1;
slopeY=$((y1-y0));
slopeX=$((x1-x0));
python -c "print "Slope of the line is:",($slopeY*1.0)/$slopeX," "";
elif [ $a -eq 2 ]
then
read -p 'Enter total numbers: ' n;
avg=0;
n0=$n;
while [ $n -ne 0 ]
do
read -p 'Enter your number: ' x;
avg=$((avg+x));
n=$((n-1));
done
python -c "print "Average of the numbers is:",($avg*1.0)/$n0," "";
elif [ $a -eq 3 ]
then
read -p 'Enter principle: ' principle;
read -p 'Enter rate: ' rate;
read -p 'Enter time in years ' years;
python -c "print "Simple interest is:", $principle*$rate*$years," "";
elif [ $a -eq 4 ]
then
read -p "Enter length of rectangle: " length;
read -p "Enter width of rectangle: " width;
python -c "print "Area of rectangle is:", $length*$width," "";
elif [ $a -eq 5 ]
then
read -p "Enter length of rectangle: " length;
read -p "Enter width of rectangle: " width;
python -c "print "Perimeter of rectangle is:", 2*($length+$width)," "";
else
echo 'Invalid Option, Enter again';
fi
done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.