【发布时间】:2020-09-30 12:13:54
【问题描述】:
我正在尝试从 shell 命令解析 java 中的输出:(可以从 apt 安装 sox)
sox inputfile.flac -n stat
但我做不到,搜索了几个小时,一无所获。
代码:
Process p = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
我尝试了命令:
String command1 = "sox inputfile.flac -n stat"; //no output
String command2 = "bash -c sox inputfile.flac -n stat"; //output of 'sox' without arguments
String command3 = "sh -c sox inputfile.flac -n stat"; //output of 'sox' without arguments
String command4 = "sh -c \"sox inputfile.flac -n stat\""; //error
String command5 = "bash -c \"sox inputfile.flac -n stat\""; //error
根据文档 od 'sh':
-c 选项使命令从字符串操作数中读取- 而不是来自标准输入。请记住,此选项仅适用于 接受单个字符串作为其参数,因此必须是多字字符串 引用。
所以我尝试了“command4”/“command5” -> 错误代码 2。 但是,我检查了普通的 linux 终端并且它工作正常:
sh -c "sox inputfile.flac -n stat"
bash -c "sox inputfile.flac -n stat"
sox inputfile.flac -n stat
我错过了什么?
【问题讨论】: