How to Use Stdin Twice from Pipe

How to use STDIN twice from pipe

You don't need to use a pipe. If you are using bash use process-substitution as <(cmd) i.e. to achieve a redirection where the input or output of a process (some sequence of commands) appear as a temporary file.

awk 'FNR==NR {col1[$1]++; col2[$2]++; next} {print $0, col2[$2] "/" length(col1)}' <(cut -f3 5- input) <(cut -f3 5- input)

python pipe only stdin,out once, how to do twice or more time

the output of call.py is buffered. so you have to flush() it to send to main.py.

#!/usr/bin/python2
import sys

getMsg = raw_input()
print getMsg
sys.stdout.flush()

getMsg2 = raw_input()
print getMsg2
sys.stdout.flush()

Note that you need shebang #!/usr/bin/python2 at least when your OS is Linux (I don't know why OP's code works without shebang. Maybe some Windows magic ?).

Also you can use -u option not to buffer the output of python.

player_pipe = subprocess.Popen(["/usr/bin/python2","-u","./call.py"], stdin=PIPE,
stdout=PIPE, stderr=STDOUT, shell=False)

Ruby use stdin twice

I'll assume that stdin, in this case, is a pipe.

You can't rewind a pipe. What you need to do is to copy the pipe's data into a regular file, then use that file as stdin for the two processes you're calling.

(You can also copy into a memory buffer, and then use a pipe to send the contents to the two processes.)


Update: here's a simple Ruby script that checks whether stdin is a regular file, and if not, it creates a temp file and copies stdin to it first, then rebinds stdin to the temp file. Then it runs each argument as a command:

#!/usr/bin/ruby
require 'tempfile'

unless File.file?($stdin)
Tempfile.open('stdin') do |temp|
IO.copy_stream($stdin, temp)
$stdin.reopen(temp)
temp.unlink
end
end

ARGV.each do |arg|
$stdin.rewind
system arg
end

Example usage (assuming the file is named multiwrite in the current directory):

$ echo foo | ./multiwrite cat 'tr a-z A-Z' 'tr a-z n-za-m'
foo
FOO
sbb

If it's a regular file, then the solution is easy: call the first process, then rewind, then call the second process.

Redirecting stdin twice?

As a terrible hack, you need to ensure that scanf's buffer is full. Something like:

{ printf 10; dd if=/dev/zero bs=4094 count=1;
echo This text will go to the read if bufsize is 4096; } | program_name

The technique here is relying on scanf reading the first 4096 bytes to fill its buffer on its first read, leaving data in the pipe for the read to get. The main problem is that it is extremely fragile and requires intimate knowledge of the buffering used. Overall, this is a terrible idea, but not too much worse that calling read after calling scanf on the same file descriptor.

How to send STDIN twice to Popen process, each time with EOF?

EOF is not a character, it just means there is no more data to read.

As such I don't believe what you're after is possible in Python or most other languages.

Reading From Stdin Twice in C

You can use rewind(stdin) to set the stream back to the start of file, but be aware that it is not guaranteed to work, especially if the stream is a pipe, a terminal or a device.

Your allocation scheme is incorrect: you could compute the size of the file and then allocate that many bytes, but your current (char*)calloc(lineNum * 18,sizeof(int)); allocates 18 times the size of type int for each line. Some files with short lines will fit in this array while others will invoke undefined behavior.

Note that c must be defined as int for c = fgetc(stdin); to properly store all values including the EOF special value.

How to avoid duplicate input on stdin when using 2-process pipeline with stdin and stdout redirect

The problem is that this line:

 execve(argv[0], p1argv, p1envp);

Is re-executing the main parent program, because that is what the content of argv[0] is at this point. I think you want to find some way to specify "./p1" there.



Related Topics



Leave a reply



Submit