Problems with grep command from adb

when i write in 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 divided it into two operators:

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

it works fine (it gives the name of the main activity of the running application).

if the only way is to split it into two - these are meens that first go into the adb shell and then run the request, is there a way to do this from C #?

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

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
                };
 }
+4
source share
1 answer

As adbthere is no problem with grep. There is a problem with your understanding of how it works shell. So let's fix this:

adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp' dumpsys window windows Android. adb shell grep Windows. , - grep.

adb shell - adb, , , Android. . . , ( , ).

- "pipe" :

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

All Articles