-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.sh
49 lines (43 loc) · 822 Bytes
/
eval.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
#!/bin/bash
if [ "$#" -ne 1 ] || ! [ -r "$1" ]; then
echo "Invalid parameters" >&2
exit 1
fi
l1=` grep ".* +" $1 | wc -l `
l2=` grep ".* -" $1 | wc -l `
l3=` grep ".* /" $1 | wc -l `
l4=` grep ".* \*" $1 | wc -l `
let size1=$l1+$l2+$l3+$l4
size2=` grep . $1 | wc -l `
if [ $size1 -ne $size2 ]
then
echo "File format not supported" >&2
exit 1
fi
let result=0
x=0
while read line
do
x=$(( x+1 ))
operand=`sed -n "$x"p $1 | cut -d' ' -f 1`
operator=`sed -n "$x"p $1 | cut -d' ' -f 2`
case $operator in
+)
result=$(( result + operand ))
;;
-)
result=$(( result - operand ))
;;
\*)
result=$(( result * operand ))
;;
/)
result=$(( result / operand ))
;;
*)
echo Line $x not supported by script
;;
esac
done <<< "$( cat "$1" )"
echo $result
exit 0