PageRenderTime 19ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/xkcd.sh

https://bitbucket.org/aweidner/bashxkcd
Shell | 48 lines | 39 code | 6 blank | 3 comment | 7 complexity | e29aa6a3573eca397c74aa4062f6c216 MD5 | raw file
 1#!/bin/bash
 2
 3if [ $# -eq 1 ]; then
 4    #First check to see if we already have it
 5    yes=`ls | grep -o "${1}\.\(jpg\|png\)"`
 6    if [ `expr length $yes` -gt 0 ]; then
 7        echo 2
 8        exit
 9    fi
10
11    #This means the user wants a specific comic
12    wget -q "http://xkcd.com/${1}"
13    if [ -f $1 ]; then
14        image=`cat $1 | grep -o "http://imgs.xkcd.com/comics/[a-zA-Z1-9_()]\+\.\(jpg\|png\)" | head -1`
15        wget -q $image
16
17        file=${image:28}
18        len=`expr length $image`
19        t=${image:$len-4} 
20
21        if [ -f $file ]; then
22            mv $file "${1}${t}"
23        fi
24        rm -f $1
25        echo 0
26    else
27        echo 1
28    fi
29else
30    echo "Clearing directory, everything will be downloaded"
31    counter=1
32    e=`./xkcd.sh $counter`
33    echo "Downloaded comic number ${counter}"
34    while [ $e -ne 1 ]; do
35        echo $e
36        if [ $e -ne 2 ]; then
37            sleep 5 
38        fi
39
40        let counter=counter+1
41        if [ $counter -eq 404 ]; then
42            let counter=counter+1
43        fi
44
45        let e=`./xkcd.sh $counter`
46        echo "Downloaded comic number ${counter}"
47    done
48fi