2

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 a c 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.

1 Answer 1

1

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.

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

2 Comments

Yup tried this things too..but still not working, by the way what will the difference if i will use interpreter instead of . character? I am using this . because i need that STATUS variable in current console other wise i will not be able to use it.
Why do you need the STATUS variable? Maybe you can put it inside your test.sh and simplify your command, to minimize the places where a problem may appear.

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.