LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2006, 04:32 PM   #1
Freemor
Member
 
Registered: Aug 2005
Location: New Brunswick
Distribution: Trisquel
Posts: 70
Blog Entries: 8

Rep: Reputation: 15
Is there a neater way to do this? (bash script)


Hi everyone,

I recently wrote this script to gab a weather radar image for the gnome panel weather applet. (I have to use the script because the images name is constantly changing to reflect date and time). I was really hoping to do the entire process as one long pipe but was unable to figure out a way to get the properly formated URL to the last wget command that way.. So figured I'd ask people more experienced then I.

Script Follows:

#!/bin/bash
#
# get the weather pic

#save the first part of the URL
echo -n http://weatheroffice.ec.gc.ca/data/radar/temp_image/ > foo

# get the webpage and grep out the name of the .gif.. appent to URL
wget -O - http://weatheroffice.ec.gc.ca/radar/index_e.html?id=wkr | grep -m 1 -o -ie wkr_cappi.*\.gif >> foo

#grab the .gif and save it with a standard name
wget -i foo -O weather.gif

# clean up
rm foo

I would love input on how to make this one long pipe or use Variables to avoid the need for Foo (actually that one I could probably work out on my own.. but I'm still open to hearing input on it as I'm sure there are much more finessed ways then what I'm likely to come up with at this point.).

Thanks in advance
Freemor
 
Old 04-01-2006, 05:07 PM   #2
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
I don't do much shell scripting, but you could do it in Perl easy enough (Perl rules ).

Code:
#!/usr/bin/perl
use strict;
use LWP::Simple;

# dir to save image - **change to suit your needs**
my $save_dir = '/home'; 

my $base_url = 'http://weatheroffice.ec.gc.ca';
my $page_url = $base_url . '/radar/index_e.html?id=wkr';

# get HTML and store it in $content
my $content = get($page_url);

# pull image URL from $content and retrieve image
# regex could be tighter here but it works
if ($content =~ m{(/data/radar/temp_image/WKR_CAPPI_.*GIF)}) {
    my $image = $base_url . $1;
    getstore($image, "$save_dir/weather.gif");
}
If Perl complains it can't find LWP::Simple, use this command (as root) to install it:

# perl -MCPAN -e 'install LWP::Simple'

-Scruff

Last edited by Scruff; 04-01-2006 at 07:32 PM.
 
Old 04-01-2006, 05:37 PM   #3
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
Also, lots of people use lynx (test-based browser) for stuff like this from shell scripts. Gimme a sec and I'll give it a try.
 
Old 04-01-2006, 07:12 PM   #4
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
Here you go:

Code:
#!/bin/bash

URL=http://weatheroffice.ec.gc.ca/radar/index_e.html?id=wkr
IMAGE=`lynx -dump $URL | grep -m 1 -o -ie wkr_cappi.*\.gif`

# print image name
echo $IMAGE
That should get you started anyway

-Scruff

Last edited by Scruff; 04-01-2006 at 07:34 PM.
 
Old 04-01-2006, 08:17 PM   #5
Freemor
Member
 
Registered: Aug 2005
Location: New Brunswick
Distribution: Trisquel
Posts: 70

Original Poster
Blog Entries: 8

Rep: Reputation: 15
Scruff, thanks for the feedback. With a little testing I've gotten it down to one line, no variables, and no temp files. Great!! The end result looks like this:

#!/bin/bash
#
# get the weather pic
wget http://weatheroffice.ec.gc.ca/data/radar/temp_image/`lynx -dump http://weatheroffice.ec.gc.ca/radar/index_e.html?id=wkr | grep -m 1 -o -ie wkr_cappi.*\.gif` -O weather.gif

That basically achievies the one pipe type result I was looking for.. knew I kept Lynx around for a reason (several actually). Your feedback was the nudge in the right direction that I needed
 
Old 04-01-2006, 08:23 PM   #6
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
Excellent! Glad to help
 
Old 04-02-2006, 02:02 PM   #7
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Scruff

The following script is scripting language, I hope.
-------------------------------------------------------

#!/bin/bash

URL=http://weatheroffice.ec.gc.ca/radar/index_e.html?id=wkr
IMAGE=`lynx -dump $URL | grep -m 1 -o -ie wkr_cappi.*\.gif`

# print image name
echo $IMAGE
-------------------------------------------------------

Please tell me if I am wrong.
 
Old 04-02-2006, 02:13 PM   #8
Scruff
Member
 
Registered: Oct 2002
Location: Stoughton, MA
Distribution: Gentoo x86_64 & PPC
Posts: 949

Rep: Reputation: 30
Quote:
Originally Posted by Gins
Scruff

The following script is scripting language, I hope.
-------------------------------------------------------

#!/bin/bash

URL=http://weatheroffice.ec.gc.ca/radar/index_e.html?id=wkr
IMAGE=`lynx -dump $URL | grep -m 1 -o -ie wkr_cappi.*\.gif`

# print image name
echo $IMAGE
-------------------------------------------------------

Please tell me if I am wrong.
I'm not sure I understand the question... That is a small shell script, yes. My first reply is a Perl script. Am I missing something?
 
Old 04-02-2006, 02:33 PM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Scruff

Thanks for the reply.

I don't know much about Perl programming.

If this is a shell script, I want to run on my computer. Of course I will find some other URL than what is in the program.

I know some scripting language. If you are telling me this is a shell script, I will try it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script ... alaios Linux - General 7 07-23-2005 02:55 AM
bash script dpaddy Linux - Software 6 09-02-2004 09:57 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration