保存wget下载的图像的多个实例

时间:2017-11-17 19:27:57

标签: linux shell

#!/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 

它的作用是顶部的网址是每分钟拍照的相机。我的代码下载该图像,保存并每隔60秒将其放入一个html文件中。 所以我要做的是将最新的图像保存在spy.html中,较旧的图像将循环遍历循环直到达到10。 index.html中最新的图片然后是index1.html中的第二个最新图片等等。 运行此代码不会在图片和html文件上生成此类文件。这种做法很有道理,但修复它是另一回事。 仍在寻求帮助。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

#!/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 

我不得不说,我没有检查它是否有效。但我相信其余的代码是正确的,所以它应该。