-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.sh
45 lines (39 loc) · 963 Bytes
/
sol.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
#!/bin/bash
# Check if the correct number of arguments is provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <operand1> <operator> <operand2>"
exit 1
fi
# Assign command line arguments to variables
operand1=$1
operator=$2
operand2=$3
# Perform arithmetic operation
result=0
case $operator in
"+")
result=$((operand1 + operand2))
;;
"-")
result=$((operand1 - operand2))
;;
"*")
result=$((operand1 * operand2))
;;
"/")
if [ "$operand2" -eq 0 ]; then
echo "Error: Division by zero is not allowed."
exit 1
fi
result=$(echo "scale=2; $operand1 / $operand2" | bc)
;;
*)
echo "Error: Invalid operator. Supported operators are +, -, *, /"
exit 1
;;
esac
# Print the result to the console
echo "Result: $result"
# Store the result in a file
echo "Result: $result" > result.txt
echo "Result stored in 'result.txt'"