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