-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini-calc.sh
92 lines (75 loc) · 1.97 KB
/
mini-calc.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# Mini script for basic calculation (addition, substraction, dividing, multiplexing)
# Pass two digits in following manner ./mini-calc [arg1] [arg2]
declare -i num1
declare -i num2
function argu() {
if [ "$1" == 2 ]; then
return 0
else
echo "Wrong number of arguments you should pass exactly two like: bash mini-calc.sh [arg1] [arg2] "
exit 1
fi
}
function checkIfNum() {
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
echo "Error! You passed the number in wrong format. Please pass the numbers"
exit 1
fi
}
function adding () {
result=$(( "$1" + "$2" ))
echo ""$1" + "$2" = "$result""
}
function substracting () {
result=$(( "$1" - "$2" ))
echo ""$1" - "$2" = "$result""
}
function multiplexing () {
result=$(( "$1" * "$2" ))
echo ""$1" * "$2" = "$result""
}
function dividing () {
echo ""
echo "ATTENTION CALCULATOR WILL RETURN INTEGER VALUE ! "
echo ""
if [ "$2" == 0 ]; then
echo "DIVIDING BY ZERO NOT ALLOWED. Exiting ..."
exit 1
else
result=$(( "$1" / "$2" ))
echo ""$1" / "$2" = "$result" "
echo ""
fi
}
function main() {
echo ""
echo "______________________________CALCULATOR_______________________________________"
echo "YOU HAVE PASSED APPROPRIATE NUMBER OF ARGUMENTS ! TELL ME WHAT YOU WANT TO DO: "
echo "--------------------------------------------------------------------------------"
echo " "
select y in ADD SUBS DIV MUL Quit
do
case $y in
"ADD") echo "YOU CHOSE: ADDITION "
adding $1 $2 ;;
"SUBS") echo "YOU CHOSE: SUBSTRACTION "
substracting $1 $2 ;;
"DIV") echo "YOU CHOSE: DIVISION "
dividing $1 $2 ;;
"MUL") echo "YOU CHOSE: MULTIPLEXING "
multiplexing $1 $2
;;
"Quit") exit ;;
*) echo "YOU DIDN'T MAKE APPRPRIATE DECISION ! Exiting ... "
exit 1
esac
break
done
}
number="$#"
argu $number
checkIfNum $1
checkIfNum $2
main $1 $2