Вопрос

I have a folder containing 100 pictures from a webcam. When the webcam sends a new picture, I want this one to replace number 0 and have all the other jpg's move up one number. I've set up a script where inotify monitors a directory. When a new file is put into this directory the script renumbers all the files in the picture directory, renames the new uploaded picture and puts it in the folder with the rest. This script 'sort of' works. 'Sort of', because sometimes it does what it's supposed to do and sometimes it complains about missing files:

mv: cannot stat `webcam1.jpg': No such file or directory

Sometimes it complains about only one file, sometimes 4 or 5. Of course I made sure all 100 files were there, properly named before the script was run. After the script is run, the files it complains about are indeed missing. This is the script, in the version I tested the full paths to the directories are used of course.

#!/bin/bash
dir1= /foo # directory to be watched
while inotifywait -qqre modify "$dir1"; do
cd /f002 #directory where the images are
for i in {99..1}
do
 j=$(($i+1))
 f1a=".jpg"
 f1="webcam$i$f1a"
 f2="test"
 f2="webcam$j$f1a"
 mv $f1 $f2 
done
rm webcam100.jpg
mv dir1/*.jpg /f002/webcam0.jpg 
done

I also need to implement some error checking, but for now I don't understand why it is missing files that are there.

Это было полезно?

Решение

You are executing the following mv commands:

mv webcam99.jpg webcam100.jpg
...
mv webcam1.jpg webcam2.jpg

The mv webcam0.jpg to webcam1.jpg is missing. With the first change to "$dir" you have the following files in /foo2:

webcam99.jp
...
webcam2.jpg
webcam0.jpg

With subsequent "$dir" change you will have the following:

webcam99.jp
...
webcam3.jpg
webcam0.jpg

In other words -- you are forgetting to move webcam0.jpg to webcam1.jpg. I would modify your script like this:

rm webcam99.jpg
for i in {98..0}
do
     j=$(($i+1))
     f1a=".jpg"
     f1="webcam$i$f1a"
     f2="test"
     f2="webcam$j$f1a"
     mv $f1 $f2 
done
mv dir1/*.jpg /f002/webcam0.jpg 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top