gpt4 book ai didi

java - 获取 Busybox 版本 : How to do?

转载 作者:行者123 更新时间:2023-11-30 03:21:29 27 4
gpt4 key购买 nike

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

最佳答案

通过一个小技巧,您可以生成在开头包含 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];

关于java - 获取 Busybox 版本 : How to do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com