java - 获取 Busybox 版本 : How to do?

标签 java android busybox

我想知道如何知道Busybox的版本。搜索互联网我发现这段代码:

public void busybox()throws IOException
    {
        /*
         * If the busybox process is created successfully,
         * then IOException won't be thrown. To get the 
         * busybox version, we must read the output of the command
         * 
         */

        TextView z = (TextView)findViewById(R.id.busyboxid);
        String line=null;char n[]=null;

        try
        {

        Process p =Runtime.getRuntime().exec("busybox");
        InputStream a = p.getInputStream();
        InputStreamReader read = new InputStreamReader(a);
        BufferedReader in = new BufferedReader(read);

        /*
         * Labeled while loop so that the while loop
         * can be directly broken from the nested for.
         * 
         */
        abc :while((line=in.readLine())!=null)
        {
            n=line.toCharArray();

            for(char c:n)
            {
                /*
                 * This nested for loop checks if 
                 * the read output contains a digit (number),
                 * because the expected output is -
                 * "BusyBox V1.xx". Just to make sure that
                 * the busybox version is read correctly. 
                 */
                if(Character.isDigit(c))
                {
                    break abc;//Once a digit is found, terminate both loops.

                }
            }

        }
        z.setText("BUSYBOX INSTALLED - " + line);
        }

但是返回的输出太详细了。我对细节不太感兴趣,只对版本感兴趣,例如 1.21.1。我该怎么办?

最佳答案

通过一个小技巧,您可以生成在开头包含 Busybox 版本的单行输出:

$ busybox | head -1
BusyBox v1.19.4-cm7 bionic (2012-02-04 22:27 +0100) multi-call binary

您发布的代码包含从那里解析版本所需的大部分内容。您只需在第一个和第二个空白字符处拆分该行。有点像

Process p = Runtime.getRuntime().exec("busybox | head -1");
InputStream a = p.getInputStream();
InputStreamReader read = new InputStreamReader(a);
String line = (new BufferedReader(read)).readLine();
String version = line.split("\\s+")[1];

关于java - 获取 Busybox 版本 : How to do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058655/

相关文章:

Android 通过 busybox 命令行静默安装 apk

linux - 从 busybox 挂载 ext2 作为完全只读的系统

java - 如何将 Angular 6 和 Spring MVC 与单个服务器集成?

java - 在鼠标上次所在的位置制作一个JLabel?

java.sql.SQLException : ORA-00928: missing SELECT keyword 异常

java - 如何从 JFrame 中的文本框接收输入?

iphone - 从 Photoshop 为 iphone 和 android 保存图片

android - 如何在运行时权限中获取 "Never ask Again"选项

android - 为什么不在我的自定义库中添加 support-v4 依赖项运行时

linux - Busybox ip show neighbors with MAC 地址