Advertisement
zefie

greyhole_monitor.sh (feat. fancy timer and a callback)

Jan 1st, 2019
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.32 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function right-aligned() {
  4.         SPC=$(expr $(tput cols) - ${#1})
  5.         for (( j=1; j<=$SPC; j++ )); do
  6.                 echo -n " ";
  7.         done;
  8.         echo -n ${1}
  9. }
  10.  
  11. function fancy-timer() {
  12.         for (( i=1; i<=$1; i++ )); do
  13.                 SEC=$(expr ${1} - $(expr ${i} - 1))
  14.                 SECW="second"
  15.                 if [ "$SEC" -ne "1" ]; then
  16.                         SECW="${SECW}s"
  17.                 fi
  18.                 echo -ne "$(right-aligned "${2} ${SEC} ${SECW}")\r"
  19.                 sleep 1
  20.         done;
  21.         if [ ! -z "${3}" ]; then
  22.                 # Callback
  23.                 ${3} ${1} $(date +'%s')
  24.         fi
  25. }
  26.  
  27. function greyhole_monitor() {
  28.         Z_GHD=$(greyhole -s);
  29.         Z_GHS=$(greyhole -S);
  30.         clear;
  31.         echo -e "${Z_GHD}\\n";
  32.         echo -e "${Z_GHS}\\n";
  33.         df -h /home;
  34.         Z_TIME=${1}
  35.         if [ ! -z "${2}" ]; then
  36.                 Z_DATE=$(date +'%s')
  37.                 Z_DIFF=$(expr ${Z_DATE} - ${2})
  38.                 Z_TIME=$(expr ${Z_TIME} - ${Z_DIFF})
  39.         fi
  40.         fancy-timer ${Z_TIME} "Updated in ${Z_DIFF}s on $(date -d"@${Z_DATE}" +'%I:%M:%S%p [%Y/%m/%d]'), next update in" greyhole_monitor
  41. }
  42. clear
  43. echo "Gathering initial information from Greyhole... This may take a while..."
  44. greyhole_monitor 25 $(date +'%s')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement