我运行一个cron作业,请求从本地地址的远程摄像头获取快照:
wget http://user:[email protected]/snapshot.cgi
这会在每次运行时创build文件snapshot.cgi
, snapshot.cgi.1
, snapshot.cgi.2
。
我希望得到的结果是文件被命名为类似于file.1.jpg
, file.2.jpg
。 基本上,顺序或date/时间命名文件与正确的文件扩展名而不是.cgi
。
有任何想法吗?
你可能可以折磨wget做到这一点,但为什么要麻烦? 尝试
wget http://user:[email protected]/snapshot.cgi mv snapshot.cgi snapshot-`date +%Y-%m-%d-%H%M%S`.jpeg
这应该创builddate和时间标记的图像,如snapshot-2011-04-12-081649.jpeg
。 会这样吗?
编辑 :好的,不需要太多的折磨:
wget -O snapshot-`date +%Y-%m-%d-%H%M%S`.jpeg http://user:[email protected]/snapshot.cgi
但是大多数人仍然喜欢使用小型分立工具的UNIX方法。
你可以用一个简单的bash循环和wget中的-O
选项来做到这一点。
像这样的东西:
i=0 while 1 do # increment our counter ((i++)) # get the file and save it wget -O file.$i.jpg http://user:[email protected]/snapshot.cgi # presumably you want to wait some time after each retrieval sleep 30 done
一个明显的烦恼是,如果你已经在目录中有一个file.1.jpg,你启动这个脚本,它将被覆盖。 要处理这个问题,首先需要find所有现有的file.N.jpg文件,findN的最大值,并从N + 1开始。 这是一个令人难以置信的 braindead方式来做到这一点:
# find the last sequential file: i=$(ls -t file.*.jpg | head -1 | awk -F. '{ print $2}') # if there weren't any matching files, force $i to 0 [ $i > 0 ] || i=0 # increment by one to get your starting value for $i ((i++)) # and then start your capture mechanism while 1 do # increment our counter ((i++)) # get the file and save it wget -O file.$i.jpg http://user:[email protected]/snapshot.cgi # presumably you want to wait some time after each retrieval sleep 30 done
真的,我应该把这整件事改写成一句话,但是我累了,所以迟到了,所以我会懒惰的。 无论如何,这应该给你一个简单的shell脚本机制如何完成这个想法。