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?
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.
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. }
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…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With