java通過Runtime執行linux命令帶管道符失效

 String command1 = "hadoop fs -du "+str +" |awk ' { SUM += $1 } END { print SUM/(1024*1024) }'" ;

    String[] command = { "/bin/sh", "-c", command1 };

    try {
        Process exec = Runtime.getRuntime().exec(command);
        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            //執行結果加上回車
            sb.append(line);
        }
        String result1 = sb.toString();
        node.put("storageSpace",result1+"MB");
        System.out.println(result);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
以上述方式執行即可

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章