This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
This forum has helped me so much in the past that I thought I would post something that might help someone else.
In the course of writing automation scripts for use under DSS, I had the need to execute a bash shell script from within my javascript code. I went nuts trying to find something on the web that would do this, but only found a few examples that were not completely what I wanted. The following javascript function is intended to run under the rhino javascript interpreter that is used with DSS. It is expecting to be invoked from a Windows environment that is running cygwin. It takes a single character string that is a command to be executed as though it were typed into a bash shell command prompt. It returns as the function's value a character string that contains the text written to the shell's stdout stream. The shell's exit code is made available in the global variable bash_exit_code. So here is the function:
// Define a function to execute shell commands on the PC host and return that shell's stdout
var bash_exit_code = 0; // global to provide exit code from bash shell invocation
function bash(command)
{
var c; // a character of the shell's stdout stream
var retval = ""; // the return value is the stdout of the shell
var rt = Runtime.getRuntime(); // get current runTime object
var shell = rt.exec("bash -c '" + command + "'"); // start the shell
var shellIn = shell.getInputStream(); // this captures the output from the command
while ((c = shellIn.read()) != -1) // loop to capture shell's stdout
{
retval += String.fromCharCode(c); // one character at a time
}
bash_exit_code = shell.waitFor(); // wait for the shell to finish and get the return code
shellIn.close(); // close the shell's output stream
return retval;
}
Hopefully, this will save someone else some time. Enjoy!
Robert,
Thank you for sending such detailed and nicely commented code; I imagine this can be easily extended to *nix systems as well, thus it is incredibly useful.
Cheers,
Rafael
I expect that it will run on Linux without modification; I just have not tested it.