Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problems with 'grep' command from adb

Tags:

android

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 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#?

in my code, it only does the first part (entering 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
                };
 }
like image 489
Zag Gol Avatar asked Oct 27 '14 04:10

Zag Gol


People also ask

Why are ADB commands not working?

Make sure USB debugging is enabled and USB is physically plugged in. Make sure everything is ok with the ADB drivers, double-check the device manager. Check if the device appears in "adb devices", make sure its authorized on the device. Try actual adb shell and other relevant adb stuff.

How do you fix ADB is not recognized as an internal or external command?

Set the path of adb into System Variables. You can find adb in "ADT Bundle/sdk/platform-tools" Set the path and restart the cmd n then try again. You can also go to the dir where adb.exe is located and do the same thing if you don't wanna set the PATH.

How do I browse files with ADB?

Open cmd type adb shell then press enter. Type ls to view files list. At the DOS prompt, adb shell ls -R > junk lists all files and puts results into file junk which you can then edit via Notepad or whatever and see more than you'd want to, but your files, too!

How do I target a specific device on ADB?

Just pass the -d switch to adb to target our device. If we had a single emulator instance and one or more physical devices attached we could pass in the -e switch to adb which targets emulator. The final switch we can use for device targeting is the -s switch, which stands for serial number.


1 Answers

There is no problem with grep in adb. There is a problem with your understanding of how shell works. So let's fix that:

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

When you run adb shell alone - 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).

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:

adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
like image 187
Alex P. Avatar answered Sep 22 '22 20:09

Alex P.