Velanar32
Velanar32

Reputation: 57

Redirecting from a .txt file to an executable file in Linux

I have a text file with all the numbers from 1 to 1000 (numbers.txt) and I have an executable file (ex2-1) that when it gets all the numbers from 1 to 1000 (it gets the numbers as input one by one) it print "Done!". When you run the file you see:

please insert 1:

If you enter 1 it shows the same but with 2 and if not it will print "wrong input" and close.

I know how to read from a text file line by line:

#!/bin/bash
filename='numbers.txt'
while read line; do
echo "$line" #echo is just to show where the number is being saved
done < $filename

But is there any way to redirect so that instead of being printed to the screen it will go to the executable file?

Upvotes: 2

Views: 1109

Answers (1)

jfMR
jfMR

Reputation: 24738

You can run all these commands in a subshell and then redirect its output through a pipe to a process corresponding to a running instance of the executable file ex2-1:

(filename='numbers.txt'; while read line; do echo "$line"; done < "$filename") | ex2-1

However, as you read the file line by line, you could simply run cat on the file numbers.txt instead:

cat numbers.txt | ex2-1

or even more concise and with just a single process:

ex2-1 < numbers.txt

Upvotes: 5

Related Questions