0% found this document useful (0 votes)
9 views8 pages

Abdullah - Al - Galib - A (OS Calculator Assignment)

The document outlines an assignment for creating a simple calculator using shell scripting as part of a computer science course. It includes an introduction to shell scripting, the code for the calculator, and a discussion on the learning outcomes from the project. The calculator performs basic arithmetic operations and demonstrates key programming concepts such as loops, conditionals, and error handling.

Uploaded by

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

Abdullah - Al - Galib - A (OS Calculator Assignment)

The document outlines an assignment for creating a simple calculator using shell scripting as part of a computer science course. It includes an introduction to shell scripting, the code for the calculator, and a discussion on the learning outcomes from the project. The calculator performs basic arithmetic operations and demonstrates key programming concepts such as loops, conditionals, and error handling.

Uploaded by

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

BANGLADESH UNIVERSITY OF PROFESSIONALS

FACULTY OF SCIENCE & TECHNOLOGY


DEPT. OF COMPUTER SCIENCE & ENGINEERING (CSE)

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.

STUDENT NAME : Abdullah Al Galib


ROLL NO. : 2252421049
SECTION :A
SEMESTER : 5th
LEVEL/TERM : 3rd
COURSE NAME :Operating System Laboratory

DATE OF SUBMISSION : 15/12/2024


Introduction:
Shell scripting is a handy tool in Unix/Linux systems that helps users automate tasks, manage
system processes, and make small programs. One of the basic yet useful programs you can write
is a simple calculator. A calculator script allows users to do basic math operations like addition,
subtraction, multiplication, and division.

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 2 :Code part-2


Code in Output:

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy