用python cmd模块实现一个unix管道?

我使用
python的cmd模块实现了一个简单的shell.

现在,我想在这个shell中实现一个unix管道,就是当我输入:

ls | grep "a"  

它会将do_ls的结果传递给do_grep的输入,
最简单的方法是什么?
对不起CryptoJones,我忘了说我的平台是Windows.

最佳答案 这是一个可以帮助您的简单示例:

from cmd import Cmd

class PipelineExample(Cmd):

    def do_greet(self, person):
        if person:
            greeting = "hello, " + person
        else:
            greeting = 'hello'
        self.output = greeting

    def do_echo(self, text):
        self.output = text

    def do_pipe(self, args):
        buffer = None
        for arg in args:
            s = arg
            if buffer:
                # This command just adds the output of a previous command as the last argument
                s += ' ' + buffer
            self.onecmd(s)
            buffer = self.output

    def postcmd(self, stop, line):
        if hasattr(self, 'output') and self.output:
            print self.output
            self.output = None
        return stop

    def parseline(self, line):
        if '|' in line:
            return 'pipe', line.split('|'), line
        return Cmd.parseline(self, line)

    def do_EOF(self, line):
        return True

if __name__ == '__main__':
    PipelineExample().cmdloop()

这是一个示例会话:

(Cmd) greet wong
hello, wong
(Cmd) echo wong | greet
hello, wong
(Cmd) echo wong | greet | greet
hello, hello, wong
点赞