簡體   English   中英

從Processing運行bash腳本

[英]Running bash script from Processing

我有一個名為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); 
}

當我運行程序時,腳本不會執行,或者不會像從終端運行腳本那樣更新文本文件。 我究竟做錯了什么?

謝謝,

洛倫

這是我如何在處理中運行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