Skip to content

system

Execute a system command

Syntax: system x, system[x]

Where x is a string representing a system command and any parameters to it, executes the command and returns any result.

q)\l sp.q
…
q)\a                     / tables in namespace
`p`s`sp
q)count \a               / \ must be the first character
'\
q)system "a"             / same command called with system
`p`s`sp
q)count system "a"       / this returns a result
3

As with \, if the argument is not a q command, it is executed in the shell:

q)system "pwd"
"/home/guest/q"

Directing output to a file

When redirecting output to a file, for efficiency purposes, avoiding using > tmpout needlessly; append a semi-colon to the command.

q)system"cat x"

is essentially the same as the shell command

$ cat x > tmpout

as kdb+ tries to capture the output. So if you do

q)system"cat x > y"

under the covers that looks like

$ cat x > y > tmpout

Not good. So if you add the semicolon

q)system"cat x > y;"

the shell interpreter considers it as two statements

$ cat x > y; > tmpout

Capture stderr output

Can I capture the stderr output from the system call? Not directly, but a workaround is

/ force capture to a file, and cat the file
q)system"ls egg > file 2>&1;cat file"
"ls: egg: No such file or directory"        
/ try and fails to capture the text
q)@[system;"ls egg";{0N!"error - ",x;}]
ls: egg: No such file or directory
"error - os"

Changing working directory in Windows

In the event of an unexpected change to the working directory, Windows users please note http://blogs.msdn.com/b/oldnewthing/archive/2007/11/21/6447771.aspx