Dựa trên ví dụ của @ Tim để tạo ra một phương pháp khép kín:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Shell {
/** Returns null if it failed for some reason.
*/
public static ArrayList<String> command(final String cmdline,
final String directory) {
try {
Process process =
new ProcessBuilder(new String[] {"bash", "-c", cmdline})
.redirectErrorStream(true)
.directory(new File(directory))
.start();
ArrayList<String> output = new ArrayList<String>();
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
while ( (line = br.readLine()) != null )
output.add(line);
//There should really be a timeout here.
if (0 != process.waitFor())
return null;
return output;
} catch (Exception e) {
//Warning: doing this is no good in high quality applications.
//Instead, present appropriate error messages to the user.
//But it's perfectly fine for prototyping.
return null;
}
}
public static void main(String[] args) {
test("which bash");
test("find . -type f -printf '%T@\\\\t%p\\\\n' "
+ "| sort -n | cut -f 2- | "
+ "sed -e 's/ /\\\\\\\\ /g' | xargs ls -halt");
}
static void test(String cmdline) {
ArrayList<String> output = command(cmdline, ".");
if (null == output)
System.out.println("\n\n\t\tCOMMAND FAILED: " + cmdline);
else
for (String line : output)
System.out.println(line);
}
}
(Ví dụ kiểm tra là một lệnh liệt kê tất cả các tệp trong một thư mục và các thư mục con của nó, một cách đệ quy, theo thứ tự thời gian .)
Nhân tiện, nếu ai đó có thể cho tôi biết tại sao tôi cần bốn và tám dấu gạch chéo ngược ở đó, thay vì hai và bốn, tôi có thể học được điều gì đó. Có một mức độ không thoát nữa đang xảy ra hơn những gì tôi đang kể.
Chỉnh sửa: Chỉ cần thử cùng một mã này trên Linux, và hóa ra là tôi cần một nửa số dấu gạch chéo ngược trong lệnh thử nghiệm! (Đó là: con số dự kiến là hai và bốn.) Giờ đây, nó không còn kỳ lạ nữa, đó là một vấn đề về tính di động.
cat
vàcsh
không liên quan gì đến nhau.