View previous topic :: View next topic |
Author |
Message |
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Tue Feb 10, 2025 11:24 pm Post subject: Latest incarnation of ~/weather.bash |
|
|
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 ~~ Dave |
|
|
Back to top |
|
crouse Site Admin

Joined: 17 Apr 2025 Posts: 11833 Location: Iowa
|
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Wed Feb 11, 2025 2:20 pm Post subject: |
|
|
Thanks. |
|
Back to top |
|
inactive Sr. Member
Joined: 29 Aug 2025 Posts: 1207
|
Posted: Thu Feb 12, 2025 2:27 am Post subject: |
|
|
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 2025.1 PWP
Mandriva Cooker
ArtistX live
Last edited by inactive on Thu Feb 12, 2025 2:41 am; edited 1 time in total |
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 2:33 am Post subject: |
|
|
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
Last edited by masinick on Thu Feb 12, 2025 2:41 am; edited 1 time in total |
|
Back to top |
|
inactive Sr. Member
Joined: 29 Aug 2025 Posts: 1207
|
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 2:39 am Post subject: |
|
|
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 |
|
Back to top |
|
inactive Sr. Member
Joined: 29 Aug 2025 Posts: 1207
|
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 2:40 am Post subject: |
|
|
Excuse me, NOT chown, it is chmod +x filename!
e.g. chmod +x ~/weather.bash
~/weather.bash 03301 |
|
Back to top |
|
hakova New Member

Joined: 03 Feb 2025 Posts: 40 Location: Virginia
|
Posted: Thu Feb 12, 2025 5:10 am Post subject: |
|
|
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 |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 9:42 am Post subject: |
|
|
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! |
|
Back to top |
|
crouse Site Admin

Joined: 17 Apr 2025 Posts: 11833 Location: Iowa
|
Posted: Thu Feb 12, 2025 10:36 am Post subject: |
|
|
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 |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 10:55 am Post subject: |
|
|
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. |
|
Back to top |
|
masinick Linux Guru

Joined: 03 Apr 2025 Posts: 8615 Location: Concord, NH
|
Posted: Thu Feb 12, 2025 10:57 am Post subject: |
|
|
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. |
|
Back to top |
|
hakova New Member

Joined: 03 Feb 2025 Posts: 40 Location: Virginia
|
|
Back to top |
|
|