Another day, another bit of tinkering with some of my old Python scripts to try and get my head round Nim.
This is an old script I had which takes a list of [IMHO] junk processes and kills them on my OSX laptop. [For the purposes of testing I'm just using Finder and Dock as placeholders, as they'll relaunch automatically after killing, which speeds up testing]
Assuming execProcess is the way to go, I've come up with:
#import
import osproc
#processes to kill
var processes = ["Finder","Dock"]
for process in processes:
execProcess("pkill", args=[" -9", process])
but, when I run that I get:
Error: expression 'execProcess("pkill", [" -9", process], nil,
{poStdErrToStdOut, poUsePath, poEvalCommand})' is of type 'TaintedString' and has to be discarded
"TaintedString" is a new one on me. But I'm assuming it's the usual newbie error when trying out a strongly typed language and I'm somehow mixing types in the command I'm passing to execProcess. So I tried with:
execProcess("pkill", args=[" -9", $process])
instead. But still got the same error.
The example in the docs has the args to the command 'hard-wired':
let outp = execProcess("nim", args=["c", "-r", "mytestfile.nim"], options={poUsePath})
let outp_shell = execProcess("nim c -r mytestfile.nim")
So, how do I pass args in as a variable?
You're missing the important part in that example:
let outp =
The way you're doing it is correct, but execProcess returns a TaintedString (doc), so you'd have to capture it or explicitly discard the result with discard like this:
# default options contains poEvalCommand, which will ignore args, so we specify our own set
discard execProcess("pkill", args=[" -9", process], options = {poStdErrToStdOut, poUsePath})
Unlike most languages, Nim does not allow you to drop a function return value implicitly.
Aha. That worked a treat
I'd thought the TaintedString error was telling me I was creating a "tainted string" when building the command. And the reason I'd omitted the let outp = was that it looked like that was assigning the result of the command to a varible so it could be used later on –and I just wanted it to run & exit.
Thanks for your help.