USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Latest incarnation of ~/weather.bash
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Tue Feb 10, 2024 11:24 pm    Post subject: Latest incarnation of ~/weather.bash Reply with quote

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.
Moderated:
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 Wink ~~ Dave



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Wed Feb 11, 2024 9:45 am    Post subject: Reply with quote

Very nice.

[crouse@VistaCrusher2 Scripts]$ ./weather 50317
Weather: Observed at Witmer Park, Des Moines, Iowa
Updated: 3:42 AM CST on February 11, 2024
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)
[crouse@VistaCrusher2 Scripts]$


One suggestion........

put in some checks for elinks.... Wink
Code:

if [[ -z $( type -p elinks ) ]]; then echo -e "elinks -- NOT INSTALLED !";exit ;fi



_________________
Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
Back to top
View user's profile Send private message Visit poster's website AIM Address
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Wed Feb 11, 2024 2:20 pm    Post subject: Reply with quote

Thanks.



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
inactive
Sr. Member


Joined: 29 Aug 2024
Posts: 1207

PostPosted: Thu Feb 12, 2024 2:27 am    Post subject: Reply with quote

Why am I getting this with it?

Thanks.

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

I copied and pasted the script directly from the above?



_________________
Mandriva 2024.1 PWP
Mandriva Cooker
ArtistX live


Last edited by inactive on Thu Feb 12, 2024 2:41 am; edited 1 time in total
Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 2:33 am    Post subject: Reply with quote

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: SimplyMEPIS
sidux - no CAPS!, antiX, Debian


Last edited by masinick on Thu Feb 12, 2024 2:41 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
inactive
Sr. Member


Joined: 29 Aug 2024
Posts: 1207

PostPosted: Thu Feb 12, 2024 2:37 am    Post subject: Reply with quote

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:
[dedanna@dedanna ~]$ cd /home/dedanna
[dedanna@dedanna ~]$ chown +x weather.sh
chown: invalid user: `+x'
[dedanna@dedanna ~]$ chown +x dedanna: weather.sh
chown: invalid user: `+x'
[dedanna@dedanna ~]$ chown dedanna: +x weather.sh
chown: cannot access `+x': No such file or directory


Edit: Ah, never mind! I got it! Very Happy
Code:
[dedanna@dedanna ~]$ ./weather xxxxx
bash: ./weather: No such file or directory
[dedanna@dedanna ~]$ sh weather.sh xxxxx
Weather: Observed at My location
Updated: 7:38 PM MST on February 11, 2024
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!



_________________
Mandriva 2024.1 PWP
Mandriva Cooker
ArtistX live


Last edited by inactive on Thu Feb 12, 2024 2:40 am; edited 2 times in total
Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 2:39 am    Post subject: Reply with quote

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: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
inactive
Sr. Member


Joined: 29 Aug 2024
Posts: 1207

PostPosted: Thu Feb 12, 2024 2:40 am    Post subject: Reply with quote

Thanks a bunch! Smile



_________________
Mandriva 2024.1 PWP
Mandriva Cooker
ArtistX live
Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 2:40 am    Post subject: Reply with quote

Excuse me, NOT chown, it is chmod +x filename!


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



_________________
Brian Masinick
Distros: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
hakova
New Member


Joined: 03 Feb 2024
Posts: 40
Location: Virginia

PostPosted: Thu Feb 12, 2024 5:10 am    Post subject: Reply with quote

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


Back to top
View user's profile Send private message
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 9:42 am    Post subject: Reply with quote

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: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
crouse
Site Admin


Joined: 17 Apr 2024
Posts: 11833
Location: Iowa

PostPosted: Thu Feb 12, 2024 10:36 am    Post subject: Reply with quote

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.



_________________
Veronica - Arch Linux 64-bit -- Kernel 2.6.33.4-1
Archie/Jughead - Arch Linux 32-bit -- Kernel 2.6.33.4-1
Betty/Reggie - Arch Linux (VBox) 32-bit -- Kernel 2.6.33.4-1
BumbleBee - OpenSolaris-SunOS 5.11
Back to top
View user's profile Send private message Visit poster's website AIM Address
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 10:55 am    Post subject: Reply with quote

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: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
masinick
Linux Guru


Joined: 03 Apr 2024
Posts: 8615
Location: Concord, NH

PostPosted: Thu Feb 12, 2024 10:57 am    Post subject: Reply with quote

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: SimplyMEPIS
sidux - no CAPS!, antiX, Debian
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
hakova
New Member


Joined: 03 Feb 2024
Posts: 40
Location: Virginia

PostPosted: Thu Feb 12, 2024 1:59 pm    Post subject: Reply with quote

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 Wink?

Very Happy


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
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 vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse