Daniel Ruiz Molina
Daniel Ruiz Molina

Reputation: 1

Running an external program from a Tcl script

I would like to export a variable depending on result of a binary command. My TCL script is this:

set A ""

exec sh -c "export A=\"`/usr/local/cuda/samples/1_Utilities/deviceQuery/deviceQuery -noprompt | grep ^Device | wc -l`\""

puts $A

if { $A == "1" } {
    set CUDA_VISIBLES_DEVICES 0
} else {
    set CUDA_VISIBLES_DEVICES 1
}

With this script, when I execute puts $A I don't get anything in terminal... so in if command I don't know what I evaluating... My "export" must return ONLY 1 or 0...

Sorry about my poor TCL level.

Thanks.

Upvotes: 0

Views: 2737

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385890

The problem is that your exec'd command runs in its own process, so when you set a variable A, that A only exists as a shell variable for the life of that process. When the process exits, A goes away.

The exec command returns the stdout of the command that was exec'd. If you want the result of the command to be in a Tcl variable, you need to set the variable to be the result of the exec:

set A [exec ...]

For more information on the exec command, see the exec man page.

Upvotes: 0

bmk
bmk

Reputation: 14137

I guess what you want is something like this:

set a [exec /usr/local/cuda/samples/1_Utilities/deviceQuery/deviceQuery -noprompt | grep ^Device | wc -l]

You set variable a in TCL context and assign the command's return value (i.e. the output text) to it.

Upvotes: 2

Related Questions