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

I am currently executing a Bash command (via Python subprocess.Popen) that reads from stdin, does something and outputs to stdout. Sort of:

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

Now I want to change this to execute another command in the same subshell, which will change state before executing the following commands, so my shell command line will now (conceptually) be:

cmd0; cmd1 | cmd2

Is there a way for an input to be sent in cmd1instead of cmd0in this scenario? I assume that the output will include the output cmd0(which will be empty), followed by the output cmd2.

cmd0doesn't really read anything out stdin, does it really matter in this situation?

, , , , cmd0, . , , .

+3
3

cmd0 cmd1 /dev/null stdin cmd0:

(cmd0 </dev/null; cmd1) | cmd2
+4

, - . cmd0 stdin, cmd1. :

ls | ( echo "foo"; sed 's/^/input: /')

( ls )

cmd2 , .

+1

, , stdin , , cmd0, cmd1:

exec 0>&3; exec 0<&-; cmd0 ; exec 3>&0 ; cmd1 | cmd2

Not sure if it can be redirected stdinthis way, but cannot verify it at the moment.

http://tldp.org/LDP/abs/html/io-redirection.html

http://tldp.org/LDP/abs/html/x17601.html

0
source

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


All Articles