LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Is there a neater way to do this? (bash script) (https://www.linuxquestions.org/questions/programming-9/is-there-a-neater-way-to-do-this-bash-script-430751/)

Freemor 04-01-2006 04:32 PM

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

Scruff 04-01-2006 05:07 PM

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

Scruff 04-01-2006 05:37 PM

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.

Scruff 04-01-2006 07:12 PM

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

Freemor 04-01-2006 08:17 PM

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

Scruff 04-01-2006 08:23 PM

Excellent! Glad to help :)

Gins 04-02-2006 02:02 PM

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.

Scruff 04-02-2006 02:13 PM

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?

Gins 04-02-2006 02:33 PM

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.


All times are GMT -5. The time now is 12:20 PM.