system : Execute pipelines directly in the POSIX shell

Sometimes I find shell pipelines more easy than Tcl pipelines when you need to do glob substitution.

proc system {str} {
    exec sh -c $str        
}

The POSIX flag -c makes sh to read commands from a string. In my case sh is linked to dash, a light and fast POSIX shell. All the string is executed by sh not by Tcl.

All the pipeline is executed by the POSIX shell so you have to use the POSIX shell redirections.

You can do things like:

>system { ls *.tcl | wc -l } 

It is a lot more clean than:

> exec ls {*}[glob *.tcl] | wc -l

Combining tcl command substitution with shell variable substitution:

>set a "hello"; set b "world"
>system [subst -novariables {a=[set a];b=[set b]; echo $a $b}]; 
hello world

The difference with the TclX system command is that you can still capture the output of the command:

>set date [system date]
>set date
lun dic 28 15:13:29 CET 2009

Comments:

Probably it is more easy to capture errors using directly exec.