【问题标题】:Android app how to run .sh file from /res/script.shAndroid 应用程序如何从 /res/script.sh 运行 .sh 文件
【发布时间】:2015-08-20 15:19:51
【问题描述】:

我在 /res/ 目录下的 Android Studio 项目中有 bash 脚本。如何通过Runtime.getRuntime().exec(path);在我的android应用程序中运行这个文件?

编辑: 所有代码

 try {
            Process process = Runtime.getRuntime().exec("bash -x raw/plik.sh");

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            Log.d("lab", "Stat: " + output);
        }catch (Exception e){
            Log.d("lab", e.getMessage());
        }

我没有日志。

【问题讨论】:

  • 我已使用经过测试且有效的解决方案编辑了我的答案。让我知道它是否有效。如果答案有帮助,请投票。 :)

标签: android


【解决方案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) {
}

【讨论】:

  • @lukassz 我有另一种方法,我很确定它会起作用。如果您还没有得到答案,请告诉我。
  • @lukassz 我的解决方案适用于简单的脚本。不幸的是,我知道除了 root 访问之外别无他法。
  • @lukassz 如果您提供对应用程序的 root 访问权限,您将能够完全按照您的意愿进行操作。
  • @lukassz 我添加了具有 root 访问权限的完整解决方案。现在您可以执行任何普通的 bash 脚本。将需要 shebang 和所有这些时间。
  • @lukassz 如果您认为答案有帮助,请投票。
【解决方案2】:

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

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

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

【讨论】:

  • 是的,我错了。我有一个原始文件。但是这段代码Process process = Runtime.getRuntime().exec("bash -x raw/plik.sh"); 不起作用。
  • Runtime.getRuntime().exec() 方法返回 Process 当你有进程时,你可以获取输入和输出流并检查哪里有问题。请查看stackoverflow.com/questions/14932502/…
  • 请看我的问题。我想打印日志,但我什么也得不到。
猜你喜欢
  • 2014-02-03
  • 1970-01-01
  • 2011-01-28
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2016-04-22
相关资源
最近更新 更多