Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we execute a shell script file from my Android Application?

I am trying to execute a shell script from my Android application.

First I tried to run Shell script from Java ,and it working fine for all commands like pwd, cd , netstat. moving the file ,copying the file.

Than I've tried it from an Android application and I'm getting output for cd ,pwd, netstat and for echo statements that are in script, but for moving and copying the file are not working.

Are any Permissions needed to execute these commands from the script file while these commands are working fine from adb shell?

my code look like this:

void execCommandLine()
    {
       //***********************
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("ls -all");

            proc = rt.exec("sh /data/shTest.sh");
            InputStream is = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;

       while ((line = br.readLine()) != null) {
         System.out.println(line);
       }
           } catch (Throwable t)
          {
            t.printStackTrace();
          }

and my script file is as follows:

#!/bin/sh
echo "Knowledge is Power"
echo "I am a script"
echo $PATH
netstat
pwd
cd /data
pwd
cd /system/bin
pwd
  mv  /data/local/hello.txt /data/
  cp  /data/local/hello1.txt /data/
cd /data/local/tmp
cd /system/bin

Only the cp and mv commands are not showing expected result. Please give me some guidance.

First thing I am trying and testing it into Emulator. In /system/bin have mv commond and I have also tried with busybox but I didnt get expected result. For root privilage how to proceed..Give some idea to proceed.

like image 759
anshu Avatar asked Feb 10 '11 13:02

anshu


People also ask

Can you run .sh on Android?

Even though your device is rooted, android environment won't allow applications to execute 'sh' commands. As android security architecture says, applications run within a secured execution space inside Application Sandbox, sh execution can bypass this security.

How do I run a script on Android?

1. On the SureMDM Web Console, navigate to Jobs > New Job > Android > Run Script. 2. In the Run Script prompt, enter a Job Name, Script and click Save.


1 Answers

mv and cp both give me trouble in the android shell. many other people use cat x > y or similar with decent results.

like image 198
Brian Sweeney Avatar answered Oct 26 '22 14:10

Brian Sweeney