Abdullah - Al - Galib - A (OS Calculator Assignment)
Abdullah - Al - Galib - A (OS Calculator Assignment)
Assignment-1
COURSE CODE:
C S E - 3 1 0 7
EXP. NO.: 01
Title: Write a shell script to create a simple calculator for basic arithmetic operations.
With simple commands like echo to show messages and read to take input, and tools like bc for
calculations, you can easily make this program. The script works through a text-based interface
where the user enters numbers and chooses the operation they want to do. It uses conditions like
if-else or case to handle user choices and checks for errors, like dividing by zero. A loop can also
be added so users can do multiple calculations in one session.
Creating this script is a great way for beginners to start learning shell scripting. It teaches key
concepts like taking inputs, making decisions, and handling errors. Plus, this kind of script shows
how useful shell scripting can be for automating small but important tasks.
Code:
#!/bin/bash
while true
do
echo "Select an operation:"
echo
echo "1. Addition (+)"
echo "2. Subtraction (-)"
echo "3. Multiplication (*)"
echo "4. Division (/)"
echo "5. Exit"
echo
read -p "Enter Operator (1-5): " get
case $get in
1)
read -p "Enter the first number: " a
read -p "Enter the second number: " b
answer=$((a + b))
echo "Result: $a + $b = $answer"
echo
;;
2)
read -p "Enter the first number: " a
read -p "Enter the second number: " b
answer=$((a - b))
echo "Result: $a - $b = $answer"
echo
;;
3)
read -p "Enter the first number: " a
read -p "Enter the second number: " b
answer=$((a * b))
echo "Result: $a * $b = $answer"
echo
;;
4)
read -p "Enter the first number: " a
read -p "Enter the second number: " b
if [ $b -eq 0 ]
then
echo "Sorry! Divisor cannot be zero."
else
answer=$(echo "scale=2; $a / $b" | bc)
echo "Result: $a / $b = $answer"
fi
echo
;;
5)
echo "Successfully exited"
break
;;
*)
echo "Select between 1 and 5."
echo
;;
esac
done
Code in console:
Fig 1 :Code part-1
Fig : Addition
Fig : Subtraction
Fig : Multiplication
Fig : Division
Fig : Exit
Discussion:
The "Simple Calculator Using Shell Scripting" assignment showed how shell scripting can be
used to make a basic tool for doing arithmetic operations. This project gave me a chance to
practice important programming ideas like loops, conditionals, and error handling, along with
using common Unix/Linux commands. The calculator script used decision-making structures
like case and if-else to handle user input and perform the required operations. Loops were added to
let users do multiple calculations without restarting the program, making it more user-friendly.
Commands like echo and read were used to make an interactive interface that allowed easy
communication between the program and the user. The script was written in a simple and
organized way, with separate sections for showing the menu and doing the calculations. This
made the code easier to read and change. The project showed how shell scripting can automate
small tasks while being simple and flexible.
Completing this assignment helped learners understand the basics of shell scripting and gave
them an idea of how it can be used in real-world tasks. This knowledge can be a starting point to
create more advanced scripts in the future.