【发布时间】:2023-04-10 13:27:02
【问题描述】:
我一直在尝试通过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()
但是输出/stderr 是NONE 或''。我也试过删除shell=True,或者把p0.stdout.close()放在通信()行之前,也没有做任何事情。
不知道为什么它不起作用,感谢任何帮助!
【问题讨论】: