Run Script Shell File On Embedded Android Device Using System Function In Android NDK

All

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

I can run the command cp,rm

through a system call. but the sh command does not work through a system call.

I also installed busy-box on android test.sh

. I am using below code. I set all permissions to .

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 starting the test.sh

script.

cannot get output "Hi"

to console.

test.sh contains

#!/system/bin/
echo "Hi"

      

If I execute a direct command at the prompt and not its working tone and its output "Hi"

to the console.

Please help me understand this issue.

thank

+3


source to share


2 answers


Where do you expect 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 that it appears in logcat) using the following commands:



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

      

You can now see your echo in adb logcat

.

+2


source


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



0


source







All Articles