Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call bash commands from tcl script?

Bash commands are available from an interactive tclsh session. E.g. in a tclsh session you can have

% ls

instead of

$ exec ls

However, you cant have a tcl script which calls bash commands directly (i.e. without exec).

How can I make tclsh to recognize bash commands while interpreting tcl script files, just like it does in an interactive session?

I guess there is some tcl package (or something like that), which is being loaded automatically while launching an interactive session to support direct calls of bash commans. How can I load it manually in tcl script files?

like image 405
Vahagn Avatar asked Nov 07 '10 07:11

Vahagn


People also ask

How do I run a Tcl script in a shell script?

You can run this program by starting tclsh from the start menu, then typing the command source c:/hello. tcl. Note that the traditional Windows \ is replaced with /. to your script.

How do you call a procedure in Tcl?

To create a TCL procedure without any parameter you should use the proc keyword followed by the procedure name then the scope of your procedure. proc hello_world {} { // Use puts to print your output in the terminal. // If your procedure return data use return keyword. }


1 Answers

If you want to have specific utilities available in your scripts, write bridging procedures:

proc ls args {
    exec {*}[auto_execok ls] {*}$args
}

That will even work (with obvious adaptation) for most shell builtins or on Windows. (To be fair, you usually don't want to use an external ls; the internal glob command usually suffices, sometimes with extra help from some file subcommands.) Some commands need a little more work (e.g., redirecting input so it comes from the terminal, with an extra <@stdin or </dev/tty; that's needed for stty on some platforms) but that works reasonably well.

However, if what you're asking for is to have arbitrary execution of external programs without any extra code to mark that they are external, that's considered to be against the ethos of Tcl. The issue is that it makes the code quite a lot harder to maintain; it's not obvious that you're doing an expensive call-out instead of using something (relatively) cheap that's internal. Putting in the exec in that case isn't that onerous…

like image 83
Donal Fellows Avatar answered Oct 02 '22 17:10

Donal Fellows