For this specific problem, this appears to be a simpler solution:
#!/usr/bin/env tclsh
# @(#) s1 Demonstrate method for printing results from exec.
set version [ info tclversion ]
set message " Hello, world from tclsh ($version)."
puts stdout $message
puts stdout ""
set m [ exec ls -1gG ]
puts stdout $m
Producing:
% ./s1
Hello, world from tclsh (8.4).
total 8
-rw-r--r-- 1 182 Dec 19 05:30 readme.txt
-rwxr--r-- 1 246 Dec 19 05:40 s1
Best wishes ... cheers, drl
PS. In future posts, please enclose code and data in CODE tags. The easy way to do this is to highlight the text in the edit window with your mouse, then click the # button just above the editing window.
I was thinking that the OP may wish to do processing on specific lines and not just printing. Here is an extended example showing a way to do that:
#!/usr/bin/env tclsh
# @(#) s3 Demonstrate method for printing results from exec.
set version [ info tclversion ]
set message " Hello, world from tclsh ($version)."
puts stdout $message
puts stdout ""
puts " As results from command substitution:"
set m [ exec ls -1gG ]
puts stdout $m
puts stdout ""
puts stdout " With foreach loop on list:"
foreach i [ list [ exec ls -1gG ] ] {
puts stdout $i
}
exit 0
Producing:
% ./s3
Hello, world from tclsh (8.4).
As results from command substitution:
total 16
-rw-r--r-- 1 182 Dec 19 05:30 readme.txt
-rwxr--r-- 1 246 Dec 19 05:40 s1
-rwxr--r-- 1 754 Dec 19 08:37 s2
-rwxr--r-- 1 415 Dec 19 08:40 s3
With foreach loop on list:
total 16
-rw-r--r-- 1 182 Dec 19 05:30 readme.txt
-rwxr--r-- 1 246 Dec 19 05:40 s1
-rwxr--r-- 1 754 Dec 19 08:37 s2
-rwxr--r-- 1 415 Dec 19 08:40 s3
Best wishes ... cheers, drl
Reference to consider:
Title: Practical Programming in Tcl and Tk
Author: Brent Welch, Ken Jones
Edition: 4th
Date: June 20, 2003
Publisher:
ISBN: 0130385603
Pages: 960
Categories: programming, text processing, GUI
Comments: 3.5 stars, 41 reviews (2008.12)
Comments: (I have 3rd edition)