Gert Gottschalk
Gert Gottschalk

Reputation: 1716

Call a unix program from a TCL variable

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

Upvotes: 1

Views: 489

Answers (2)

Dinesh
Dinesh

Reputation: 16428

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 ] 

Upvotes: 5

jcoppens
jcoppens

Reputation: 5440

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.

Upvotes: 1

Related Questions