【发布时间】:2019-03-19 01:34:04
【问题描述】:
我正在尝试仅使用 os.pipe() 在 python 中复制 'cat < hello.txt | cat | cat > hello2.txt'。我不是想制造一些花哨的东西,它只是为了教育(uni)。
我真的很困惑如何做到这一点。但我正在考虑这样做:
所以我列出了管道之间要执行的所有命令 -
pipe_commands = [['/bin/cat < hello.txt'] , ['/bin/cat'], ['/bin/cat > hello2']]
for i in pipe_command:
r, w = os.pipe()
if '<' in i:
pid = os.fork()
if pid == 0:
os.close(r)
w = os.fdopen(w, 'w') # Start Writting on write end of pipe
os.dup2(w.fileno(), sys.stdout.fileno())
f= open('hello.txt', 'r') # start reading from hello.txt
os.dup2(f.fileno(), sys.stdin.fileno())
os.execl('/bin/echo', 'echo')
else:
os.wait()
os.close(w)
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno()) # read from read end of pipe
# OUTPUT TO BE GIVEN TO NEXT COMMAND
elif '>' in i:
pid = os.fork()
if pid == 0:
os.close(w) # Start reading from previous commands pipe output
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno())
f = open('hello2.txt', 'w') # write to hello2.txt
os.dup2(f.fileno(), sys.stdout.fileno())
os.execl('/bin/echo', 'echo')
else:
os.wait()
else:
pid = os.fork()
if pid == 0:
os.close(r)
w = os.fdopen(w, 'w') # Start Writting on write end of pipe
os.dup2(w.fileno(), sys.stdout.fileno())
os.execl('/bin/echo', 'echo') #input from read end of the previous command
else:
os.wait()
os.close(w)
r = os.fdopen(r, 'r')
os.dup2(r.fileno(), sys.stdin.fileno()) # read from read end of pipe
# OUTPUT TO BE GIVEN TO NEXT COMMAND
在此之后我很困惑,我该让谁执行下一个命令,即从输出中读取 '/bin/cat' 以执行第一个管道 (cat < hello.txt | cat)?
还有关于如何将其置于循环以使其自动化的任何提示? (我知道我使用的 for 循环是错误的)
我知道这不是执行管道的最佳解决方案,但我们只接受了os.pipe() 的培训,并避免使用os.system 和os.subprocess。
提前致谢
【问题讨论】:
-
您的代码甚至无法编译。请修正错误。您似乎有一些语法错误和一些错误的缩进,这弄乱了完整的程序逻辑。
-
@Corion 嗨,它更像是一个伪代码而不是实际代码,让读者了解我的前进方向。我直接在问题窗口中编写了代码,甚至没有检查它是否正在编译。
标签: python linux python-3.x bash python-2.7