Android 来自 adb 的“grep”命令的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26581034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:54:12  来源:igfitidea点击:

problems with 'grep' command from adb

androidadb

提问by Zag Gol

when i write in adb:

当我在 adb 中写入时:

adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

i get the error output:

我得到错误输出:

'grep' is not recognized as an internal or external command, operable program or batch file.

but if i split it to two operators:

但如果我将其拆分为两个运营商:

adb shell 
dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

it works okay (it gives the main activity name of the running app).

它工作正常(它给出了正在运行的应用程序的主要活动名称)。

if the only way is to split it to two - that meens that first enter to adb shell, and then run the Inquire, there is a way to do it from c#?

如果唯一的方法是将它分成两部分 - 那意味着首先进入 adb shell,然后运行查询,有没有办法从 c# 中做到这一点?

in my code, it only does the first part (entering shell).

在我的代码中,它只执行第一部分(进入 shell)。

here is my code:

这是我的代码:

 public static void startNewProccess(object startInfo)
 {
        p = new Process();
        p.StartInfo = (System.Diagnostics.ProcessStartInfo)startInfo;
        p.Start();
        p.WaitForExit();
 }

 public static void getMainActivity()
 {
 var startInfo1 = new System.Diagnostics.ProcessStartInfo
                { 
                    WorkingDirectory = @ADB_FOLDER,
                    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
                    FileName = "cmd.exe",
                    Arguments = "/c" + " adb shell",
                    //adb shell am start -n com.package.name/com.package.name.ActivityName
                    UseShellExecute = false
                };
                startNewProccess(startInfo1);

                var startInfo2 = new System.Diagnostics.ProcessStartInfo
                {
                    WorkingDirectory = @ADB_FOLDER,
                    WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
                    FileName = "cmd.exe",
                    Arguments = "/c" + " dumpsys window windows | grep -E   'mCurrentFocus|mFocusedApp'",
                    UseShellExecute = false
                };
 }

回答by Alex P.

There is no problem with grepin adb. There is a problem with your understanding of how shellworks. So let's fix that:

in没有问题grepadb。您对shell工作原理的理解存在问题。所以让我们解决这个问题:

In your adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'command only dumpsys window windowspart runs on Android. Both adb shelland grepcommands are being run on your Windows PC. Thus the error you get - you just don't have grepavailable.

在您的adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'命令中,只有dumpsys window windows部分在 Android 上运行。双方adb shellgrep命令正在Windows PC上运行。因此你得到的错误 - 你只是没有grep可用。

When you run adb shellalone - you start an interactive adb shell session and everything you enter get executed on the Android side. This works great for manual testing. But adds an extra complexity layer when used for automation. To use the interactive mode from your code you would need multiple threads (one for the shell itself, another for sending the commands).

当您adb shell单独运行时 - 您启动一个交互式 adb shell 会话,您输入的所有内容都会在 Android 端执行。这对于手动测试非常有用。但是当用于自动化时会增加一个额外的复杂层。要使用代码中的交互模式,您需要多个线程(一个用于 shell 本身,另一个用于发送命令)。

But in your case you do not really need all that complexity - just escape the "pipe" character or put the whole shell command in quotes like this:

但在你的情况下,你并不真正需要所有的复杂性 - 只需转义“管道”字符或将整个 shell 命令放在这样的引号中:

adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"