方法一:
#!/bin/sh
cd /userdata
mount -o remount,rw /
#echo "nameserver 114.114.114.114" > /etc/resolv.conf
#rm -rf beijing
wget -q http://time.tianqi.com/beijing
chmod 777 beijing
yearStr=`cat ./beijing | grep nyear | grep -v day`
yearS=${yearStr#*=}
year=${yearS:0:4}
#echo $year
monStr=`cat ./beijing | grep nmonth | grep -v day`
monS=${monStr#*=}
mon=${monS:0:2}
#echo $mon
dayStr=`cat ./beijing | grep nday | grep -v year`
dayS=${dayStr#*=}
day=${dayS:0:2}
#echo $day
hrsStr=`cat ./beijing | grep nhrs= | grep -v year`
hrsS=${hrsStr#*=}
hrs=${hrsS:0:2}
#echo $hrs
minStr=`cat ./beijing | grep nmin= | grep -v year`
minS=${minStr#*=}
min=${minS:0:2}
#echo $min
secStr=`cat ./beijing | grep nsec= | grep -v year`
secS=${secStr#*=}
sec=${secS:0:2}
#echo $sec
date -s $year.$mon.$day-$hrs:$min:$sec
hwclock -f /dev/rtc1 -w
rm -rf beijing
#rm -rf /etc/resolv.conf
方法二:
#!/bin/sh
cd /tmp
APP=time
rm -rf $APP
wget -q http://kf.zhuzhu.biz:7102/time
chmod 777 $APP
content=`cat ./$APP`
echo "content: $content"
if [ -z "$content" ]
then
echo "\$content is empty"
# 继续从中国天气获取
else
echo "\$content is not empty"
# 解析json
#/ code=`echo $content |grep -o 'code[":]+[^"]+'`
# echo $code
# currentTime=`echo $content |grep -Po 'currentTime[" :]+\K[^"]+'`
# echo $currentTime
#data2=${content#*time_iso8601\":\"}
#echo $date2
code=`echo $content | sed 's/,/\n/g' | grep "code" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "code: $code"
if [ "$code" != "0" ];then
echo "错误"
exit 1
fi
year=`echo $content | sed 's/,/\n/g' | grep "year" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "year: $year"
month=`echo $content | sed 's/,/\n/g' | grep "month" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "month: $month"
day=`echo $content | sed 's/,/\n/g' | grep "day" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "day: $day"
hour=`echo $content | sed 's/,/\n/g' | grep "hour" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "hour: $hour"
minute=`echo $content | sed 's/,/\n/g' | grep "minute" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'`
echo "minute: $minute"
second=`echo $content | sed 's/,/\n/g' | grep "second" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g' | sed 's/}//g'`
echo "second: $second"
date -s $year.$month.$day-$hour:$minute:$second
echo "date -s $year.$month.$day-$hour:$minute:$second"
hwclock -w
fi