Advertisement
Guest User

pgraph.sh

a guest
Mar 27th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.17 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # PercentageGraph (pgraph)
  4. # Basic ASCII Graphing Tool
  5. #
  6. # CSV format: Name,Used,Total (or Maximum)
  7. #
  8. # Awk isn't perfect at rounding.. .5 rounds down
  9. #
  10. # v1.1 sol@subnetzero.org
  11.  
  12. if [ -z $1 ]; then
  13.         printf "Usage: pgraph [datafile]\n"
  14.         exit 1
  15. fi
  16.  
  17. # Set Vars
  18. # FILLER and ENDDELIM are used for drawing bars.
  19. ENDDELIM="="
  20. FILLER="="
  21. SCALE=40
  22. INPUTFILE=$1
  23. NAME=(`awk -F"," '{print $1}' < "$INPUTFILE"`)
  24. USED=(`awk -F"," '{print $2}' < "$INPUTFILE"`)
  25. TOTAL=(`awk -F"," '{print $3}' < "$INPUTFILE"`)
  26.  
  27. # Get Max qty for scaling
  28. MAXQTY=0
  29. for VALUE in ${TOTAL[*]}
  30. do
  31.         if [ "$VALUE" -gt "$MAXQTY" ]; then
  32.                 MAXQTY=$VALUE
  33.         fi
  34. done
  35.  
  36. #echo "max is $MAXQTY"
  37.  
  38. # Make graph header and markings
  39. printf "\n Resources\n"
  40. printf "____________________0"
  41. QTRSCALE=`echo "$SCALE / 4" | bc -l | awk '{printf("%.0f",$0)}'`
  42. HALFSCALE=`echo "$SCALE / 2" | bc -l | awk '{printf("%.0f",$0)}'`
  43. THRSCALE=`echo "$SCALE * 0.75" | bc -l | awk '{printf("%.0f",$0)}'`
  44. LCNT=1
  45. while [ "$LCNT" -le "$SCALE" ];
  46. do
  47.         case $LCNT in
  48.                 $QTRSCALE)      printf ".";;
  49.                 $HALFSCALE)     printf "|";;
  50.                 $THRSCALE)      printf ".";;
  51.                 $SCALE)         printf "|100%%\n";;
  52.                 *)              printf "_";;
  53.         esac
  54.         LCNT=$(( $LCNT + 1 ))
  55. done
  56.  
  57. # Draw graph bars
  58. i=0
  59. for ITEM in ${NAME[*]}
  60. do
  61.         # Print Category name in format along with info and bars
  62.         LENGTH=`echo "scale=2;(( ${USED[$i]} / ${TOTAL[$i]} ) * $SCALE )" |\
  63.                 bc | \
  64.                 awk '{printf("%.0f",$0)}'`
  65.         PCT=`echo "scale=2;(( ${USED[$i]} / ${TOTAL[$i]} ) * 100)" |\
  66.              bc |\
  67.              awk '{printf("%.0f",$0)}'`
  68.         printf "%-12.12s (%3.3s%%) |" "$ITEM" "$PCT"
  69.         BLOCKS=""
  70.         while [ "$LENGTH" -gt "0" ]; do
  71.                 if [ "$LENGTH" -eq "1" ]; then
  72.                         BLOCKS="$BLOCKS$ENDDELIM"
  73.                 else
  74.                         BLOCKS="$BLOCKS$FILLER"
  75.                 fi
  76.                 LENGTH=$(( $LENGTH - 1 ))
  77.         done
  78.         printf "$BLOCKS\n"
  79.         i=$(( $i + 1 ))
  80. done
  81. #printf "\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement