• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps problems with 'grep' command from adb

zaalgol

Lurker
Oct 27, 2014
2
0
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
};
}
 
The windows shell is getting confused by your pipe operator, and trying to break apart your command string. After all, pipes do work on Windows.

C:\Users\deprogram>adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
'grep' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\deprogram>adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
mCurrentFocus=Window{u0 com.google.android.talk/com.google.android.apps.hangouts.phone.ConversationActivity}
mFocusedApp=AppWindowToken{oken=Token{ ActivityRecord{com.google.android.talk/com.google
.android.apps.hangouts.phone.ConversationActivity t4}}}

C:\Users\deprogram>
So, your arguments parameter in your code would be something like:

Arguments = "/c" + " adb shell \"dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'\"",
You basically want the shell to treat the entire android shell command line as a single parameter. Quoting will do that for you.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones