从Android应用程序运行shell脚本 [英] Run shell script from android application

查看:207
本文介绍了从Android应用程序运行shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本这样的,

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

现在这个脚本位于/测试/目录下。
我打电话从由通过JNI android应用称为C函数此脚本。
我使用这个函数来调用我的C脚本

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);
    }
}

现在当过我叫我从这个活动功能的系统调用,它不能执行该脚本test.sh内部的命令
同样的脚本工作,如果我编译正常的C源$ C ​​$ C二进制并执行控制台二进制文件。我试图赋予权限与搭配chmod 777 /test/test.sh,但仍然不工作。

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.

任何帮助将AP preciated。
谢谢你。

Any help will be appreciated. Thank you.

推荐答案

如果你的外壳间preTER是 /系统/ bin / sh的则:

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


  • 对于与家当启动shell脚本,使它#!/系统/ bin / sh的

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

尝试使用除preTER路径本身,而不是字符的运行脚本

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

和似乎 popen这可能不起作用在旧版本的Andr​​oid 的。

这篇关于从Android应用程序运行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆