How to save interactive command line output in a Unix-like shell?

I code in C and run programs in a Linux terminal. I want to save the output of a program in a .txt file. I usually do this by doing./a.out>filename.txt

But I wrote an interactive program in which I need to enter a number to start the program. In this case, how to do it?

Thank you very much and your valuable suggestions are most welcome.

+3
source share
4 answers

Move the requirement to enter the number from the terminal into the command line parameter.

./a.out 42> filename.txt

Or, more simply, accept input from redirected input

echo 42 | ./a.out> filename.txt
./a.out <input.txt> filename.txt
+5
source

script .

$man script

+5

./a.out | tee filename.txt

Tee

+4

Assuming you enter the number you want to pass to the program into a file called 'input.txt'. If you want to redirect the output to 'output.txt', at a command prompt, type:

./a.out  <  input.txt  >  output.txt
+1
source

Source: https://habr.com/ru/post/1795881/


All Articles