0

I am trying to execute the following command;

String cmd = "/system/bin/netstat -"+firstString+" | grep http";
Process process = Runtime.getRuntime().exec(cmd);
Log.d("1:CMD VALUE", cmd);

The log result is 1:CMD VALUE: /system/bin/netstat -a | grep http

However, the output is not getting filtered for the 'http' parameter. The regular "/system/bin/netstat -a" result is what I get for the "/system/bin/netstat -a | grep http`" command as well. Why is this? Is this because of the pipe symbol or the grep command? Please help. Thanks.

Zac1
  • 208
  • 7
  • 34
  • if `grep` is a binary executable file (a program), then it simply may be absent in Android system since Android is not full linux. – Vladyslav Matviienko Nov 30 '16 at 06:48
  • http://stackoverflow.com/a/5928316/1904517 – iagreen Nov 30 '16 at 07:21
  • @iagreen: I tried the String array before I posted the question, but still, it gives the same output, i.e. grep doesn't work or the pipe doesn't work?! . My cmd array was {"/system/bin/netstat", "-"+firstString, " | grep http"} - does this look correct? Vlad: How to check if it is absent? – Zac1 Nov 30 '16 at 17:03
  • No. The important part about that link is that "|" is not supported by `exec`, but by the shell. You need to invoke `sh` and then have `sh` run your command. – iagreen Nov 30 '16 at 17:12
  • Can you please give me an example? I added "bash -c" to my command, and I get the message that I need root permission to invoke bash. – Zac1 Nov 30 '16 at 17:18

1 Answers1

1

Other answers on this website were not very clear. Here is the solution. You need to append "/system/bin/sh -c" before your runtime command for grep to work. Cheers.

Zac1
  • 208
  • 7
  • 34