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
};
}
There is no problem with
grep
inadb
. There is a problem with your understanding of howshell
works. So let's fix that:In your
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
command onlydumpsys window windows
part runs on Android. Bothadb shell
andgrep
commands are being run on your Windows PC. Thus the error you get - you just don't havegrep
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: