-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_v7.sh
executable file
·54 lines (48 loc) · 947 Bytes
/
calc_v7.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
#!/bin/bash
isNumeric() {
n=$1
re='^[0-9]+$'
if [[ $n =~ $re ]]; then
return 0
else
return 1
fi
}
operate() {
n1=$1
n2=$2
operation=$3
if ! isNumeric "$n1"; then
echo "El primer valor no es numérico" >&2
return 1
fi
if ! isNumeric "$n1"; then
echo "El segundo valor no es numérico" >&2
return 1
fi
if [[ "$operation" == "+" ]]; then
result=$((n1+n2))
elif [[ "$operation" == "-" ]]; then
result=$((n1-n2))
elif [[ "$operation" == "*" ]]; then
result=$((n1*n2))
elif [[ "$operation" == "/" ]]; then
if [[ $n2 -eq 0 ]]; then
echo "No se puede dividir entre 0" >&2
return 1
else
result=$((n1/n2))
fi
else
echo "Operación no encontrada" >&2
return 1
fi
echo $result
}
operation="+"
for n1 in $(seq 0 5); do
for n2 in $(seq 0 5); do
result=$(operate "$n1" "$n2" "$operation")
echo "${n1} + ${n2} = ${result}"
done
done