cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

running command line executable and capturing stdout

jsulak
1-Visitor

running command line executable and capturing stdout

Hey everybody,

Is there a way from either ACL or Javascript to run a command line executable and capture the output from stdout? The ACL function system() just returns a success or failure status code. (I'm running Windows.)

For a broader picture, I want to do some HTTP PUT and DELETEs using curl, and capture the resulting status code that is output to stdout. So if there's a reasonably sane way to do low-level HTTP stuff more natively in Arbortext, I'm open to that too.

Thanks,

James
6 REPLIES 6

Hi James-



The basic pattern for doing this is to redirect stdout to a file when
you run the command, and then read in the resulting text file and parse
it as needed. I developed a utility function to do this a while back,
here's the gist of it:



function windows_command(command) {

# executes the given command and returns any text sent to stdout

result = system("cmd /C " . command . " > $home\epicoutput.txt",1);

if (result != 0) { return "*** command failed ***" }

else {

$output = ";

$outputfile = open("$home\epicoutput.txt");

while (getline($outputfile,$line)!=0) {

$output = $output . " " . $line;

}

close($outputfile);

system("cmd /C del $home\epicoutput.txt",1);

return $output;

}

}



HTH



--Clay



Clay Helberg

Senior Consultant

TerraXML


jsulak
1-Visitor
(To:jsulak)

Thanks, everyone. I was hoping to avoid the write to a file / read from a file loop, but I guess that's the only way to do it.

-James

If you *really* object to writing/reading a file, you might be able to
set something up using Java where you redirect System.out to a
java.io.StringWriter or something similar and use Runtime.exec(). But to
be honest that seems like a lot more trouble than it's worth, unless
there's some kind of high cost involved with writing and reading from
the file system.



--Clay



Clay Helberg

Senior Consultant

TerraXML


OK, so it was more trouble than it was worth, except that my curiosity
got the better of me. Here's some JavaScript code that uses Java objects
to get the job done without writing to/reading from any disk files:



var rt = Packages.java.lang.Runtime.getRuntime();

var pid = rt.exec("cmd /C dir C:\\Temp");

var is = pid.getInputStream();

var isr = Packages.java.io.InputStreamReader(is);

var br = Packages.java.io.BufferedReader(isr);

var line = br.readLine();

var output = ";

while (line != null) {

output += line + "\n";

line = br.readLine();

}

Application.print(output);



This is just static code to run a test case, but it works. Conversion to
a general function is left as an exercise.



Of course, you could also write it in pure Java if you prefer that, or
maybe even ACL using the various java_*() functions.



--Clay



Clay Helberg

Senior Consultant

TerraXML


Very cool, thanks for sharing Clay!


jsulak
1-Visitor
(To:jsulak)

Very nice, Clay. Just be careful crossing the street...
Announcements

Top Tags