How do I redirect stdin/stdout when I have a sequence of commands in Bash?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-04-16T05:11:00Z Indexed on 2010/04/16 5:13 UTC
Read the original article Hit count: 252

Filed under:
|
|
|

I've currently got a Bash command being executed (via Python's subprocess::Popen) which is reading from stdin, doing something and outputing to stdout. Something along the lines of:

pid = subprocess.Popen( ["-c", "cmd1 | cmd2"],
                       stdin = subprocess.PIPE, 
                       stdout = subprocess.PIPE, 
                       shell =True )
output_data = pid.communicate( "input data\n" )

Now, what I want to do is to change that to execute another command in that same subshell that will alter the state before the next commands execute, so my shell command line will now (conceptually) be:

cmd0; cmd1 | cmd2

Is there any way to have the input sent to cmd1 instead of cmd0 in this scenario? I'm assuming the output will include cmd0's output (which will be empty) followed by cmd2's output.

cmd0 shouldn't actually read anything from stdin, does that make a difference in this situation?

I know this is probably just a dumb way of doing this, I'm trying to patch in cmd0 without altering the other code too significantly. That said, I'm open to suggestions if there's a much cleaner way to approach this.

© Stack Overflow or respective owner

Related posts about bash

Related posts about pipes