I like to blog about my Bash scripts. Mostly it's so that I can refer back to them later when I can't remember how I did something. Also I always have this hope that someone is going to read them and tell me how I could have written it better. But that has never happened.
It seems that everything I do in Bash starts out as one-line script and ends up as a more unwieldy thing that leaves me wishing I had picked another language.
I wanted to be able to quickly scan through all of the Pictures For Sad Children comics. The files are all named with sequential numbers, so I figured I'd just download them all.
a="http://www.picturesforsadchildren.com/comics"
for i in {1..358}
do
wget `printf "$a/%08d.gif" $i`
done
Problem is, they're not all gif files. The script quickly becomes more complicated.
There are three possible file extensions to try to download for each file. The data set has a lot of contiguous subsets of comic strips of the same type, so this script benefits from the heuristic optimization of moving a type to the front of the list when it downloads successfully.
I'm sure this would be a lot more concise if I knew more about how to do array manipulation. But Bash is really not my language.
a="http://www.picturesforsadchildren.com/comics"
types=( gif png jpg )
for i in {1..358}
do
b=`printf "%08d" $i`
if [ "$(ls $b.* 2> /dev/null | wc -l)" -ne "0" ]
then
echo "$b skipped"
continue
fi
tested=()
untested=()
found=""
for c in "${types[@]}"
do
if [ ! $found ]
then
echo -n "$b.$c ..."
wget -q "$a/$b.$c"
if [ -f "$b.$c" ]
then
found="$c"
echo " found"
else
tested=( "${tested[@]}" "$c" )
echo " not found"
fi
else
untested=( "${untested[@]}" "$c" )
fi
done
types=( "$found" "${tested[@]}" "${untested[@]}" )
done
For my next trick, I dumped all of these images onto a web page. This one was a one-liner:
f=`date +%s`; p=`pwd`; for a in `ls` ; do echo "<img src=\"$p/$a\"/>"; done > /tmp/$f.html ; chromium-browser /tmp/$f.html