How to save the output of an interactive command-line program in a Unix-like shell?
I am coding in C and running the programs in a Linux terminal. I want to save the output of the program in a .txt file. I usually do this by executing ./a.out>filen开发者_如何学运维ame.txt
But I have written an interactive program in which I have to enter a number to initiate the program. In that case how do I do it?
Thanks a lot and your valuable suggestions are most welcomed.
Move the requirement to enter a number from the terminal to a command line parameter.
./a.out 42 > filename.txt
Or, easier, accept the input from a redirected input
echo 42 | ./a.out > filename.txt ./a.out < input.txt > filename.txt
Try this
./a.out | tee filename.txt
Tee man page
You can use script
to capture all output to a file.
$ man script
Assuming you have enter the number you wish to pass the to the program in an file called 'input.txt'. If you want to re-direct the output to 'output.txt', then type at the command-line:
./a.out < input.txt > output.txt
精彩评论