Results 1 to 1 of 1
The following script reads some analog data (floating point) checks to make sure its within range and then writes the output to a trend application. It seems very long for ...
- 06-21-2007 #1Just Joined!
- Join Date
- Aug 2006
- Posts
- 8
Bash Script works but probably needs optimising...suggestions Please
The following script reads some analog data (floating point) checks to make sure its within range and then writes the output to a trend application. It seems very long for what I want to do. Especially the part of testing the results are within range, I had to do a stupid cut command as the -gt -lt expected an integer.
Any observations on how I can optimise this code. As it runs on an embedded linux device anyway I can shorten the prog would benefit my memory usage. Would it benefit from perl ?
#!/bin/bash
OWSERVER="-s 3002"
while true
do
### Force Simulataneous 1wire conversion ###
/opt/bin/owwrite $OWSERVER simultaneous/voltage 1
/opt/bin/owwrite $OWSERVER simultaneous/temperature 1
### Read 1wire data and convert into engineering units #ph was 2.6667##
PH=$(/opt/bin/owread $OWSERVER 20.B71F05000000/volt.A | /opt/bin/awk '{printf "%4.2f\n",((($0-1)/1.6)+5.5)}')
US=$(/opt/bin/owread $OWSERVER 20.B71F05000000/volt.B | /opt/bin/awk '{printf "%5.1f\n",($0*100)}')
ORP=$(/opt/bin/owread $OWSERVER 20.B71F05000000/volt.D | /opt/bin/awk '{printf "%5.1f\n",($0*100)}')
TempAqua=$(/opt/bin/owread $OWSERVER 20.B71F05000000/volt.C | /opt/bin/awk '{printf "%3.1f\n",((($0-1)*5)+20)}')
LITRES=$(/opt/bin/owread $OWSERVER 20.952A05000000/volt.D | /opt/bin/awk '{printf "%3.1f\n",(($0-2.900)*60)}')
TempOut=$(/opt/bin/owread $OWSERVER 28.11BF97000000/temperature | /opt/bin/awk '{printf "%3.1f\n",($0)}')
TempAmb=$(/opt/bin/owread $OWSERVER 28.D70598000000/temperature | /opt/bin/awk '{printf "%3.1f\n",($0)}')
TempRes=$(/opt/bin/owread $OWSERVER 28.A6EE97000000/temperature | /opt/bin/awk '{printf "%3.1f\n",($0)}')
Accum_flow=$(/opt/bin/owread $OWSERVER 1D.EBEF0900000034/counters.A | /opt/bin/awk '{printf "%g\n",($0/4100)}')
### Out of Range Error trap -Do not log data ###
PHtest=`echo $PH |cut -f1 -d'.'`
if [ $PHtest -gt 8 ] || [ $PHtest -lt 5 ]; then
PH=U
echo "`date` PH reading error value $PH" >>/public/nohup.out
fi
ORPtest=`echo $ORP |cut -f1 -d'.'`
if [ $ORPtest -gt 450 ] || [ $ORPtest -lt 50 ]; then
ORP=U
echo "`date` ORP reading error value $ORP" >>/public/nohup.out
fi
UStest=`echo $US |cut -f1 -d'.'`
if [ $UStest -gt 400 ] || [ $UStest -lt 100 ]; then
US=U
echo "`date` Conductivity reading error value $US" >>/public/nohup.out
fi
TEMPtest=`echo $TempAqua |cut -f1 -d'.'`
if [ $TEMPtest -gt 50 ] || [ $TEMPtest -lt 5 ]; then
TempAqua=U
echo "`date` Aquarium Temp reading error, value $TempAqua" >>/public/nohup.out
fi
### Calculations based upon Data ###
C12=$(/opt/bin/awk -v PH="$PH" '{printf "%3.1f\n",((3*$0)*(10^(7-PH)))}' /public/kh_value.txt)
Todayflow=$(/opt/bin/awk -v AF="$Accum_flow" '{printf "%g\n",(AF-$0)}' /public/lastflow.txt)
Weekflow=$(/opt/bin/awk -v AF="$Accum_flow" '{printf "%g\n",(AF-$0)}' /public/weekflow.txt)
### Push Data into NEW Trend Files ###
rrdtool update /public/rrd/aqua_ph.rrd N:$PH
rrdtool update /public/rrd/aqua_orp.rrd N:$ORP
rrdtool update /public/rrd/aqua_temp.rrd N:$TempAqua:$TempRes
rrdtool update /public/rrd/aqua_co2.rrd N:$C12
rrdtool update /public/rrd/aqua_cond.rrd N:$US
rrdtool update /public/rrd/aqua_level.rrd N:$LITRES
rrdtool update /public/rrd/weather_temp.rrd N:$TempAmb:$TempOut
rrdtool update /public/rrd/aqua_waterchange.rrd N:$Todayflow:$Weekflow
sleep 90
done
#
Cheers
rob
Australia


Reply With Quote