18

Say I have a TCL script like this:

exec ls -l 

Now this will print out the content of current directory. I need to take that output as a string and parse it. How I can do this?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Narek
  • 38,779
  • 79
  • 233
  • 389

1 Answers1

21

exec returns the output so simply set a variable to it:

set result [exec ls -l]

You may want to wrap this in a catch however:

if {[catch {exec ls -l} result] == 0} { 
    # ...
} else { 
    # ... (error)
} 
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • Yes, may my problem is another one. I actually call the following: `set cvsPath "C:/Program Files (x86)/cvsnt/cvs.exe"; exec $::cvsPath -n upd; puts "DONE"`, and right after `exec`ing there is no message "DONE". It seams it exits, but why? – Narek Sep 26 '12 at 16:21
  • And how can I prevent exiting? – Narek Sep 26 '12 at 16:25
  • Something is wrong with `cvs upd` command. For example `cvs log filename` works correctly. – Narek Sep 26 '12 at 16:32
  • Try putting the exec call in a try in case the csv command returns an error. try {exec $::cvsPath -n upd}; – PherricOxide Sep 26 '12 at 16:34
  • I use TCL8.4. There is no `try` there, sorry. – Narek Sep 26 '12 at 16:39
  • Catch it instead then, and see what happens. But isn't the problem, then, that the call _isn't_ exiting? – Andrew Cheong Sep 26 '12 at 17:36
  • 2
    Also, *specifically for ls* you'd probably want to avoid running a subprocess for that sort of thing. All the information that it displays is available directly in Tcl via `glob` and `file stat`… – Donal Fellows Sep 27 '12 at 10:00