获取Busybox版本:怎么办?

时间:2013-09-27 19:29:19

标签: 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。我该怎么办?

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

运行root@android:/ # busybox

时,Busybox v1.21.1会生成以下输出
  

BusyBox v1.21.1(2013-07-08 10:20:03 CDT)多呼叫二进制文件   BusyBox在1998 - 2012年间受到许多作者的版权保护   根据GPLv2许可。有关详细信息,请参阅源分发   版权声明。
  [省略了其余的输出]


知道输出应该是什么样的,您可以使用正则表达式在String 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");
}