3

All

Here i want to run .sh file via system call in android NDK.

I able to run cp,rm command via system call. but sh command is not working via system call.

I also installed busy-box on android.I am using below code.I set all permission on test.sh.

Code :

#include <stdlib.h>
#include <stdio.h>
#include <android/log.h>
#include <jni.h>

#define LOG_TAG "Demo"

#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)


void Java_com_ndkdemo_NDKdemoActivity_systemcall(JNIEnv * env, jobject obj) {

    int result;

    result = system("cp /mnt/sdcard/test /Download");

    LOGD("result is cp %d", result);

    result = system("sh ./test.sh");

    LOGD("result of test.sh is %d", result);

}

Output :

 result is cp 0.
 result of test.sh is 0.

Here i am getting 0 in system("sh ./test.sh"); but not started the test.sh script.

cant get output "Hi" on console.

test.sh contains

#!/system/bin/
echo "Hi"

If i am executing direct command on prompt than its working fine and its given output "Hi" on console.

Please Help me to figure out this issue.

Thanks

1
  • also, FYI: your first line should point to the script you want to run, so should be set to #!/system/bin/sh. Commented Nov 7, 2012 at 14:43

2 Answers 2

2

Where are you expecting to see the "echo"? By default, android sends stdout to /dev/null.

According to the documentation you can redirect stdout and stderr to the log (so it will show up in logcat) using the following commands:

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start

Now you may see your echo in adb logcat.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks For Your Reply , But without this i cant able to run shell script?
You can run the shell script without doing the stdio redirect, but the echo is lost.
0

You might want to try sh -c /full/path/to/test.sh

1 Comment

OK. Note that using echo might not be the best way to test this, try using touch or something that creates a file. It's not exactly clear where the stdout of shells you start from NDK are redirected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.