Pregunta

I would like to know how do I know the version of Busybox. Searching the internet I found this code:

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);
        }

But returns a outpout too detailed. I am interested less detail, only the version, for example, 1.21.1. How can I do?

¿Fue útil?

Solución

With a little trick, you can produce a single line output that contains the Busybox version at the beginning:

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

The code you posted contains most of what is required to parse the version from there. You just have to split the line at the first and second whitespace character. Something like

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];

Otros consejos

Busybox v1.21.1 produces the following output when you run root@android:/ # busybox

BusyBox v1.21.1 (2013-07-08 10:20:03 CDT) multi-call binary.
BusyBox is copyrighted by many authors between 1998-2012.
Licensed under GPLv2. See source distribution for detailed
copyright notices.
[rest of the output omitted]


Knowing what the output should look like you can use a regular expression to search for a pattern within the String line variable. Checkout the Pattern and Matcher java classes for more details on using regular expressions in Java.

Remove the line z.setText("BUSYBOX INSTALLED - " + line); and replace it with the below code block. This will set the TextView's content to 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");
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top