Android shell 脚本

2018-09-03

cat file1 > file2    //读取/复制file1为file2

echo "string" > file    //覆盖写入string到file

echo "string" >> file    //追加写入string到file

//注意引号里面的内容string要replaceAll("\\n", "\\\\n"),replaceAll("\"", "\\\\\"");

下面是一个执行多条命令的方法

public static CommandResultexecCommand(String[] commands, boolean isRoot) {

        CommandResult commandResult =new CommandResult();

        if (commands ==null || commands.length ==0)return commandResult;

        Process process =null;

        DataOutputStream os =null;

        BufferedReader successResult =null;

        BufferedReader errorResult =null;

        StringBuilder successMsg =null;

        StringBuilder errorMsg =null;

        try {

            process = Runtime.getRuntime().exec(isRoot ?COMMAND_SU :COMMAND_SH);

            os =new DataOutputStream(process.getOutputStream());

            for (String command : commands) {

                if (command !=null) {

                os.write(command.getBytes());

                    os.writeBytes(COMMAND_LINE_END);

                    os.flush();

                }

        }

        os.writeBytes(COMMAND_EXIT);

            os.flush();

            commandResult.result = process.waitFor();

            //获取错误信息

            successMsg =new StringBuilder();

            errorMsg =new StringBuilder();

            successResult =new BufferedReader(new InputStreamReader(process.getInputStream()));

            errorResult =new BufferedReader(new InputStreamReader(process.getErrorStream()));

            String s;

            while ((s = successResult.readLine()) !=null) successMsg.append(s);

            while ((s = errorResult.readLine()) !=null) errorMsg.append(s);

            commandResult.successMsg = successMsg.toString();

            commandResult.errorMsg = errorMsg.toString();

        }catch (IOException e) {

            String errmsg = e.getMessage();

            if (errmsg !=null) {

                Log.e(TAG, errmsg);

            }else {

                e.printStackTrace();

            }

        }catch (Exception e) {

            String errmsg = e.getMessage();

            if (errmsg !=null) {

                Log.e(TAG, errmsg);

            }else {

                e.printStackTrace();

            }

        }finally {

        try {

                if (os !=null) os.close();

                if (successResult !=null) successResult.close();

                if (errorResult !=null) errorResult.close();

            }catch (IOException e) {

                String errmsg = e.getMessage();

                if (errmsg !=null) {

                    Log.e(TAG, errmsg);

                }else {

                    e.printStackTrace();

                }

        }

        if (process !=null) process.destroy();

     }

     return commandResult;

}

修改Packages.xml文件权限

String path ="/storage/emulated/0/temp.xml";

CommandExecution.execCommand("cat /data/system/packages.xml > " + path, true);

File file =new File(path);

if (!file.exists()) {

    Log.d("File", path +"不存在");

}else {

    StringBuffer sb =new StringBuffer();

    try {

        InputStream is =new FileInputStream(file);

        if (is !=null) {

            InputStreamReader isr =new InputStreamReader(is);

            BufferedReader br =new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) !=null) {

                sb.append(line);

                sb.append("\n");

            }

            CommandExecution.execCommand("echo \"" +

            sb.toString().replaceAll("granted=\"false\"", "granted=\"true\"")

                                .replaceAll("\\n", "\\\\n")

                                .replaceAll("\"", "\\\\\"") +

                                "\" > /data/system/packages.xml", true);

            Log.i("setPermission", "Success");

        }

}catch (FileNotFoundException e) {

        Log.d("FileNotFoundException", file.getAbsolutePath() +" doesn't found\n" + e.getMessage());

    }catch (IOException e) {

        Log.d("File", "read exception: " + e.getMessage());

    }

}

你可能感兴趣的:(Android shell 脚本)