运行 bash 来自 Processing 的脚本
Running bash script from Processing
我有一个名为 lightmeter.sh 的 bash 脚本,creates/overwrites 一个名为 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);
}
当我 运行 脚本不执行程序时,或者它不会像我从终端 运行 时那样更新文本文件。我做错了什么?
谢谢,
洛伦
这是我如何 运行 Bash 脚本处理 (a more complete version is here) 的示例:
// 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
}
}
我有一个名为 lightmeter.sh 的 bash 脚本,creates/overwrites 一个名为 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);
}
当我 运行 脚本不执行程序时,或者它不会像我从终端 运行 时那样更新文本文件。我做错了什么?
谢谢,
洛伦
这是我如何 运行 Bash 脚本处理 (a more complete version is here) 的示例:
// 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
}
}