简体   繁体   中英

Running bash script from Processing

I have a bash script called lightmeter.sh that creates/overwrites a text document called 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:

    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 ):

// 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
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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