Joan Cruz · Mar 14, 2017

How to get result of $ZF(-1, "commmand") into a variable


I'd like to know if there is an easy way to get result from a $ZF(-1) into a varaible to use it in HS code.


0 1,156
Discussion (9)2
Log in or sign up to continue

$zf(-1) can only return status code from called process

To get some output result, you should redirect this output to some file, and read result from this file after executing.

set status=$zf(-1, "find /tmp -name *.tmp > /tmp/result.log")
set fs=##class(%Stream.FileCharacter).%New()
set fs.Filename="/tmp/result.log"
set result=fs.Read()

Be aware that Herman's code relies on the process having the non-default SetZEOF setting. More info here.

If you are trying to get the output of the command, one way is to use a pipe device. See this post.

I have used this in order to call python code in the server, it calls bcrypt encryption in python.

  Set password = "password"
  Set tSC = ##class(EnsLib.JavaGateway.Service).RunCommandViaCPIPE("python -c 'import bcrypt; print(bcrypt.hashpw("_$c(34)_password_$c(34)_", bcrypt.gensalt(10)))'",.pDevice,.pOutput)
  Write pOutput,!

Like most DC posts nowadays, this one got auto-crossposted to the intersystems-public-cache Google Group. When I checked this morning there were 5 responses from people trying to help the original poster. But since those answers don't automatically feed across to DC I'm drawing attention to them here. Because unless the OP knows to look there they may never see them.!topic/intersystems-public-cache/fngd5j...

using a Command Pipe would be the way to achieve this:

Set Command="git status"
Set Pipe="|CPIPE|"
Open Pipe:(Command:"R"):0
If '$Test Return "Cannot open CommandPipe to "_Command
For  {
    Use Pipe Read Line If $ZEOF Quit
    Set Lines($Increment(Lines))=$ZStrip(Line,"<>W")
Close Pipe