Rotary Heart
Rotary Heart

Reputation: 1969

Send multiple shell commands with app

Ok I'm learning how shell commands work, so I decided to develop a app to send the commands. This is what I got.

moveDirectory.setOnClickListener(new OnClickListener(){
    public void onClick(View v)
    {
        try{
            Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/", "cp pic1 /sdcard/pic1"});
            send.waitFor();
        } catch (Exception ex){
            String toast = null;
            Log.i(toast, "Couldn't copy file", ex);
            }
        }
    });

But it isn't working, the first command is working, but not the second one. What should I add to it?

Thanks

EDIT: forgot to add the send.waitFor(); line

Upvotes: 0

Views: 2781

Answers (2)

Marius Kjeldahl
Marius Kjeldahl

Reputation: 6824

I'm speculating, but you may have misunderstood what the parameter to exec really is. It's not a list of commands to be executed (effectivly a batch/shell script), but a single command WITH it's arguments to be executed by a shell. Making it a one-liner like Pepelac suggests or putting the series of commands into a single file that you execute with exec later may be what you are looking for. For the command you are trying to execute there is absolute no reason why you can not make it a one-liner with the full source path included (instead of changing to it), but there may be other reasons why you need to do this that you have not mentioned.

Upvotes: 0

Laser
Laser

Reputation: 6960

Use normal command delimeter ;

moveDirectory.setOnClickListener(new OnClickListener(){
    public void onClick(View v)
    {
        try{
            Process send = Runtime.getRunetime().exec(new String[] {"cd /sdcard/music/ ; cp pic1 /sdcard/pic1"});
        } catch (Exception ex){
            String toast = null;
            Log.i(toast, "Couldn't copy file", ex);
            }
        }
    });

In this code you
1) go to the /sdcard/music
2) copy from /sdcard/music pic1 to /sdcard/pic1

Upvotes: 3

Related Questions