0

I have a program that waits for 'get', and then waits for multiple file inputs (either on the same line or one by one). 'quit' is then used to end input. My files reside in a folder "test_files/" in a different directory. What I want is for the script to input every file from my folder into my program.

Right now I have:

{ echo "get"; echo "file1.txt file2.txt ... fileN.txt quit"; } | ./program 

But what I want is something like this:

echo "get" | ./program for N in {files in folder} do echo "fileN.txt" | ./program done echo "quit" | ./program 
4
  • 1
    You mean for files in test_files/*; do ./program < files; done? Commented Feb 26, 2021 at 1:55
  • Why is this tagged C? You do mean any program, right? Commented Feb 26, 2021 at 2:07
  • Do you want to invoke your program multiple times, or do you just want { echo get; ls; echo quit; } | ./program? Commented Feb 26, 2021 at 2:20
  • Please, don't tag this question as C, if you are asking for a shell script to do the work. Edit the question and take off the tag. Or if it's C, then show the code you have. It's impossible to help you if you don't show the code you have written. Commented Feb 27, 2021 at 18:19

4 Answers 4

3

This should work:

#!/bin/bash for i in `ls file*.txt`; do echo $(basename $i) | ./program done 
Sign up to request clarification or add additional context in comments.

Comments

0

first you need to ensure that the program is executable then, in each execution wait for the file to be processed, which can be with wait $! (although I think I can make it wait for the end of the execution of ./program) you can also give a timeout, like half a second slee0 0.5

# before chmod +x program program get for N in $(ls files*) do ./program file$N wait $! # or #sleep 0.5 done ./program quit echo "END" 

Comments

0

If you need to run your program, then feed it with get, a list of filenames, ended by quit, you need to produce all of these from your shell script:

#!/bin/sh folder=/some/folder/in/your/filesystem # all the programs executed inside the parenthesis run in a # subshell, so the whole output of this subshell is # effectively redirected to the pipe to the program. # the attempts shown in your attempt and the proposed solutions # just execute the program several times and pass to it only one # of the commands, not all in sequence. ( echo get # for all files in folder for file in "$folder"/* do echo "$file" done echo quit ) | ./program 

Comments

0

if you need simply join files and feed them to stdin of yor program just use:

cat *.txt | ./program # or cat file1.txt file2.txt ... fileN.txt | ./program 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.