ubuntu@ubuntu:/home/ubuntuUser$ cat test.txt
This is a test file
used to validate file handling programs
#pyName: test.txt
this is the last line
ubuntu@ubuntu:/home/ubuntuUser$ cat test.txt | grep "#pyName"
#pyName: test.txt
ubuntu@ubuntu:/home/ubuntuUser$ "
#1 >>> a = subprocess.Popen(['cat test.txt'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
#2 >>> o, e = a.communicate(input='grep #pyName')
#3 >>> print o
#3 This is a test file
#3 used to validate file handling programs
#3 #pyName: test.txt
#3 this is the last line
#3
#3 >>>
问题:
Q1:文件上的shell grep 命令只打印匹配的行,而通过子进程的grep 打印整个文件。怎么了?
Q2:通过communicate()发送的输入如何添加到初始命令('cat test.txt')中?之后#2
,初始命令是否会在“|”之后附加来自通信的输入字符串 和shell命令变得像cat test.txt | grep #pyName
?