Run a shell command and return the result as a result of a user-defined function

I would like to write a custom OpenOffice function that runs a shell command and places the result in the cell from which it was called. I have a basic macro, but I cannot find a way to capture the output of the command.

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

The above always returns 0. If you look at the documentation of the command Shell, I don’t think it really returns STDOUT.

How can I write the output so that I can return it to my function?

+3
source share
2 answers

, ?

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

+1

, . ( ), (, /tmp, ).

:

Dim myCommand As String
myCommand = "echo hello world"
Shell ("bash", 1, "-c "" " & myCommand & " | xclip -selection clipboard"" ", true)
' setting the 4th parameter to true ensures that the Shell function will wait until '
' the command terminates before returning '

' then paste the content of the clipboard '
Dim dh As Object
dh = createUnoService("com.sun.star.frame.DispatchHelper")
dh.executeDispatch(StarDesktop.CurrentFrame, ".uno:Paste", "", 0, Array())

.

, , " " ( , ).

+1

Source: https://habr.com/ru/post/1748793/


All Articles