I have a script that creates images from files with certain date and time. This date and time is stored in list.txt file, few lines with same date and different time

2020 03 12 11 45
2020 03 12 10 23
2020 03 12 04 12
2020 03 12 00 23

I read this TXT file in a while loop:

while read line
    echo Line is... $line
    echo $line > $BIN/line.txt

    python3 $BIN/makeimage.py #creates image 
done < $BIN/list.txt

The filename of output images looks like: filename_202003_1245.png.

What's the easiest solution, to check if the file already exists and then skip to next line in list.txt? Now my script is just overwriting images again and again. Not sure if I have to delete line from list.txt as soon as image is created OR use awk - grep to extract hour and minute from list.txt and then from filename and compare them.


1 Answer 1


Assuming that the makeimage.py script always names the files


with a fixed string filename, you could perform the following modification to your script:

while read year nr hr min sec
   echo "Line is >$year $nr $hr $min $sec<"

   if [[ -f "$fname" ]]
      echo "File $fname already exists, skipping"

   echo "$year $day $hr $min $sec" > "$BIN/line.txt"

   python3 "$BIN"/makeimage.py #creates image 
done < $BIN/list.txt

This will assemble the prospective filename in the shell variable $fname and use the bash test operator [[ ... ]] to check if a file name $fname already exists in the current directory (if the files are in a different directory, prepend that in either the $fname variable or in the test construct). If so, it skips to the next loop iteration using the continue statement.

As side-note, you should always double-quote shell variables unless you explicitly want glob expansion and word-splitting to happen.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .