On Thu, Jul 27, 2006 at 03:51:15PM -0400, Peter wrote:
> I am writing a shell script to handle simple IP accounting and I'm
> getting an error I cannot solve. Here is the pertinent snippet:
>
> PORT_IN=$(pfctl -sl | grep $i | grep $LABEL | cut -d ' ' -f 9) # bytes
> PORT_IN=$(echo "scale=3; $PORT_IN / 1024" | bc) # kilobytes
> PORT_IN_SUM=$(cat $IN_DIR/$LABEL) # current
> value
> PORT_IN_SUM=$(expr $PORT_IN_SUM + $PORT_IN) # add new value
> echo $PORT_IN_SUM > $IN_DIR/$LABEL # send
if you absolutely must have it in kilobytes with three decimal places:
how about:
> PORT_IN=$(pfctl -sl | grep $i | grep $LABEL | cut -d ' ' -f 9)
> PORT_IN=$(echo "scale=3; $PORT_IN / 1024" | bc)
PORT_IN_THOUSANDS=${PORT_IN%.*}
PORT_IN_ONES=${PORT_IN#*.}
> PORT_IN_SUM=$(cat $IN_DIR/$LABEL)
PORT_IN_SUM_THOUSANDS=${PORT_IN_SUM%.*}
PORT_IN_SUM_ONES=${PORT_IN_SUM#*.}
> PORT_IN_SUM=$(expr $PORT_IN_SUM + $PORT_IN)
PORT_IN_SUM=$(echo "scale=3; ((($PORT_IN_THOUSANDS + $PORT_IN_SUM_THOUSANDS) *
1000) \
+ ($PORT_IN_ONES + $PORT_IN_SUM_ONES)) / 1000" | bc)
--
jared
[ openbsd 3.9-current GENERIC ( jun 22 ) // i386 ]