免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1514 | 回复: 0
打印 上一主题 下一主题

获取天气预报的shell脚本学习 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-26 21:29 |只看该作者 |倒序浏览
获取天气预报的shell脚本学习
脚本来自 http://www.linuxsir.org/bbs/thread29701-11.html
红字部分是我的注释
作者: chainone
                               
//前面的漏了一点东西,在VI中没有全部COPY出来,不好意思下面是完整的。
天气预报(限国内城市)
用法
WeatherReport 城市名
如城市名为空则默认为南京
刚学SHELL,请高手指教
=============================================================================
#weather reporter@chainone
place=$1
if test "$place" = ""
then
place="南京"
fi
rm -f index.shtml
wget -q
http://weather.sina.com.cn/text/index.shtml
#我的FreeBSD没有安装wget, 把wget -q 改成fetch
ifzhixia=$(grep "$place" -c index.shtml)
if test "$ifzhixia" = 2
then
cat index.shtml | grep "$place" -A 3 |sed '1d' | grep '>.\+' > .weathertemp2
elif test "$ifzhixia" = 1
then
cat index.shtml | grep "$place" -A 3 | grep '>.\+' > .weathertemp2
#cat index.shtml | grep "$place" -A 3 | grep '>.\+ .weathertemp2
else
echo "没有该城市的天气讯息!"
exit 0
fi
# grep "$place" -A 3  匹配$place 所在的行,包括后面的3行 -A 是After
# sed '1d'  删除第一行
# grep '>.\+
                     
匹配">???????
# tr -d '
天津
天津

  ≤3
12 ℃四川
成都

  ≤3
17 ℃
#-----------------------------
placetitle="城市: "
condition="天气状况: "
wind="风向风力: "
tempr="最低温度:        "
echo "$placetitle" > .weathertemp
echo "$condition" >> .weathertemp
echo "$wind" >> .weathertemp
echo "$tempr" >> .weathertemp
paste .weathertemp .weathertemp2
rm -f index.shtml
                       
               
       
       
我这样处理后正常了,我的默认 UTF-8 的   
#我的gnome终端字体设成UTF-8可以显示中文
# 但还是不行,
# 用locale 检查一下
www# locale
LANG=zh_CN.eucCN
LC_CTYPE="zh_CN.eucCN"
LC_COLLATE="zh_CN.eucCN"
LC_TIME="zh_CN.eucCN"
LC_NUMERIC="zh_CN.eucCN"
LC_MONETARY="zh_CN.eucCN"
LC_MESSAGES="zh_CN.eucCN"
LC_ALL=
#设置字体
www# setenv LC_ALL zh_CN.UTF-8
www# locale
LANG=zh_CN.eucCN
LC_CTYPE="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_ALL=zh_CN.UTF-8
#这样shell运行才完全正确
wget下面加一句:
iconv -f GB2312 -t UTF-8 index.shtml > index.shtml1
下面的  index.shtml 改为: index.shtml1
最后面 加一句:
rm -f index.shtml1
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/20041/showart_431057.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP