Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

redirect the output of a tcl command to a variable

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007
Hi,

I have written this code to redirect the output of a tcl command to a variable . But it is not working.

Facing problem with:
"interp alias {} puts {} ::forward::r_puts"

Code:
---------
rename puts t_puts
interp alias {} puts {} t_puts

namespace eval forward {
}

proc forward::r_puts { args} {
 variable result
       append result [lindex $args 1]
}

proc forward {{args} args1} {
set tee 0
set append 0
set file_name "[lindex $args 0]"
set script "$args1"
   set prev_alias [interp alias {} puts]
   interp alias {} puts {} ::forward::r_puts
   set ::forward::result {}

 catch {uplevel $script} rsl
 eval {interp alias {} puts {}} $prev_alias

   upvar $file_name my_variable

     set my_variable $::forward::result

 return $rsl
}


Command to execute
---------------------------
tcl>forward {out_variable} {ls}

Expected output :
-------------------------
tcl> puts $out_variable
The data in the variable should be the stdout of the command "ls"
Note: "ls" is a tcl command .

Posted by admin (Graham Ellis), 8 October 2007
I think you need something as short as

set varname [billy param1 param2 etc]

In this example, a variable named "varname" is set to the result of running the billy command with three parameters - param1, param2 and etc

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007
Hi,

I could not able to find this command it says that command not found.

tcl>billy ls
Error: invalid command name "billy"


regards
Deep


Posted by admin (Graham Ellis), 8 October 2007
Yes, it would - billy is an example - in other words it's put in there to illustrate the syntax you would use.  I don't know what command you want to run - you will need to use its name in place of billy.

Posted by deepshikha_dutta (deepshikha_dutta), 8 October 2007
Hi,

I need to run command "ls" .

set var [ls]  

puts $var is still giving empty string as it should not be.

B.T.W
--------
Please let me know how this will work.
-------------------------------------------
rename puts tcl_puts
interp alias {} puts {} tcl_puts

namespace eval redirect {
proc var_puts { args} {
tcl_puts "$args"
}
}

proc redirect {args} {
   set prev_alias [interp alias {} puts]
 interp alias {} puts {} ::redirect::var_puts

uplevel $args
 eval {interp alias {} puts {}} $prev_alias

}
---------------------------------------------------------

Note: I want to send the output of "uplevel $script" as argument to  "::redirect::var_puts "

Note: $args in uplevel can be any command.

Regards
Deep

Posted by admin (Graham Ellis), 8 October 2007
I really worry about the code and direction you're headed - there has to be an easier way.  Let's start by establishing why you need to use "ls" - which will work intergctive at the command line but not with ina script as it works via the unknown interface.    It looks to me like you may be trying to get a list of all the file names in a diectory.   In which case, what is wrong with

set water_biscuit [glob *]



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho