Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking-out Individual Values Returned From "exec"

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
Say I have the following:

set OrigList [exec -keepnewline -- ls -la | grep abc]

then say I want to break-out the first (i.e. - permissions), third (i.e. - owner) and ninth (i.e. - filename) fields each to a seperate list variable ...

foreach elem $OrigList {
lappend perm [lindex $elem 0]
lappend owner [lindex $elem 2]
lappend fname [lindex $elem 8]
}


puts $perm
puts $owner
puts $fname

My output from my puts cmds contains only the elements from the first row (of the ls -la). I know it's because the OrigList variable is being treated as a string, not as a list.

How do I get the OrigList variable to act like a list?



 
I hate to do this, but I answered my own question ...

I've changed from to

set OrigList [exec -keepnewline -- ls -la | grep abc]
foreach elem $OrigList {


set OrigList [exec ls -la | grep abc]
foreach elem [split $OrigList \n] {


The exec option -keepnewline and the overall dealing with strings and lists always trip me up!
 
Is there a way to save the result of the exec cmd to a string in the first place and not have to deal with split cmd?
 
I don't have a UNIX machine to try this on but maybe it's the newline that's doing it. Try something like:
set OrgList [split $OrgList \n]

HTH

Bob Rashkin
rrashkin@csc.com
 
OK. I was too slow.

Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top