Not able to run grep command

瘦欲@ 提交于 2019-12-25 04:16:06

问题


I was trying to run the following instruction,

 Process p = Runtime.getRuntime().exec("/system/bin/lsof|grep mediaserver");

In android(java) but I am getting error. if I run following instruction,

 Process p = Runtime.getRuntime().exec("/system/bin/lsof ");

the file is successfully saved.Can anyone tell what is the error? Actually I want to list and check if media server service is being running or not.


回答1:


The grep utility may not be installed on your device.

You can check it by trying the following in a console:

> adb shell
$ grep
grep: not found

The last line indicates that this command is not available.




回答2:


The problem is that Runtime.getRuntime().exec(...) does not know how to deal with the shell language. On a Linux / Unix platform you would so something like this:

Process p = Runtime.getRuntime().exec(new String[]{
    "/bin/sh", "-c", "/system/bin/lsof | grep mediaserver"});

However, (apparently) Android doesn't have a command shell / command prompt by default. So either you need to identify and install a suitable shell on your device, or construct the pipeline "by hand"; i.e. by creating a "pipe" file descriptor, and execing the two commands so that lsof writes to the pipe and grep reads from it.

Maybe the answer is to run it like this ...

Process p = Runtime.getRuntime().exec(
        "adb shell /system/bin/lsof | grep mediaserver");

(Try running the "shell ...." part from adb interactively before doing it from Java.)



来源:https://stackoverflow.com/questions/12143646/not-able-to-run-grep-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!