Skip to content

Instantly share code, notes, and snippets.

@rianoc
Created December 7, 2021 10:20
Show Gist options
  • Save rianoc/433911b653f69a8580612e5d2f970857 to your computer and use it in GitHub Desktop.
Save rianoc/433911b653f69a8580612e5d2f970857 to your computer and use it in GitHub Desktop.
Kdb+ minimise use of '/tmp' during 'system' calls

In Kdb+ you cannot control the use of '/tmp' during a system call. You may need to avoid this if the drive space is small.

For familiarity use the TMPDIR environment variable:

q)setenv[`TMPDIR] "/my/chosen/path" 

Function to run system commands:

systemTMPDIR:{[c] 
  f:first system"mktemp"; //Make a temp file respecting TMPDIR
  c:c," > ",f," 2>&1;echo $?"; //Add redirect to tmp file and capture of exit code
  e:"J"$first system c; //Execute the command
  f:hsym `$f; 
  r:read0 f; //Read the result of the command 
  hdel f; //Delete the tmp file
  $[not 0=e; //Check if the exit code was an error (not 0)
    [-1 last r;'`os]; //If an error print the last line and signal with 'os
    r] //If success return the data 
}

On success:

q)systemTMPDIR"ls"
"file1"
"file2"

On failure:

q)systemTMPDIR"blah"
sh: 1: blah: not found
'os
[0] systemTMPDIR"blah"
^

*Note: This is just a small example and likely will not behave the exact same as the native 'system' in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment