简体   繁体   English

从Processing运行bash脚本

[英]Running bash script from Processing

I have a bash script called lightmeter.sh that creates/overwrites a text document called lightstuff.txt. 我有一个名为lightmeter.sh的bash脚本,该脚本创建/覆盖了一个名为lightstuff.txt的文本文档。 Here is the code for that: 这是该代码:

#!/bin/bash          
      gphoto2 --get-config=lightmeter 1> lightstuff.txt

I've started to write a processing script to execute the bash script: 我已经开始编写处理脚本来执行bash脚本:

    void setup() {


       String[] args = {"sh","","/Users/lorenzimmer/Documents/RC/Camera_control/first_scripts/lightmeter.sh"};
exec(args); 
}

When I run the program the script does not execute, or it doesn't update the text file like it does when I run it from the terminal. 当我运行程序时,脚本不会执行,或者不会像从终端运行脚本那样更新文本文件。 What am I doing wrong? 我究竟做错了什么?

Thanks, 谢谢,

Loren 洛伦

Here's an example of how I run Bash scripts in 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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM