IsraelTorres icon

calcperc.sh

IsraelTorres | PRO | 03/23/12 09:26:09 PM UTC | 0 ⭐ | 348 👁️ | Never ⏰ | []
Bash |

870 B

|

None

|

0 👍

/

0 👎

#!/bin/bash
:<<israeltorres_comment_block
 
./calcperc.sh
Israel Torres
2012-03-23
Quickly calculate percentage of original loss
 
Mac OS X 10.7.3 11D50d 
Darwin Kernel Version 11.3.0
GNU bash, version 3.2.48(1)-release 
 
israeltorres_comment_block
 
#required app check
rqdapp='bc'
hash $rqdapp 2>&- || { echo >&2 "$rqdapp binary not found - aborting"; exit 1; }
#
if [ ! $# -ne 2 ]; then
ORIGINAL=$1
CURRENT=$2
SCALE=2 # adjust scale accordingly
# ((original - current)/original)x100
function calculatepercentage() {
ORIGINAL=$1; CURRENT=$2
RESULT1=$(echo "scale=$SCALE; $ORIGINAL-$CURRENT" | bc)
RESULT2=$(echo "scale=$SCALE; $RESULT1/$ORIGINAL" | bc)
RESULT3=$(echo "scale=$SCALE; $RESULT2*100" | bc)
RESULT3=$(echo "$RESULT3" | cut -d "." -f 1)
echo "$RESULT3%"
}
 
calculatepercentage $ORIGINAL $CURRENT
 
else
echo "usage: $0 'original' 'current'"
echo "example: $0 272 224.6"
fi
#eof

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎