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 のバージョンを含む 1 行の出力を生成できます。

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

投稿したコードには、そこからバージョンを解析するために必要なもののほとんどが含まれています。最初と 2 番目の空白文字で行を分割するだけです。何かのようなもの

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変数内のパターンを検索できます。Java で正規表現を使用する方法の詳細については、PatternおよびMatcher 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 に答える