How to runCommand() of a bash script with redirected input from file in Mozilla Rhino?

StackOverflow https://stackoverflow.com/questions/18361933

  •  26-06-2022
  •  | 
  •  

문제

Is it possible to execute external bash script and set another file as input for it using Rhino? e.g. I need to rewrite bash script(exec.sh) with following content:

somescript.sh <fileInput.txt

I've tried many ways but without success:

  1. Reading fileInput.txt as input stream and passing to shell:

    var inputStream = new java.io.InputStream(fileInput.txt);
    runCommand( "somescript.sh", inputStream);

  2. Writing "somescript.sh <fileInput.txt" to additional bash script and calling runCommand():

    message = new FileUtils.writeStringToFile(helpfulScript, "somescript.sh
    runCommand("bash", helpfulScript.getCanonicalPath());

Sorry for pure highlight and thanks in advice for any ideas.

도움이 되었습니까?

해결책

You need to pass the input stream in as the input property of an object.

var inputStream = new java.io.FileInputStream("fileInput.txt");
runCommand("somescript.sh", { input: inputStream });

If input is not an InputStream it will be converted to a string and sent to the command directly. Similarly, you can add output and/or err properties to capture the command's standard output and standard error (documentation here).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top