我正在使用脚本来确定rrdtool图的日/夜:
# Calculating Civil Twilight based on location from LAT LON
DUSKHR=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun rises/{:a;n;/Nautical twilight/b;p;ba}' | cut -c 45-46`
DUSKMIN=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun rises/{:a;n;/Nautical twilight/b;p;ba}' | cut -c 47-48`
DAWNHR=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun rises/{:a;n;/Nautical twilight/b;p;ba}' | cut -c 30-31`
DAWNMIN=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun rises/{:a;n;/Nautical twilight/b;p;ba}' | cut -c 32-33`
# Calculating sunset/sunrise based on location from LAT LON
SUNRISEHR=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun transits/{:a;n;/Civil twilight/b;p;ba}' | cut -c 30-31`
SUNRISEMIN=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun transits/{:a;n;/Civil twilight/b;p;ba}' | cut -c 32-33`
SUNSETHR=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun transits/{:a;n;/Civil twilight/b;p;ba}' | cut -c 45-46`
SUNSETMIN=`/usr/bin/sunwait sun up $LAT $LON -p | sed -n '/Sun transits/{:a;n;/Civil twilight/b;p;ba}' | cut -c 47-48`
# Converting to seconds
SUNR=$(($SUNRISEHR * 3600 + $SUNRISEMIN * 60))
SUNS=$(($SUNSETHR * 3600 + $SUNSETMIN * 60))
DUSK=$(($DUSKHR * 3600 + $DUSKMIN * 60))
DAWN=$(($DAWNHR * 3600 + $DAWNMIN * 60))
在rrdtool图中使用如下:
#!/bin/bash
. inc/colors.sh
. inc/sunlight.sh
# Inside last 24 hours (mobile)
rrdtool graph /ram/temperature/graphs/sensors-24h-inside-mobile.png \
--alt-autoscale \
--slope-mode \
--width 638 --height 337 \
--border 0 \
--font AXIS:13:0 \
--color BACK#363636 --color CANVAS#363636 --color GRID#999999 --color MGRID#B5B5B5 --color FONT#CCCCCC \
--x-grid MINUTE:60:HOUR:1:MINUTE:360:0:%R \
--y-grid 1:1 \
DEF:Obyvacka=ds_temperature.rrd:O:AVERAGE \
CDEF:nightplus=LTIME,86400,%,$SUNR,LT,INF,LTIME,86400,%,$SUNS,GT,INF,UNKN,Obyvacka,*,IF,IF \
CDEF:nightminus=LTIME,86400,%,$SUNR,LT,NEGINF,LTIME,86400,%,$SUNS,GT,NEGINF,UNKN,Obyvacka,*,IF,IF \
AREA:nightplus#303030 AREA:nightminus#303030 \
CDEF:dusktilldawn=LTIME,86400,%,$DAWN,LT,INF,LTIME,86400,%,$DUSK,GT,INF,UNKN,Obyvacka,*,IF,IF \
CDEF:dawntilldusk=LTIME,86400,%,$DAWN,LT,NEGINF,LTIME,86400,%,$DUSK,GT,NEGINF,UNKN,Obyvacka,*,IF,IF \
AREA:dusktilldawn#222222 AREA:dawntilldusk#222222 \
LINE2:Obyvacka$GREEN:
有时(我还没有确定模式)我会得到以下错误:
inc/sunlight.sh: line 15: 18 * 3600 + 09: value too great for base (error token is "09")
ERROR: invalid rpn expression in: LTIME,86400,%,19920,LT,INF,LTIME,86400,%,,GT,INF,UNKN,Obyvacka,*,IF,IF
但看起来,它只发生在23个小时左右。
我找到了很多解决方案,但我不能将单一解决方案应用到我的脚本中。
答案 0 :(得分:2)
带有前导零的bash中的数字与许多其他编程语言一样,被解释为八进制。因此value too great for base
,因为八进制数字仅从0到7。
解决方案是告诉您的脚本变量SUNRISEHR
,SUNRISEMIN
等是十进制而不是八进制。这可以通过在变量名前加10#
来完成,如下所示:
SUNR=$((10#$SUNRISEHR * 3600 + 10#$SUNRISEMIN * 60))
SUNS=$((10#$SUNSETHR * 3600 + 10#$SUNSETMIN * 60))
DUSK=$((10#$DUSKHR * 3600 + 10#$DUSKMIN * 60))
DAWN=$((10#$DAWNHR * 3600 + 10#$DAWNMIN * 60))
最小例子:
$ export A=09; echo $(($A+1))
bash: 09: value too great for base (error token is "09")
$ export A=09; echo $((10#$A+1))
10