Date

GNU/Linux Just a quick way to Utilize a single query result value

Just a quick way to utilize a single query result value in a bash variable.

#!/bin/bash

# Just a bunch of dates relative to today...
TDAY=`date +%d`
MDAY=`date +%d -d "yesterday"`
TYMDAY=`date +%Y-%m-%d -d "yesterday"`
TMDAY=`date +%m-%d -d "yesterday"`
TY1DAY=`date +%Y-01-01 -d "yesterday"`
LYMDAY=`date +%Y-%m-%d -d "yesterday last year"`
LY1DAY=`date +%Y-01-01 -d "yesterday last year"`
YYEAR=`date +%Y -d "yesterday"`
LYEAR=`date +%Y -d "yesterday last year"`

# Get values from database...
Zl=$(mysql InfoRobot -u root --execute="SELECT COALESCE(ZOpbrengst, 0) AS ZOpbrengst from EDagCombi WHERE COALESCE(ZOpbrengst, -1) > -1 ORDER BY Jaar DESC, Maand DESC, Dag DESC LIMIT 1;" --silent)

DM=$(mysql InfoRobot -u root --execute="SELECT CONCAT(DAY(MAX(Tijdstip)), ' ', MONTHNAME(MAX(Tijdstip))) AS DayName from ZOpbrengst WHERE Tijdstip >  (now() - interval 2 day);" --silent)

ZM=$(mysql InfoRobot -u root --execute="SELECT ROUND(MaandTotaal) from ZMaandOpbrengst where Maand = (SELECT ThisMonth From CZThisMonth LIMIT 1);" --silent)

ZMLY=$(mysql InfoRobot -u root --execute="SELECT ROUND(MaandTotaal) from ZMaandOpbrengst where Maand = (SELECT ThisMonthLastYear From CZThisMonth LIMIT 1);" --silent)

Z1LY=$(mysql InfoRobot -u root --execute="SELECT ROUND(SUM(Opbrengst)) FROM ZOpbrengst WHERE Tijdstip >= '${LY1DAY}' and Tijdstip <= '${LYMDAY}';" --silent)

Z1TY=$(mysql InfoRobot -u root --execute="SELECT ROUND(SUM(Opbrengst)) FROM ZOpbrengst WHERE Tijdstip >= '${TY1DAY}' and Tijdstip <= '${TYMDAY}';" --silent)

P1TY=$(mysql InfoRobot -u root --execute="SELECT (MAX(T1afgenomen) - MIN(T1afgenomen)) + (MAX(T2afgenomen) - MIN(T2afgenomen)) FROM P1uitlezen WHERE date >= '${TY1DAY}' and date <= '${TYMDAY}';" --silent)

# Bash math functions using bc...
ZXPLY=$(echo "scale=2; (${ZM}/${ZMLY})" | bc)
ZZ=$(echo "scale=0; (${ZXPLY}*100)" | bc)
ZXYLY=$(echo "scale=2; ((${Z1TY}/${Z1LY})*100)" | bc)
FZ=$(echo "scale=0; (${ZXYLY})" | bc | xargs printf "%.*f" "0")

# Mail stuff...
/usr/local/sbin/send_to_mail.py SolarPower "${DM}: ${Zl} KWh, Total TY: ${Z1TY} KWh (${FZ}%)" > /dev/null