1

My TCL script puts together a call to a command line tool using TCL variables. I've tried exec or eval for the command line but nothing worked.

#!/usr/bin/tclsh
set dbg 0
set iso 100
set cmd "gphoto2 --set-config-value /main/imgsettings/iso=${iso}"
if {$dbg} {puts $cmd} else {eval $cmd}

Gives :

invalid command name "gphoto2"
while executing
"gphoto2 --set-config-value /main/imgsettings/iso=100"
("eval" body line 1)
invoked from within
"eval $cmd"
invoked from within
"if {$dbg} {puts $cmd} else {eval $cmd}"
(file "./canon.tcl" line 22)

If tried { exec $cmd } but that did not work either.

couldn't execute "gphoto2 --set-config-value /main/imgsettings/iso=100": no such file or directory
while executing
"exec $cmd"
invoked from within
"if {$dbg} {puts $cmd} else {exec $cmd}"
(file "./mofi_canon.tcl" line 22)

I tried giving absolute path name /usr/bin/gphoto2 but again no success. I guess the problem has to do with the entire command string being one object and the unix execution cannot parse it. But what's the way to give it to the shell properly?

from the linux command line of course the command works fine.

@ubuntu:~/TCL$ gphoto2 --summary                                        
Camera summary:                                                                
Manufacturer: Canon Inc.
Model: Canon EOS DIGITAL REBEL XSi

This is from a kubuntu 14.04 linux.

uname -a
Linux ubuntu 3.13.0-36-generic #63-Ubuntu SMP Wed Sep 3 21:30:07 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Thanks & Cheers, Gert

3
  • what output do you get if you were to execute which gphoto2. Commented Sep 28, 2014 at 4:52
  • I think you need to separate the $cmd string into an array of separate strings, one per argument, and then use exec. The error message indicates it couldn't find a single command called ghphoto2 --set-config-value …. You could use "sh" and "-c" and the current value of $cmd too: else { exec "sh" "-c" $cmd } or thereabouts — my Tcl/Tk is rusty at best. Commented Sep 28, 2014 at 5:06
  • @Red Cricket: Which gphoto2 gives /usr/bin/gphoto2. /usr/bin is also part of the shell $path. Gert Commented Sep 28, 2014 at 7:15

2 Answers 2

5

If you are using tcl 8.5 or higher version, then you can use as follows,

exec {*}$cmd

Else, use the below code

eval exec $cmd

Let's consider an example as such

example.tcl

#usr/bin/tclsh
puts $argc
puts [ lindex $argv 0 ]
puts [ lindex $argv 1 ]

execargs.tcl

#usr/bin/tclsh

#Calling the example.tcl file with the command line args
set cmd "tclsh example.tcl 1 2"

#For Tcl versions less than 8.5, this will work
puts [ eval exec $cmd ] 

#For Tcl 8.5 or higher, this will work
puts [ exec {*}$cmd ] 
Sign up to request clarification or add additional context in comments.

2 Comments

All Tcl programmers are strongly advised to use {*} instead of eval if at all possible; it definitely doesn't have nasty landmine surprises in it.
eval [linsert $cmd 0 exec] -- I hate that. Hard to read cmd after argument. Better use exec {*}$cmd. But that is still better than eval exec $cmd which has some surprises...
1

I haven't used tcl in a long while, but I suspect the problem is you are combining the command into one variable.

Try executing

exec gphoto2 --set-config-value /main/imgsettings/iso=100

in tclsh. That should work.

If you combine the entire command into one var, you are trying to find an executable of that name.

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.