Register
It is currently Thu Feb 22, 2018 12:26 am

Latest incarnation of ~/weather.bash


All times are UTC


Post new topic Reply to topic  [ 44 posts ]  Go to page 1, 2, 3  Next
Author Message
 PostPosted: Tue Feb 10, 2009 11:24 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Dave Crouse originally wrote a few variations of a short weather script and later turned it into a Bash function. Jeo took Dave's stuff and really expanded on it to get a larger, nicer report. I've taken Jeo's work, removed the hooks to the bot procedures or whatever was calling this stuff, changed output to simple echo statements, and otherwise left it alone, and it worked. Here is the resulting code:

#####################################################################################################
Quote:
#!/bin/bash
# weather.bash
#desc Find current weather stats for your zip code
#desc Ex: ${trig}weather 03301
# weather 1.1 -Crouse
# With Updates by Jeo
# Modified to run stand alone by Brian Masinick
# Example: !weather 03301
# Usage: weather + zipcode

zipcode=$1
if [ -z "$zipcode" ]; then
echo "Please provide a zip code (Ex: weather 03301)"
else
unset response
# Add a backslash (\) after -dump-width 300 if this line splits
# across two lines; Should be one distinct line:
WEATHER="$(elinks -dump -dump-width 300 "http://mobile.wunderground.com/cgi-bin/findweather/getForecast?query=${zipcode}" | grep -A16 Updated)"

if [ -z "$WEATHER" ]; then
response="No Results for $zipcode"
echo "${response}"
else
response[1]="$(echo "$WEATHER" | grep -Eo 'Observed.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print "Weather: " $1}')"
response[2]="$(echo "$WEATHER" | grep -Eo 'Updated.*' |sed s/\ *\|\ */\|/g |awk -F\| '{print $1}')"
response[3]="$(echo "$WEATHER" | grep -Eo 'Temperature.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}' | sed s/DEG/\ /g )"
response[4]="$(echo "$WEATHER" | grep -Eo 'Windchill.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
response[5]="$(echo "$WEATHER" | grep -Eo 'Wind .*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
response[6]="$(echo "$WEATHER" | grep -Eo 'Conditions.*' | sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
response[7]="$(echo "$WEATHER" | grep -Eo 'Humidity.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"
response[8]="$(echo "$WEATHER" | grep -Eo 'Dew.Point.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}'| sed s/DEG/\ /g)"
response[9]="$(echo "$WEATHER" | grep -Eo 'Pressure.*' |sed s/\ *\|\ */\|/g | awk -F\| '{print $1 ": " $2}')"

for index in `seq 1 9`; do
if [ -n "${response[$index]}" ]; then
echo "${response[$index]}"
fi
let "index = $index + 1"
done
fi
fi


Do note that the WEATHER= line should be on one single line - if it splits, combine it back together or add a continuation character, \
between the lines so that it works correctly.
[mod]I edited the post to make it show correctly... kinda blows out the page unless you have a widescreen monitor, but it copys/pastes much better ;) ~~ Dave [/mod]

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Wed Feb 11, 2009 9:45 am   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
Very nice.

[[email protected] Scripts]$ ./weather 50317
Weather: Observed at Witmer Park, Des Moines, Iowa
Updated: 3:42 AM CST on February 11, 2009
Temperature: 36.8 F / 2.7 C
Windchill: 34 F / 1 C
Wind: North at 4.0 mph / 6.4 km/h
Conditions: Clear
Humidity: 82%
Dew Point: 32 F / -0 C
Pressure: 29.60 in / 1002.3 hPa (Steady)
[[email protected] Scripts]$


One suggestion........

put in some checks for elinks.... ;)
Code:
if [[ -z $( type -p elinks ) ]]; then echo -e "elinks -- NOT INSTALLED !";exit ;fi

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Wed Feb 11, 2009 2:20 pm   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Thanks.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 2:27 am   
User avatar
Why am I getting this with it?

Thanks.

Code:
[[email protected] ~]$ chmod a+x weather.sh
[[email protected] ~]$ sh weather.sh
Please provide a zip code (Ex: weather 03301)
[[email protected] ~]$ xxxxx
bash: 88130: command not found
[[email protected] ~]$ sh weather.sh
Please provide a zip code (Ex: weather 03301)
[[email protected] ~]$ weather xxxxx
bash: weather: command not found
[[email protected] ~]$ weather xxxxx
bash: weather: command not found
[[email protected] ~]$ ./weather xxxxx
bash: ./weather: No such file or directory

I copied and pasted the script directly from the above?


Last edited by inactive on Thu Feb 12, 2009 2:41 am, edited 1 time in total.

Top
  
 PostPosted: Thu Feb 12, 2009 2:33 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
You have to provide it with a zip code or location, then either add it to your path or specify the path.

Eg.

chmod +x weather (or whatever you called it)
./weather 88130

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Last edited by masinick on Thu Feb 12, 2009 2:41 am, edited 1 time in total.

Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 2:37 am   
User avatar
I did - see the first line of code that I posted, and the last two lines.

I chmod'ed it first line, and then last two lines is ./weather 88130. When I try chown +x, I get the following:
Code:
[[email protected] ~]$ cd /home/dedanna
[[email protected] ~]$ chown +x weather.sh
chown: invalid user: `+x'
[[email protected] ~]$ chown +x dedanna: weather.sh
chown: invalid user: `+x'
[[email protected] ~]$ chown dedanna: +x weather.sh
chown: cannot access `+x': No such file or directory


Edit: Ah, never mind! I got it! :D
Code:
[[email protected] ~]$ ./weather xxxxx
bash: ./weather: No such file or directory
[[email protected] ~]$ sh weather.sh xxxxx
Weather: Observed at My location
Updated: 7:38 PM MST on February 11, 2009
Temperature: 46.2 F / 7.9 C
Windchill: 46 F / 8 C
Wind: WSW at 1.3 mph / 2.1 km/h
Conditions: Partly Cloudy
Humidity: 29%
Dew Point: 16 F / -9 C
Pressure: 29.99 in / 1015.5 hPa (Rising)


Thanks!


Last edited by inactive on Thu Feb 12, 2009 2:40 am, edited 2 times in total.

Top
  
 PostPosted: Thu Feb 12, 2009 2:39 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Also, you have to name the command the same as whatever you name the file - unless you create an alias, then use that name instead.

For instance, mine is:

~/weather.bash 03301

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 2:40 am   
User avatar
Thanks a bunch! :)


Top
  
 PostPosted: Thu Feb 12, 2009 2:40 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Excuse me, NOT chown, it is chmod +x filename!


e.g. chmod +x ~/weather.bash
~/weather.bash 03301

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 5:10 am   
New Member
User avatar

Joined: Tue Feb 03, 2009 3:11 am
Posts: 49
Location: Iowa
Here is my million dollar question:

What if I have a non-English locale which leads e-links to produce output accordingly? Then the script functions do not work appropriately to filter the output to the console, obviously. Is there a way to temporarily change e-links behavior here, so that it produces output in English? I tried
Code:
LC_ALL=en_US |weather.bash 11111
, but it didn't change the language of the e-links output.

Hakan


Top
 Profile  
 PostPosted: Thu Feb 12, 2009 9:42 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
I have absolutely no idea to what extent elinks has included localization support, and if it has it, how well it is supported by various distributions. It could work on one system and not on another, depending how well various locales are implemented on that particular system. As far as calling the script, do not put a pipe symbol in the command line. Instead, use either a relative or absolute path. Also, in the past, I have used LANG rather than LC_ALL. It has been quite a while since I've done localization work; I don't really remember all that gets inherited with those two environment variables, but first try removing the pipe and adding a relative path, such as ./ or ~/, then try replacing LC_ALL with LANG and see if either change makes any difference at all.

Let me know the outcome, and also which distro you are using. Glad to have you in the USALUG --- good questions!

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 10:36 am   
Site Admin
User avatar

Joined: Thu Apr 17, 2003 3:44 am
Posts: 12300
Location: Iowa
I think you can do something like:
Code:
export LOCALE=UTF-8


at the beginning of the bash script... not positive, as I don't use anything other than what is normally set when the locale is set during setup. Should be a good place to start anyway.

_________________

Zbook (Laptop)- Mint 18 64-bit - I7-4800mq - 32GB ram
Phantom (Desktop)- Mint 64-bit - i7-5770 - 32GB ram
Nas4Free (NAS) - 6X3TB File Server.
Sky (HP BladeCenter) - 16 blades - 536 GB ram-G7/G6/G5 blades.


Top
 Profile WWW  
 PostPosted: Thu Feb 12, 2009 10:55 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
See http://www.linux.com/feature/53781 for a good discussion of locale environment variables. In the context of this script, I would say that either LANG or LC_ALL would work, but only if localization is properly implemented for the elinks browser on the distribution in question.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 10:57 am   
Linux Guru
User avatar

Joined: Sat Apr 03, 2004 12:39 am
Posts: 12346
Location: Clinton Township, Michigan
Also, it should not be necessary to modify the script; change the locale environment variable on the command line, e.g.
LANG=C ~/weather.bash 03301 and if elinks is properly localized, that should work.

_________________
Brian Masinick
Distros: MX-16, antiX, Debian


Top
 Profile WWW YIM  
 PostPosted: Thu Feb 12, 2009 1:59 pm   
New Member
User avatar

Joined: Tue Feb 03, 2009 3:11 am
Posts: 49
Location: Iowa
I think I couldn't make myself very clear in the first message. It is not the e-link's menu interface being in my locale, it is the whole web page loading in my locale language.

Well, setting any of the environment parameters did not work. The only way I could get it work was to change the language in .elinks.conf. It was defaulted to "system", which obviously uses my locale settings. I changed it to English, so that now the web pages load in English, even though there is a translated version available.

If I have to get an output in my locale language, then I guess I should replace the words in search phrases of the original scripts with the ones in my locale language, right? In that way, may be I can contribute to the internationalization of your script ;)?

:D


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 44 posts ]  Go to page 1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP