executing shell commands programmatically in android

I have two commands to execute and return the data of My.sh file as two commands and it looks like this

su dhpkd et0

When I try to execute the .sh command in my Android terminal by typing in the name of the .sh file as sh It doesn't give me output, but when I execute it by printing a separate line it works. SO when I program like

nativeProcess = Runtime.getRuntime().exec("su");
nativeProcess = Runtime.getRuntime().exec("dhcpcd eth0");
while ((line = br.readLine()) != null) 
{
    contents.append(line + "\n");
}

      

What's bad about it? I am getting the content of the output as zero

+3


source to share


2 answers


exec

in Java starts a new process. So the first line creates a new process su

that is going to just sit and wait for input. The second line starts a new dhcpcd

process that will not be privileged and therefore will not produce useful output.

You want to run dhcpcd

with su

as usual:



exec("su -c dhcpcd eth0")

      

+3


source


We can execute command commands using Runtime class like this.

Runtime.getRuntime().exec("ls");

      



The above code snippet will create its own process for the given ls command, return the same process as the Process object.

More about it Check here

+4


source







All Articles