-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek6.5
More file actions
54 lines (47 loc) · 939 Bytes
/
week6.5
File metadata and controls
54 lines (47 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
5. Write a shell program to simulate a simple calculator.
#!/bin/bash
# function to add two numbers
addition() {
echo "$1 + $2 = $(($1 + $2))"
}
# function to subtract two numbers
subtraction() {
echo "$1 - $2 = $(($1 - $2))"
}
# function to multiply two numbers
multiplication() {
echo "$1 * $2 = $(($1 * $2))"
}
# function to divide two numbers
division() {
if [ $2 -eq 0 ]; then
echo "Error: division by zero"
else
echo "$1 / $2 = $(($1 / $2))"
fi
}
# read the operator and two numbers from user input
echo "Enter operator (+, -, *, /):"
read operator
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
# call the appropriate function based on the operator
case $operator in
+)
addition $num1 $num2
;;
-)
subtraction $num1 $num2
;;
\*)
multiplication $num1 $num2
;;
/)
division $num1 $num2
;;
*)
echo "Error: invalid operator"
;;
esac