· Jun 26, 2017

How to execute external unix/windows commands from COS

I would like  to invoke some unix/windows commands from COS, any API could do that?

Also, I would like to set up  some Environment Variables for this process which would execute  the above external command.

Anyway to achieve that?

Thanks for your help.

Discussion (5)0
Log in or sign up to continue

Joining this question.

It would also be nice if someone could describe how to catch and process I/O at this point. Ideally I am looking for something like:

set pseudocode = ##class(Any.Util).Execute("cmd") // let's say on Windows
do pseudocode.Write("ipconfig")
// loop may go here
set data = pseudocode.Read()
// loop may close here
// ... and the data is the stdout of the process!



At a low level, there are two options:

Pipes / command pipes - useful if you want to be able to read command output OR write to the command. Unfortunately, you have to pick one or the other. To answer Nikita's question - maybe there's some tricky way around this by building a command that redirects stdout to a file, which is read using a separate device?

$zf(-1) $zf(-2) - these are a little bit easier to use. $zf(-1) spawns a child process and waits for it to return; $zf(-2) doesn't wait.

Needless to say, if you're building these into an application, make sure that you're careful about building user input into the commands that you execute. wink