writing a script
I have a script to goes to yahoo weather with lynx and gets the current
conditions. These are saved in a file which is passed to torsmo and displayed on my desktop. The problem is the script is outdated and doesn't quite work. Here is the old one: I'm trying to modify this script to one that works. Code:
#!/bin/bash http://weather.yahoo.com/forecast/USIL1191.html I'm not necessarily asking for someone to write this script completely for me (although that would be nice). I just cant seem to figure out what it all does from doing man 'head' man 'tail' etc... I can sort of get an idea of what each part does but not how it works together. So if someone could explain what it does then that would help me modify it. |
This is why people should document scripts when they write them:
First line tells it to execute the remaining using the bash shell. (Otherwise it would use whatever shell the user is running so the syntax might not be valid. This is called the interpreter line and must always appear first with #! before the interpreter. Except for that line "#" (pound/number sign typically called "hash" in I.T. is a "comment" meaning it doesn't get acted upon on run of the script. Accordingly I'm inserting comments in your script: Code:
#!/bin/bash link=USIL1191.html As that's where you want to go now. For the rest you need to see if the assumptions previoulsy made were still valid e.g. does weather.yahoo.com/USIL1191.html contain a line (and only one) that contains the word "Currently". Does it contain the same information as the other html did? Hopefully the above gives you enough to figure out what you need to change. |
Thanks. Looks like it will take some work but that's a good start. I'm studying for finals now so who knows when I'll get around to it.
|
After taking a closer look I think it would be easier to start from scratch (yahoo's site has changed a lot since the script was written, and they ask you to use a goverment site for data). I have some questions though:
What's a good way to cut out arbitrary lines? I need to search for the n'th, occurrence of a string and then save some lines in the region. (the region might vary, so I need a general method) Also what is a good program to format the lines I've saved? I did a little research and it looks like awk/gawk is one and sed is another. Should I learn just one or both? |
Just a hint:
The XML output of the RSS feed is much cleaner, and easier to sort through than the HTML output of the webpage. Try grabbing the needed info from http://xml.weather.yahoo.com/forecastrss?p=USIL1191&u=c instead. Should be easier. |
As an alternative, if you're using gnome, you can right click on a taskbar, click add to panel and choose the Weather Rreport applet. You can then customise it to your location and how often it updates.
Of course, if you're writing the script as an exercise to learn a bit about bash then go with the script :) |
Thanks for the help so far everyone.
However I'm using Fluxbox, so I'm not really interested in how to do it with Gnome (flux is very minimal which makes it pretty fast and also forces me to go and learn how to do stuff via command line--which is definitely a mixed blessing, but I think in the long run worth it.) Back the the script which I'm still having trouble with. The RSS feed does look simplest of all the web pages so far so that is probably what I should go with. In case it wasn't clear in the first place I'm trying to grab the weather data (from the rss feed I suppose) and format it so that it looks nice. I will then have torsmo run this script periodically and display the information on my desktop. I'm thinking of something that looks like this: (pretend there's a degree symbol after those temperatures!) Today Currently: 20 Hi: 23 Low: 12 Tomorrow Hi: 10 Low: 0 Hopefully some specific questions will get my started: In the statement below I understand what grep does, cut grabs what's in the fifth field (where fields are separated by spaces) but my question is what does the -n do in cat? The man page says it numbers the lines but that isnt very informative. I tried some experiments in the shell using just cat -n but I guess I messed those up cuz they dont help me. Code:
begin=`cat -n $file | grep "Currently" | cut -d ' ' -f5` |
From the command: cat --help
-n, --number number all output lines That is used to find the line number which has "Current" then, cut everything else out as we just want that number. I was putzing with the script abit and this is what it looks like so far... Code:
#!/bin/bash |
All times are GMT -5. The time now is 12:20 AM. |