Forum OpenACS Q&A: Tcl exec args question

Collapse
Posted by David Cotter on
Subject: Tcl exec args question

I wish to write my own exec procedure in tcl for a workaround to a problem on FreeBSD
My problem is that I write a procedure like:

proc my_exec (prog_name args) { exec $progname $args }

and all the args are places in one argument to the program as though quotes were put around them on the Unix command line so they all get intrepeted as the first argument to the program.

e.g. dc_exec ls david.txt test.txt will be execed as exec ls "david.txt test.txt" and there is no file with that name.

I simple don't know how do prevent this happening in tcl - any help would be appreciated.

Collapse
Posted by David Cotter on
I finally got it to work:


proc my_exec {prog_name args} {
set command "exec $prog_name"
foreach var $args {
append command " $var"
}
eval $command
}

Collapse
Posted by Vadim Nasardinov on

That'll work except for the somewhat obscure case where one of arguments is a string consisting entirely of whitespace. You'll lose it when you build up you command variable. This may work better:

proc my_exec {prog_name args} {
    eval [concat exec $prog_name $args]
}

# test
puts [my_exec ls index.html index-all.html "" ]