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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exec or Open?

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I would like to exec some unix commands from my Tcl scrips
a command like for instance:
set command = plist | awk '{ print \$1}' where plist returns some data.

then I tried:

set status [catch {exec {$command}} plistout] -> got errors

set status [catch {open "$command"} plistout] -> plistout contains file4.

What should I use?

Thanks,
 
Try:
[open "| $command" r+] Bob Rashkin
rrashkin@csc.com
 
I've tried the following:

set command "plist | awk '{print \$1}'"

set status [catch [open "| $command" +r] result]

this does not work.

Thanks
 
You can try:
Code:
set cmd "ps | wc -l"
set rc [catch {eval exec $cmd} res]
if {$rc == 0} { puts "result: $res" } else { puts stderr "error: $res" }
The eval before exec is here because $cmd contains spaces.
catch returns 0 if all is okay.
res contains the result of the command if catch returns 0, else the error message.

bonne chance !

ulis
 
The other way:
Code:
set rc [catch { set fp [open "|ps2 | wc -l"] } msg]
if {$rc} { puts stderr "error: $msg" } else { puts [read $fp] }
The open gets you a handle if all is okay.
Next the read gets the result of the piped command.

bonne chance

ulis
 
Hi, all.
Awk is very tricky to call from within tcl scripts.
Much of the syntax, and braces present obstacles.
Richard Suchenwirth has sseveral good examples of using embedded awk scripts but they are not for the faint of heart.

What you want done can be handled easily in tcl anyway:

proc Awkish {filename fld {FS " "}} {
if {![catch {set fd [open $filename]}]} {
set od [read $fd] ; set x 0 ; set c 0
foreach line [split $od "\n"] {
incr x
foreach ind [split $line $FS] {
set arra($x,[incr c]) $ind
}
set c 0
}
}
foreach elem [array name arra] {
if {[lindex [split $elem ","] 1] == $fld} {
puts $arra($elem)
}
}
return
}
 
A quick usage/example for this proc as I see it relates to your data manipulation question as well.

time [Awkish "/home/me/scrap.txt" 1 ","]
21/54/2001
23/04/2002
23/04/2002
24/04/2002
24/04/2002
23/78/2002
23/06/09
21 microseconds per iteration
 
Thanks for your answers guys they are useful, I agree that I should convert some awk scripts into tcl but sometimes I can't be bothered..

Anyways, I found that putting the awk script between {} works fine, i.e.

set result [exec plist | awk { {print$1} }]

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top