IsraelTorres

calcperc.sh

Mar 23rd, 2012
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. :<<israeltorres_comment_block
  3.  
  4. ./calcperc.sh
  5. Israel Torres
  6. 2012-03-23
  7. Quickly calculate percentage of original loss
  8.  
  9. Mac OS X 10.7.3 11D50d
  10. Darwin Kernel Version 11.3.0
  11. GNU bash, version 3.2.48(1)-release
  12.  
  13. israeltorres_comment_block
  14.  
  15. #required app check
  16. rqdapp='bc'
  17. hash $rqdapp 2>&- || { echo >&2 "$rqdapp binary not found - aborting"; exit 1; }
  18. #
  19. if [ ! $# -ne 2 ]; then
  20. ORIGINAL=$1
  21. CURRENT=$2
  22. SCALE=2 # adjust scale accordingly
  23. # ((original - current)/original)x100
  24. function calculatepercentage() {
  25. ORIGINAL=$1; CURRENT=$2
  26. RESULT1=$(echo "scale=$SCALE; $ORIGINAL-$CURRENT" | bc)
  27. RESULT2=$(echo "scale=$SCALE; $RESULT1/$ORIGINAL" | bc)
  28. RESULT3=$(echo "scale=$SCALE; $RESULT2*100" | bc)
  29. RESULT3=$(echo "$RESULT3" | cut -d "." -f 1)
  30. echo "$RESULT3%"
  31. }
  32.  
  33. calculatepercentage $ORIGINAL $CURRENT
  34.  
  35. else
  36. echo "usage: $0 'original' 'current'"
  37. echo "example: $0 272 224.6"
  38. fi
  39. #eof
RAW Paste Data