运行shell脚本文件在Android嵌入式设备中的Andr​​oid NDK使用系统功能嵌入式、脚本、功能、文件

2023-09-05 10:14:21 作者:浅琉璃

所有

在这里,我想通过运行在Android的NDK系统调用sh文件。

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

我能跑 CP,RM 通过系统调用命令。但sh命令不是通过系统调用的工作。

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

我也装忙盒是用低于codeI组对 test.sh 所有权限。

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

}

输出:

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

在这里我得到0 系统(SH ./test.sh); ,但没有启动 test.sh 脚本。

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包含

#!/system/bin/
echo "Hi"

如果我在提示符下执行命令,直接比它做工精细和给定输出在控制台上。

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

请帮我找出这个问题。

感谢

推荐答案

你是哪里期待看到的回声?默认情况下,Android的标准输出发送到/ dev / null的。

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

按照文档可以重定向输出和错误到日志(所以它会显示在logcat中)使用以下命令:

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

现在,你可能会看到你的回声亚行logcat

Now you may see your echo in adb logcat.