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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho