0

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.

4
  • You cannot change parent process' environment variables from child process. Commented Dec 3, 2013 at 9:37
  • But I'm creating A and CUDA_VISIBLES_DEVICES into the TCL script as new environment variables... I think. Commented Dec 3, 2013 at 9:48
  • New is just one form of change. But maybe i misunderstanding what your 'export' should mean. Please clarify. Commented Dec 3, 2013 at 10:26
  • In your sample you execute a shell subprocess and set the A environment variable to hold the result of the pipeline. However, once you return to Tcl, the shell subprocess has terminated and all its variables are lost. There is no magic connection between the variables defined in Tcl and those with the same name in the child process. Instead you will need to capture the output of the pipeline and parse that in Tcl. @bmk provides one way to achieve this. Commented Dec 3, 2013 at 14:42

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The exec manual page is a must-read for Daniel.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.