Android应用程序如何从/res/script.sh运行.sh文件

时间:2015-08-20 15:19:52

标签: android

我在/ res /目录下的Android Studio项目中有bash脚本。如何通过library(svDialogs) columnFunction <- function (x) { column.D <- dlgList(names(x), multiple = T, title = "Spalten auswaehlen")$res if (!length((column.D))) { cat("No column selected\n") } else { cat("The following columns are choosen:\n") print(column.D) x <- x[,!names(x) %in% column.D] } return(x) } df <- columnFunction(df) 在我的Android应用中运行此文件?

编辑: 所有代码

Runtime.getRuntime().exec(path);

我没有日志。

2 个答案:

答案 0 :(得分:1)

更新:第一个解决方案仅执行/res/raw/中文件中的命令列表。这不是一个真正的解决方案。

查看第二部分以获得完整的解决方案。

InputStream ins = getResources().openRawResource(
            getResources().getIdentifier("my_file_name_without_extension", "raw", getPackageName()));

        BufferedReader r = new BufferedReader(new InputStreamReader(ins));
        StringBuilder total = new StringBuilder();
        String line;
        try {
            while((line = r.readLine()) != null) {
                total.append(line);
                total.append(" ; ");
            }
            total.delete(total.length() - 3, total.length() -1); // Remove delimiter (;) at end
            r.close();
            ins.close();
        } catch(IOException e) {}

        try {
            Process proc = Runtime.getRuntime()
                .exec(new String[] {"sh", "-c", total.toString()});
            proc.waitFor();
        } catch (Exception ex) {
            ex.printStackTrace();
            Log.v("TAG", "exec failed");
        }

我使用仅包含行

的脚本文件测试了上述代码
cp /mnt/sdcard/Folder1/file /mnt/sdcard/Folder2/
cp /mnt/sdcard/Folder1/file1 /mnt/sdcard/Folder2/
res/raw中的

您必须从脚本中删除shebang和任何空行。另外,我相信你必须将每个命令包含在一行中,即你不能将命令分布在多行上。或者您可以编辑字符串构建部分以适合您的特定情况。

根本访问的完整解决方案

InputStream is =  getResources().openRawResource(getResources().getIdentifier("script_name_without_extension", "raw", getPackageName()));
boolean copysuccess = false;
// Copy from /res/raw/script.sh to /data/data/com.mycompany.myapp/files/script.sh
// because we need to chmod the script
File file = new File(getFilesDir(), "script.sh");
String scriptPath = file.getAbsolutePath();
if(!file.exists()) {
    try {
        OutputStream output = new FileOutputStream(file);
        byte[] buffer = new byte[4*1024];
        int read;
        while((read = is.read(buffer))!=-1){
            output.write(buffer,0, read);
        }
        copysuccess = true;
        output.flush();
        output.close();
        is.close();
    } catch(Exception e) {
        copysuccess = false;
        // TODO perform cleanup
    }

    // perform chmod now
    if(copysuccess) {
        try {
            Process proc = Runtime.getRuntime()
                .exec(new String[] {"su", "-c", "chmod 755 "+ scriptPath});
            proc.waitFor();
        } catch (Exception e) {
        }
    }
}

// Execute the script now
try {
    Process proc = Runtime.getRuntime()
        .exec(new String[] {scriptPath});
        proc.waitFor();
    } catch (Exception e) {
}

答案 1 :(得分:0)

首先 我认为bash scrip更好的地方是/raw目录。

但即使是原始目录也可能是错误的,因为我不确定您的应用是否可以访问此目录 - 应该有,但我不确定。

下一个有用的命令是: bash -x raw/script.sh