1

I have a compiled program which i run from the shell; as i run it, it asks me for an input file in stdin. I want to run that program in a bash loop, with predefined input file, such as

for i in $(seq 100); do
    input.txt | ./myscript
done

but of course this won't work. How can I achieve that? I cannot edit the source code.

3 Answers 3

2

Try

for i in $(seq 100); do
    ./myscript < input.txt
done

Pipes (|) are inter-process. That is, they stream between processes. What you're looking for is file redirection (e.g. <, > etc.)

Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script.

You may see cat used for this e.g. cat file | mycommand. Given the above, this usage is redundant and often the winner of a 'Useless use of cat' award.

Sign up to request clarification or add additional context in comments.

1 Comment

maybe i didn't explain myself well. the program starts running, THEN, after some internal work, he asks me for input. So your way does not work for me
0

You can use:

./myscript < input.txt

to send content of input.txt on stdin of myscript

3 Comments

maybe i didn't explain myself well. the program starts running, THEN, after some internal work, he asks me for input. So your way does not work for me.
Which program asks for input? Is it myscript or your compiled program? Can youst your complete script so that I can suggest.
It is a compiled FORTRAN program; i do not have the source code. Anyway I may have found a solution by making use of expect
0

Based on your comments, it looks like myscript prompts for a file name and you want to always respond with input.txt. Did you try this?

for i in $(seq 100); do
    echo input.txt | ./myscript
done

You might want to just try this first:

echo input.txt | ./myscript

just in case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.