LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linux scripting - wget loop (https://www.linuxquestions.org/questions/programming-9/linux-scripting-wget-loop-381003/)

northy_ie 11-08-2005 01:56 AM

Linux scripting - wget loop
 
Hi all :)
Basically, I want to have a script, that runs wget to download and backup some files off my webserver.
I've been trying to "add" a number and run it in a loop, but unfortunately, the leading zero's were gone after that.
This is the script, that should be run:

# !/bin/sh
# Backup files from webserver
#echo backing up files..
NUMBER="0001"
echo wget "h**p://shadow/edata/ed"$NUMBER".inf" (can't post the url due to forum restrictions)

The next file, it should try to get via wget is 0002, 0003, 0004, you get the point :) the file range is currently 0001 - 0250..
Would any of you have an idea of how to get that up and running?

Thanks in advance!

scuzzman 11-08-2005 03:02 AM

This is a quick and dirty (very dirty) way of doing it:
Code:

#!/bin/bash
NUMBER=1
while [ $NUMBER lt 251 ]; do #This will do our counting
  while [ `echo $NUMBER | wc -c` lt 5 ]; do #This adds 0's to the beginning of the number until it's 4 characters in length, I hope.
    let NUMBER=0$NUMBER
  done
  wget http://shadow/edata/ed{$NUMBER}.inf
  let NUMBER += 1 #increment
done

#EOF

I think this should work, but to be safe, add 'echo' before the wget statement to see if it gives the right results... I don't have a box on which I can test it right now.

northy_ie 11-08-2005 04:07 AM

Thank you very much for your fast reply!
When I tried to execute the script, I got an "lt: binary operator expected" error in line 3, so I changed the lt to -lt.
I then got an error in line 8. After a google search, I added the quotation marks, that works fine as well now :)
When running the script now, I get following output:
./backup: line 4: [: +lt: binary operator expected
wget h**p://shadow/edata/ed{241}.inf <- (it's counting up from 1 to 250 okay, seems like the {} are not needed).
But when I change the lt in line4 to -lt, nothing happens, the script just hangs...

Getting very close :)

this is the current script:
Code:

#!/bin/bash
NUMBER=1
while [ $NUMBER -lt 251 ]; do #This will do our counting
  while [ `echo $NUMBER | wc -c` lt 5 ]; do #This adds 0's to the beginning of the number until it's 4 characters in length, I hope.
    let NUMBER=0$NUMBER
  done
  echo wget h**p://shadow/edata/ed{$NUMBER}.inf
  let "NUMBER += 1" #increment
done

#EOF


scuzzman 11-08-2005 06:23 AM

Did you try adding 'echo' to test the output? If not, try that...
I'm not exactly sure what is wrong without seeing the error messages.

northy_ie 11-08-2005 06:58 AM

Yes, the script is exactly as in the last post.
When running it, it shows

./backup: line 4: [: lt: binary operator expected
wget h**p://shadow/edata/ed{241}.inf
./backup: line 4: [: lt: binary operator expected
wget h**p://shadow/edata/ed{242}.inf
./backup: line 4: [: lt: binary operator expected
wget h**p://shadow/edata/ed{243}.inf
./backup: line 4: [: lt: binary operator expected
wget h**p://shadow/edata/ed{244}.inf
./backup: line 4: [: lt: binary operator expected
wget h**p://shadow/edata/ed{245}.inf

etc. :)

destuxor 11-08-2005 10:18 AM

I did something identical to what you're asking for to download webcomics this summer, only using Perl.
Code:

#!/usr/bin/perl -w
$x = 1;
while ($x < 365) {
        system ("wget http://hackles.org/strips/cartoon$x.png");
        $x++;
}

To get those leading zeros, I'd use sprintf in a C or Perl script like this (this is a C program):
Code:

#include <stdio.h>
#include <stdlib.h>

int main() {
        char * my_string = calloc(13, sizeof(char));
        int x = 0;
        while (x < 100) {
                sprintf(my_string, "%5d\n", x);
                char * current_char = my_string;
                while (* current_char == ' ') {
                        * current_char = '0';
                        current_char++;
                }
                printf(my_string);
                free(my_string);
                my_string = calloc(13, sizeof(char));
                x++;
        }
        free(my_string);
        return EXIT_SUCCESS;
}

Now you could use this code to write a program to do what you're asking for like this:
Code:

#include <stdio.h>
#include <stdlib.h>

const char * url = "h**p://shadow/edata/ed";

int main() {
        char * my_string = calloc(5, sizeof(char));
        char * my_command = calloc(512+1, sizeof(char));
        int x = 1;
        while (x < 251) {
                sprintf(my_string, "%4d", x);
                char * current_char = my_string;
                while (* current_char == ' ') {
                        * current_char = '0';
                        current_char++;
                }
                sprintf(my_command, "wget %s%s.inf", url, my_string);
                printf("%s\n", my_command);
                // replace the above line with: system("my_command");
                free(my_command);
                my_command = calloc(512+1, sizeof(char));
                free(my_string);
                my_string = calloc(13, sizeof(char));
                x++;
        }
        free(my_string);
        free(my_command);
        return EXIT_SUCCESS;
}

And that should take care of it :)

chrism01 11-09-2005 12:34 AM

Incidentally, in the bash version, you've only converted the 1st instance of 'lt' => '-lt'. you need to do the same to the 2nd instance, which funnily enough is line 4 (!) as per the error msg... ;)

northy_ie 11-09-2005 08:17 AM

Hey again :)

I did not see the posts from yesterday, so I sat down with a friend of mine and analyzed the script, and we got it working :)
if anyone is interested, here it is:

Code:

#!/bin/bash
NUMBER=1
while [ $NUMBER -lt 251 ]; do #This will do our counting
COUNTER=$NUMBER
  while [ `echo $COUNTER | wc -c` -lt 5 ]; do #This adds 0's to the beginning of the number until it's 4 characters in length, I hope.
export COUNTER="0"$COUNTER
  done
wget http://shadow/edata/ed$NUMBER.inf
  let "NUMBER += 1" #increment
done

#EOF

Thank you all very much for your help :)


All times are GMT -5. The time now is 02:13 AM.