我一直在制作一个bash脚本来改变我的背景,这取决于一天中的时间,但我无法弄清楚为什么即使时间是中午,背景总是变为黎明。
#!/bin/bash
date=$(date "+%H")
DawnNum=$(seq -s " -o " 5 9)
DayNum=$(seq -s " -o " 10 17)
DuskNum=$(seq -s " -o " 18 22)
NightNum=$(seq -s " -o " 23 24)
NightNumCont=$(seq -s " -o " 0 4)
if
[ -e ~/.cache/weather.txt ] ;
then
rm ~/.cache/weather.txt ;
else
echo "Cache is clean." ;
fi
curl -o ~/.cache/weather.txt -s "https://weather.yahooapis.com/forecastrss?w=2470169&u=f" ;
cat ~/.cache/weather.txt | grep Rain -n | grep 29 || cat ~/.cache/weather.txt | grep Snow -n | grep 29 ;
current=$?
if
[ "$current" -eq 0 ] && [ "$date" -eq $DawnNum -o $DayNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountains-stormday.png" ;
elif
[ "$current" -eq 0 ] && [ "$date" -eq $DuskNum -o $NightNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountains-stormnight.png" ;
elif
[ "$current" -ne 0 ] && [ "$date" -eq $DawnNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountainsdawn.png" ;
elif
[ "$current" -ne 0 ] && [ "$date" -eq $DayNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountainsday.png" ;
elif
[ "$current" -ne 0 ] && [ "$date" -eq $DuskNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountainsdusk.png" ;
elif
[ "$current" -ne 0 ] && [ "$date" -eq $NightNum ] ; then
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/mountainsnight.png" ;
else
gsettings set org.cinnamon.desktop.background picture-uri "file:///home/user/Pictures/Google Now/sanfranciscodawn.png" ;
fi
exit
答案 0 :(得分:2)
我建议简化逻辑并减少代码中的重复。当您使用bash并使用整数时,可以使用(( ))
数值测试来确定您所处的时间范围。此类测试不要求您使用$
变量名称,并更清楚地表达您的意思:
#!/bin/bash
date=$(date "+%H")
dawn_start=5
day_start=10
dusk_start=18
night_start=23
# ...
current=$?
if [[ "$current" == 0 ]]; then
if (( date >= dawn_start )) && (( date < dusk_start )); then
uri="mountains-stormday.png"
else
uri="mountains-stormnight.png"
fi
else
if (( date >= dawn_start )) && (( date < day_start )); then
uri="mountainsdawn.png"
elif (( date >= day_start )) && (( date < dusk_start )); then
uri="mountainsday.png"
elif (( date >= dusk_start )) && (( date < night_start )); then
uri="mountainsdusk.png"
elif (( date >= night_start )) || (( date < dawn_start )); then
uri="mountainsnight.png"
fi
fi
# catch-all, if $uri hasn't been set
[[ -z "$uri" ]] && uri="sanfranciscodawn.png"
dir="file:///home/user/Pictures/Google Now"
gsettings set org.cinnamon.desktop.background picture-uri "$dir/$uri"
答案 1 :(得分:1)
这不符合您的期望:
[ "$date" -eq $DawnNum -o $DayNum ]
问题是-eq
比-o
绑定得更紧。
更简单的测试可能会达到您的目标:
[ "$date" -ge 5 ] && [ "$date" -le 17 ]