【问题标题】:Get Busybox version: How to do?获取 Busybox 版本:怎么办?
【发布时间】:2013-09-27 19:29:19
【问题描述】:

我想知道如何知道 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。我该怎么办?

【问题讨论】:

    标签: java android busybox


    【解决方案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];
    

    【讨论】:

      【解决方案2】:

      Busybox v1.21.1 在您运行root@android:/ # busybox 时会产生以下输出

      BusyBox v1.21.1 (2013-07-08 10:20:03 CDT) 多调用二进制。
      BusyBox 在 1998 年至 2012 年期间由许多作者拥有版权。
      根据 GPLv2 许可。详见源分布
      版权声明。
      [省略其余输出]


      知道输出应该是什么样子,您可以使用正则表达式在字符串line 变量中搜索模式。查看PatternMatcher java 类以获取有关在 Java 中使用正则表达式的更多详细信息。

      删除z.setText("BUSYBOX INSTALLED - " + line); 行并将其替换为以下代码块。这会将 TextView 的内容设置为 1.21.1

      Pattern versionPattern = Pattern.compile("(?:(\\d+)\\.)?(?:(\\d+)\\.)?(\\*|\\d+)");
      Matcher matcher = versionPattern.matcher(line);
      if (matcher.find()){
            z.setText("BUSYBOX VERSION: "+matcher.group());
      } else {
            z.setText("BUSYBOX INSTALLED - Not Found");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-16
        • 1970-01-01
        • 1970-01-01
        • 2013-08-01
        • 2014-05-11
        • 1970-01-01
        • 2018-03-23
        相关资源
        最近更新 更多