LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with this script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-this-script-836811/)

p0rn0saurus 10-07-2010 03:11 PM

Help with this script
 
Im trying to grab the jpg and rename it accord to ls. what do i need to do to get this working? ls will list a bunch of .htm files

Quote:

#!/bin/bash

list=`ls`

for i in `cat $list | grep productpic |sed -e 's/ //g'| sed -e 's/.*productpic//' | sed -e 's/jpg.*//' | sed 's/[.]/.jpg/g'`
do

wget "http://www.digi163.com/battery/productpic$i" > $list

done

crts 10-07-2010 03:38 PM

Hi,

have a look at the following threads first:
http://www.linuxquestions.org/questi...-files-831347/
http://www.linuxquestions.org/questi...lename-827305/
http://www.linuxquestions.org/questi...olders-804100/
http://www.linuxquestions.org/questi...751/page2.html

Renaming files is a common task and it has been covered several times now. There is probably something in the links above that will solve your problem. Post again if you have trouble understanding a command.

[EDIT]
Just saw that you are using wget. Are you trying to keep a local directory in sync with the contents of a webpage? Please provide some sample filenames and how you want them to rename.

grail 10-07-2010 04:02 PM

Well I am a little concerned with the whole script!!
Code:

list=`ls`
So where ever I run this, all files and directories will be thrown into 'list'. Is this what you really want? Seems you are only looking for jpg files??
Code:

for i in `cat $list | grep productpic |sed -e 's/ //g'| sed -e 's/.*productpic//' | sed -e 's/jpg.*//' | sed 's/[.]/.jpg/g'`
This has to be the most convoluted way I have seen to try and avoid word splitting I have seen for a while.
Code:

wget "http://www.digi163.com/battery/productpic$i" > $list
This one is possibly my favourite. See problem one above and then ask yourself what the possible value is that might be stored in '$list'??
Not only that, but you went to all the trouble of avoiding word splitting for your for loop but then didn't quote this variable so here it is again??

Let us go back to some basics:

1. What does the directory structure look like where you are running ls?

2. Provide at least one complete url (can be fake) so we understand what you are wgetting?

3. What do you wish to be the contents of the file and of what file?

Kenhelm 10-07-2010 05:55 PM

I'm assuming there is directory containing files such as
HP-HSTNN-LB31-battery.htm
from downloading
http://www.digi163.com/battery/HP-la...31-battery.htm
and you want to extract the jpg relative URL from the htm file, download the jpg file, and save it as
HP-HSTNN-LB31-battery.jpg.
Code:

#!/bin/bash
for filename in *battery.htm;do
 pp=$(grep -o '/productpic/[^"]*\.jpg' "$filename")
 wget -O "${filename%.htm}.jpg" "http://www.digi163.com/battery$pp"
done


ghostdog74 10-07-2010 09:00 PM

@OP, how is your htm files being downloaded in the first place? With wget, you can also download those image files.


All times are GMT -5. The time now is 03:08 PM.