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.
Sweeney already gave you a solution, but here is an explanation.
Android is not GNU Linux, so the
cp
andmv
come by default are quite different. On Android they just manage hard links, so for example, if you are trying to move files across partitions, it will make your data disappear, since it doesn't move the data from partition to partition, just adjusts the hard links. Using busybox, or creatingcp
andmv
scripts, thatcat file > otherfile
and for mv arm file
is a must if you can't change the scripts, but its best to change the scripts, since it makes them portable to systems that doesn't have your hacks.The solution I use is an symbolic link. For example if in /system/bin are not present commands like cp, grep or mv command, for sure are present in busybox. For this I just remount /system as RW using command (from terminal emulator) and than create symbolic links
su mount -o rw,remount /system cd /system/bin ln -s busybox cp # cp or other command
Some hints about what could be happening to you. do you have a rooted phone? Do you have the commands mv and cp in /system/bin ? Finally, you could try to use busybox, if your phone has it. Maybe you should first request root privileges in order to do this...
mv
andcp
both give me trouble in the android shell. many other people usecat x > y
or similar with decent results.