运行 python 中的 adb 管道命令
Run adb pipe commands in python
我一直在尝试通过 python 脚本 运行 下面的 adb 命令:
'ls -1r | grep -m1 house | xargs tail -n2'
请注意,在这一行之前,我已经导航到 adb shell 中的正确目录,并且我手动测试了它按预期工作的命令。在做了一些研究之后,我做了以下尝试:
使用获取输出:
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()
但是 output/stderr 是 NONE
或 ''
。我也尝试删除 shell=True
,或将 p0.stdout.close()
放在 communicate() 行之前,也没有做任何事情。
不确定为什么它不起作用,感谢任何帮助!
您可以 运行 通过子进程执行 adb 命令。确保命令链有效
subprocess.check_output(["adb", "shell", "ls -1r | grep -m1 house | xargs tail -n2"])
我一直在尝试通过 python 脚本 运行 下面的 adb 命令:
'ls -1r | grep -m1 house | xargs tail -n2'
请注意,在这一行之前,我已经导航到 adb shell 中的正确目录,并且我手动测试了它按预期工作的命令。在做了一些研究之后,我做了以下尝试:
使用获取输出:
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()
但是 output/stderr 是 NONE
或 ''
。我也尝试删除 shell=True
,或将 p0.stdout.close()
放在 communicate() 行之前,也没有做任何事情。
不确定为什么它不起作用,感谢任何帮助!
您可以 运行 通过子进程执行 adb 命令。确保命令链有效
subprocess.check_output(["adb", "shell", "ls -1r | grep -m1 house | xargs tail -n2"])