Redirect two or more STDOUT to a single STDIN
Yes.
{ command1 ; command2 ; } | command3
Is it possible to redirect stdout as stdin to same program?
Yes, you can create a pipe to yourself in any POSIX system.
But "There ain't no such thing as a free lunch". If you need to store data, it has to go somewhere. The pipe is backed by a buffer managed by the OS.
You can literally set the ends of the pipe to back stdin
and stdout
(as in Joachim's answer), but I'd advise against that, as it would override the usual input and output channels of the program. (Neither C nor any POSIX service will "tee" the stream so the user can see it as well as have it echoed inside the program.)
Redirect stdout and stdin to file
Working off the responses here, I came up with this:
d=`tty`
exec >b.txt <b.txt
echo a
read c
exec >$d <$d
I also realized that it might be better to leave stdout and stdin alone:
exec 3>b.txt 4<b.txt
echo a >&3
read c <&4
Redirect stdin to stdout
You could use expect for this. Expect is a tool for automating interactive command-line programs. Here's how you could automate typing those values in:
#!/usr/bin/expect
set timeout 20
spawn "./a.out"
expect "Enter a: " { send "10\r" }
expect "Enter b: " { send "20\r" }
interact
This produces output like this:
$ ./expect
spawn ./test
Enter a: 10
Enter b: 20
a + b = 30
There are more examples here.
bash - redirect specific output from 2nd script back to stdin of 1st program?
An additional expect
-like software tool would be empty
!
http://empty.sourceforge.net
Redirect Stdin and Stdout to File
This is expected (correct) behaviour. The input is never part of stdout. If you do example.exe > output.txt
and blindly type in 42, you should expect that 42 also shows up only once in the output.
The only solution I could think of is that the terminal/shell records the session as a whole. Windows command shell is not capable of that. You could write your own terminal proxy though, which feeds stdin into the student's program and reads the output itself, while writing out both in a combined fashion. It is quite easy to fork for exection of another program and redirect that one's stdin/out under POSIX (provided to you by Cygwin), I don't know about plain DOS/Windows though.
Popen: Sending stdout from one process to the stdin of two processes
you cannot do this. You can only pipe the output to one process, as pipe consumes the data.
but you can read all output from process, and run other processes with this output as the argument of communicate
.
from subprocess import Popen, PIPE
input = Popen(['python', 'input.py'], stdout = PIPE)
contents = input.stdout.read()
proc1 = Popen(['python', 'print.py'], stdin = PIPE)
proc2 = Popen(['python', 'print.py'], stdin = PIPE)
proc1.communicate(contents)
proc2.communicate(contents)
This isn't going to run in parallel obviously, unless you thread both proc1
& proc2
Popen
calls. And the first process will have to run completely first.
Another possibility would be to feed inputs to both processes without using communicate
:
from subprocess import Popen, PIPE
proc_in = Popen(['python', 'input.py'], stdout = PIPE)
proc1 = Popen(['python', 'print.py'], stdin = PIPE)
proc2 = Popen(['python', 'print.py'], stdin = PIPE)
for line in proc_in.stdout:
proc1.stdint.write(line)
proc2.stdint.write(line)
proc1.wait()
proc2.wait()
in that case everything runs in parallel. Each time an output is produced from first process it is fed to both processes, in real time.
now realizing that the question is a duplicate of Python: Redirect stdout of a subprocess to stdin of 2 or more subprocesses. The answer is maybe too tailored to the question and complex. Leaving that one here.
Related Topics
Explanation of Memcpy Memmove Glibc_2.14/2.2.5
Can Old Arm32 Binary Files Be Run on Aarch64 Kernel
Does the Linux Scheduler Prefer to Run Child Process After Fork()
How to Change Alignment of Code Segment in Elf
Building Opencv as Static Libraries
Total Number of Lines in a Directory
Why Exported Variables in Makefile Is Not Received by Executable
How to Use a Linux Expect Script to Enter Answer a Prompt for Password
How to Capture All the Commands Typed in Unix/Linux by Any User
How to Find the Count of Multiple Words in a Text File
How to Narrow Down Perf.Data to a Time Sub Interval
How to Substitute Shell Variables in Complex Text Files
How to Check If a File Contains Only Zeros in a Linux Shell
How to Prevent Matlab Printing False Space and Use Wrong Fonts