0

The idea is that I want to execute a command, then it asks for a file name to process, and after it is done with processing, it outputs an image with the name prediction.png, it asks for the next filename and so on. I have thousands of files, so I cannot enter them manually one by one. However, I have a .txt file containing all the filenames.

So I wanted to write a bash script which:

1- execute the command

2- when it asks about a filename, it reads the first line from the txt file

3- the program will process the file, outputs an image, then cp this image to another directory (to avoid overwriting for the next file)

4- then asks for the next, do the same and so on

until the program asks for a file, and already the .txt file is done, then it terminates automatically

What I can do up till now is reading files one by one and passing them as answers for my program when it asks using

cat filenames.txt | ./program.sh

The problem, is that I want to cp the output before processing the new file

closed as unclear what you're asking by Rui F Ribeiro, Sparhawk, schily, Thomas, G-Man Jul 30 '18 at 20:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • crossposted: askubuntu.com/questions/1060761/… – steeldriver Jul 30 '18 at 13:11
  • it is a new question to the same script. – pLumo Jul 30 '18 at 13:12
  • @steeldriver yes different question but related to the same script – Mostafa Hussein Jul 30 '18 at 13:17
  • Do you have the ability to modify program.sh? – glenn jackman Jul 30 '18 at 14:35
  • @glennjackman it is an exe dependent on some c files, so it is too much, because I need to edit the c files and then make again and so that's why I was looking for an easier solution – Mostafa Hussein Jul 31 '18 at 6:30
1

I don't think you can do this while running program.sh just once, as it will continue creating the files, and you can never be sure when a file is complete.

Instead run the program once for each line filenames.txt:

while IFS= read -r FILE; do # loop through files in filenames.txt
  echo "$FILE" | ./program.sh # run program once and end it with newline
  mv created_filename /some/other/location/${FILE}.ext; # move file
done < filenames.txt
  • Use mv instead of cp, it will be faster if you're moving to the same file system.
  • Change .ext to the output format (e.g. .png).
  • The problem that running the program takes time, but it is already supporting the option of passing multiple files. That's why it is much much faster to run it once, then whenever it asks for a file, I pass its name to it – Mostafa Hussein Jul 30 '18 at 12:41
  • If you have access to the original script, edit it, but without being able to change it, I think you have no choice. – pLumo Jul 30 '18 at 12:43
  • concerning the part where you said 'I will never be sure when a file is complete', no I will, because when it is done with a file, it asks for a new file, and accordingly, I can pass the new file name – Mostafa Hussein Jul 30 '18 at 12:44
  • 1
    someone familiar with expect may help you ... – pLumo Jul 30 '18 at 13:15
  • 1
    echo -e "$FILE\n" is too much work: what's wrong with plain echo "$FILE" ? If you want that much control over the newlines in the output, use printf "%s\n" "$FILE" – glenn jackman Jul 30 '18 at 14:08
-1

You can do it like this:

(
while IFS= read -r FILE; do
    echo "$FILE"
    sleep 1 # or somehow make a trigger to see when the file is ready ...
    mv output_file /somewhere/else
done < filenames.txt;
) | ./program.sh

(via)


same using xargs:

(
xargs -a filenames.txt -I{} sh -c 'echo {}; sleep 1; mv file /somewhere/else'
) | ./program.sh
  • Please tell me what is wrong when downvoting, so I can learn. – pLumo Jul 31 '18 at 7:36

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