简体   繁体   中英

Run shell script from android application

I have one shell script like this,

    #Script name : test.sh
    mkdir /boot
    mount -t vfat /dev/block/mmcblk0p1 /boot
    cp file1 /boot
    umount /boot

    mkdir -p /test1/test2/test3
    cp file2 /test1/test2
    cp file3 /test1/test2/test3
    STATUS=TRUE

Now this script is located in /test/ directory. I am calling this script from ac function which is called from android application via jni. I am using this function to call my script in C

void Java_com_ndkdemo_NDKdemoActivity_systemcall(JNIEnv * env, jobject obj) {
    fp = popen(". /test/./test.sh; echo STATUS=$STATUS","r");
    while (fgets(buffer, sizeof (buffer), fp) != 0)
    {
        LOGD("%s",buffer);
    }
}

Now when ever i call this systemcall function from my activity, it is not able to execute the commands inside the script test.sh The same script is working if i am compiling a binary from normal C source code and execute that binary on console. I have tried to give permission with "chmod 777 /test/test.sh" but still it's not working.

Any help will be appreciated. Thank you.

If your shell interpreter is /system/bin/sh then:

  • for shell scripts that start with a shebang , make it #!/system/bin/sh

  • try to use the interpreter path itself instead of the . character to run your script

And it seems that popen may not work in older versions of Android .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM