Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement