1

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
do
    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.

0
0

Assuming that the makeimage.py script always names the files

filename_YYYYNN_HHMM.png

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

while read year nr hr min sec
do
   echo "Line is >$year $nr $hr $min $sec<"
   fname="filename_${year}${nr}_${hr}${min}.png"

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

   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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.