问题描述
当我在adb中编写时
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
我得到错误输出:
'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'
工作正常(给出正在运行的主要活动名称app)。
it works okay (it gives the main activity name of the running app).
如果唯一的方法是将其拆分为两个-意思是首先进入adb shell,然后运行Inquire,则有一种方法在我的代码中,它来自c#?
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#?
,它只做第一部分(进入shell)。
in my code, it only does the first part (entering shell).
在这里是我的代码:
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
};
}
推荐答案
没有 adb
中的 grep
问题。您对 shell
的工作方式的理解存在问题。因此,请修复此问题:
There is no problem with grep
in adb
. There is a problem with your understanding of how shell
works. So let's fix that:
在您的 adb shell dumpsys窗口中| grep -E mCurrentFocus | mFocusedApp
命令仅在Android上运行 dumpsys窗口窗口
部分。 adb shell
和 grep
命令都在Windows PC上运行。因此,您得到的错误-您只是没有 grep
。
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.
当您运行<$ c $仅使用c> adb shell -您启动了一个交互式adb shell会话,您输入的所有内容都会在Android端执行。这非常适合手动测试。但是在用于自动化时会增加一个额外的复杂性层。要从代码中使用交互模式,您将需要多个线程(一个用于shell本身,另一个用于发送命令)。
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).
但是在您的情况下,您并不需要需要所有这些复杂性-只需转义 pipe字符或将整个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:
adb shell "dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'"
这篇关于亚行的“ grep”命令出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!