Run shell command and return output as result of custom function

Mark picture Mark · Jun 7, 2010 · Viewed 7.8k times · Source

I would like to write a custom OpenOffice function that runs a shell command and puts the result into the cell from which it was invoked. I have a basic macro working, but I can't find a way to capture the command's output.

Function MyTest( c1 )
    MyTest = Shell("bash -c "" echo hello "" ")
End Function

The above always returns 0. Looking at the documentation of the Shell command, I don't think it actually returns STDOUT.

How would I capture the output so that I can return it in my function?

Answer

Kimball Robinson picture Kimball Robinson · Jul 14, 2010

Can you redirect the output to a temporary file, then read in the contents of that file with another command?

eg, Shell("bash -c "" echo hello > tmp.txt "" ")