1

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

4

2 回答 2

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];
于 2013-09-27T22:27:41.583 回答
0

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

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


知道输出应该是什么样子,您可以使用正则表达式在 Stringline变量中搜索模式。查看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");
}
于 2013-09-27T21:18:52.783 回答