Skip to content
Advertisement

Save multiple instances of an image downloaded by wget

#!/bin/bash   
trap '.' INT
u = "URL"
if echo $u | egrep -q '^(http://|https://).+(.jpg|.jpeg|.JPG|.JPEG)$'
then echo "Url checks out"
while : 
do
 wget -q $u -O picture$i.jpg
 time=$(date)
 echo "<html><head><title>Spy</title></head><body><h1>spying</h1><p>Picture 
 uploaded: $time</p><img src="picture$i.jpg" width="300"></body></html>"
 > spy.html
    for (( i=1; i<=10; i++))
     do
      if [$i = 10]
     then 
      rm picture$i.jpg
     else 
      mv picture$i picture$(($i+1)).jpg
      mv spy.html spy$(($i+1)).html
     fi
    done
 sleep 60
 done
 else echo "Link not accepted"
 fi 

What it does is that the url on top is to a camera taking photos every minute. My code downloads that image, saves it and puts it in a html file every 60 seconds. So what I am trying to do is save the newest image in spy.html and the older ones would iteration through the loop until it reaches 10. So newst picture in index.html then the second newest in index1.html and so on.. Running this code yields no such file on picture and the html file. This kind of makes sense, but fixing it is a different matter. Still looking for help on this.

Advertisement

Answer

You can do it like that:

#!/bin/bash   
trap '.' INT
u="URL"
counter=0 # Or 1 if you want to start counting from 1
if echo ${u} | egrep -q '^(http://|https://).+(.jpg|.jpeg|.JPG|.JPEG)$'
 then echo "Url checks out"
 while : 
 do
  wget -q ${u} -O picture.jpg
  time=$(date)
  echo "<html><head><title>Spy</title></head><body><h1>spying</h1><p>Picture 
  uploaded: ${time}</p><img src="picture.jpg" width="300"></body></html>"
 > index_${counter}.html  # Just makes a file with the counter in it's name
  counter=counter+1 # Count's up
  sleep 60
 done
else 
 echo "Link not accepted"
fi 

I have to say, I didn’t check if it works. But I believe the rest of the code is correct, so it should.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement