2013-09-27 1 views
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과 같은 버전이다. 어떻게해야합니까?

답변

1

, 당신은 처음에 비지 박스 버전을 포함하는 하나의 라인 출력을 생성 할 수 있습니다 :

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

비지 박스의 v1.21.1는 [email protected]:/ # busybox

비지 박스의 v1.21.1 (2013년 7월 8일 중부 서머 타임 10시 20분 3초) 다중 호출 바이너리를 실행하면 다음과 같은 출력을 생성합니다.
BusyBox는 1998-2012 년 사이 많은 작가의 저작권으로 보호됩니다.
GPLv2에서 라이센스가 부여됩니다. 자세한 내용은 소스 배포판을 참조하십시오.
저작권 고지. 당신이 문자열 line 변수 내의 패턴을 검색하는 정규 표현식을 사용할 수 있습니다처럼 출력이 보일 것입니다 무엇을 알고
[생략 출력의 나머지]


. Java에서 정규식 사용에 대한 자세한 내용은 PatternMatcher 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"); 
}