在python中运行adb管道命令

时间:2018-12-19 16:28:22

标签: python pipe adb popen

我一直在尝试通过python脚本运行以下adb命令:

'ls -1r | grep -m1 house | xargs tail -n2'

请注意,在此行之前,我已经导航到adb shell内的正确目录,并且已经手动测试了它按预期工作的命令。经过研究后,我做了以下尝试:

使用getoutput:

output = commands.getoutput('ls -1r | grep -m1 house | xargs tail -n2')

使用Popen:

args0 = ['ls','-1r']
args1 = ['grep','-m1','house']
args2 = ['xargs','tail','-n2']
p0 = Popen(args0, shell=True, stdout=PIPE,stderr=PIPE)
p1 = Popen(args1, shell=True,stdin=p0.stdout, stdout=PIPE,stderr=PIPE)
p2 = Popen(args2, shell=True, stdin=p1.stdout,stdout=PIPE,stderr=PIPE)
(output,stderr) = p2.communicate()

但是输出/标准错误为NONE''。我也尝试过删除shell=True,或将p0.stdout.close()放在communication()行之前,也无济于事。

不确定为什么不起作用,感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以通过子进程运行adb命令。确保命令链有效

subprocess.check_output(["adb", "shell", "ls -1r | grep -m1 house | xargs tail -n2"])
相关问题