king vash
July 23rd, 2008, 08:57 AM
so I make a sh script to download all the xkcd webcomics, but it only downloaded by file name so I added a counter in front of the file name.
I really want the format to be 1_name_morename.jpg but to get the number and file name I use
mv $name $i.$name if i use
mv $name $i_$name it thinks it is one variable name. Help, suggestions, comments anything.
ORIGINAL CODE
#!/bin/sh
cd /home/king3/Pictures/webcomics/XKCD/
i=1;
while [ $i -lt 450 ]
do
wget http://xkcd.com/$i/
url=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
name=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2 | cut -d\/ -f5`
alttext=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f6`
wget -N $url
mv $name "$i"_"$name"+"$alttext"
rm index.html
i=`expr $i + 1`
done
Explanation of the grep | head | cut
[CODE]# the grep return the entire <img src=" line
# <img src="http://imgs.xkcd.com/comics/tree_cropped_(1).jpg" title="'Petit' being a reference to Le Petit Prince, which I only thought about halfway through the sketch" alt="Petit Trees (sketch)" /><br/> <h3>Image URL (for hotlinking/embedding): http://imgs.xkcd.com/comics/tree_cropped_(1).jpg</h3>
#by adding the "| head -1" we get one line of html
#<img src="http://imgs.xkcd.com/comics/tree_cropped_(1).jpg" title="'Petit' being a reference to Le Petit Prince, which I only thought about halfway through the sketch" alt="Petit Trees (sketch)" /><br/>
#by adding the "cut -d\" -f2" we find the second instance of text seperated by two quote marks
#http://imgs.xkcd.com/comics/tree_cropped_(1).jpg
#the second cut filters to the image name
#tree_cropped_(1).jpg[
#to get the alt text "cut cut -d\" -f6" will probable work
/CODE]
I really want the format to be 1_name_morename.jpg but to get the number and file name I use
mv $name $i.$name if i use
mv $name $i_$name it thinks it is one variable name. Help, suggestions, comments anything.
ORIGINAL CODE
#!/bin/sh
cd /home/king3/Pictures/webcomics/XKCD/
i=1;
while [ $i -lt 450 ]
do
wget http://xkcd.com/$i/
url=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2`
name=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f2 | cut -d\/ -f5`
alttext=`grep http://imgs.xkcd.com/comics/ index.html | head -1 | cut -d\" -f6`
wget -N $url
mv $name "$i"_"$name"+"$alttext"
rm index.html
i=`expr $i + 1`
done
Explanation of the grep | head | cut
[CODE]# the grep return the entire <img src=" line
# <img src="http://imgs.xkcd.com/comics/tree_cropped_(1).jpg" title="'Petit' being a reference to Le Petit Prince, which I only thought about halfway through the sketch" alt="Petit Trees (sketch)" /><br/> <h3>Image URL (for hotlinking/embedding): http://imgs.xkcd.com/comics/tree_cropped_(1).jpg</h3>
#by adding the "| head -1" we get one line of html
#<img src="http://imgs.xkcd.com/comics/tree_cropped_(1).jpg" title="'Petit' being a reference to Le Petit Prince, which I only thought about halfway through the sketch" alt="Petit Trees (sketch)" /><br/>
#by adding the "cut -d\" -f2" we find the second instance of text seperated by two quote marks
#http://imgs.xkcd.com/comics/tree_cropped_(1).jpg
#the second cut filters to the image name
#tree_cropped_(1).jpg[
#to get the alt text "cut cut -d\" -f6" will probable work
/CODE]