iGio90
iGio90

Reputation: 298

Exec multiple shell command with onClick

here is an example of what i'm trying to make:

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};

                 try {
                     Runtime.getRuntime().exec(hwdebug1);              
                } catch (IOException e) {
                }            

so, if i click my button, it works perfectly, But it doesn't if, example, i do something like that:

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt","echo hello > /system/hello2.txt","echo hello > /system/hello3.txt"};

My intent is to let the button exec more than 1 command. I already did it by let it execut a bash script but i prefer to find a way to put it on code.

Thanks!

Solved with Ben75 Method

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};
final String[] hwdebug2 = {"su","-c","echo hello > /system/hello2.txt"};
final String[] hwdebug3 = {"su","-c","echo hello > /system/hello3.txt"};
ArrayList<String[]> cmds = new ArrayList<String[]>();
cmds.add(hwdebug1);
cmds.add(hwdebug2);
cmds.add(hwdebug3);
for(String[] cmd:cmds){
    try {
       Runtime.getRuntime().exec(cmd);              
   } catch (IOException e) {
       e.printStacktrace(); 
   }          
}

Upvotes: 0

Views: 1032

Answers (1)

ben75
ben75

Reputation: 28706

The Runtime.exec command is working for one single command and is not a simple "wrapper" for a cmd line string.

Just create a List and iterate on it:

final String[] hwdebug1 = {"su","-c","echo hello > /system/hello1.txt"};
final String[] hwdebug2 = {"su","-c","echo hello > /system/hello2.txt"};
final String[] hwdebug3 = {"su","-c","echo hello > /system/hello3.txt"};
ArrayList<String[]> cmds = new ArrayList<String[]>();
cmds.add(hwdebug1);
cmds.add(hwdebug2);
cmds.add(hwdebug3);
for(String[] cmd:cmds){
    try {
       Runtime.getRuntime().exec(cmd);              
   } catch (IOException e) {
       e.printStacktrace(); 
   }          
}

Upvotes: 2

Related Questions