我有一个名为lightmeter.sh的bash脚本,用于创建/覆盖名为lightstuff.txt的文本文档。这是代码:
#!/bin/bash
gphoto2 --get-config=lightmeter 1> lightstuff.txt
我已经开始编写处理脚本来执行bash脚本:
void setup() {
String[] args = {"sh","","/Users/lorenzimmer/Documents/RC/Camera_control/first_scripts/lightmeter.sh"};
exec(args);
}
当我运行程序时,脚本不会执行,或者它不会像从终端运行时那样更新文本文件。我做错了什么?
谢谢,
洛伦
答案 0 :(得分:0)
以下是我在Processing(a more complete version is here)中运行Bash脚本的示例:
// required imports that aren't loaded by default
import java.io.BufferedReader;
import java.io.InputStreamReader;
void setup() {
String commandToRun = "./yourBashScript.sh";
// where to do it - should be full path
File workingDir = new File(sketchPath(""));
// run the script!
String returnedValues;
try {
Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir);
int i = p.waitFor();
if (i == 0) {
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ( (returnedValues = stdInput.readLine ()) != null) {
println(returnedValues);
}
}
// if there are any error messages but we can still get an output, they print here
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ( (returnedValues = stdErr.readLine ()) != null) {
println(returnedValues);
}
}
}
// if there is any other error, let us know
catch (Exception e) {
println("Error running command!");
println(e);
// e.printStackTrace(); // a more verbose debug, if needed
}
}