How to call bash commands from tcl script?

天大地大妈咪最大 提交于 2019-12-22 08:09:40

问题


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?


回答1:


What's going on here is that the unknown proc is getting invoked when you type a command like ls, because that's not an existing tcl command, and by default, that command will check that if the command was invoked from an interactive session and from the top-level (not indirectly in a proc body) and it's checking to see if the proc name exists somewhere on the path. You can get something like this by writing your own proc unknown.

For a good start on this, examine the output of

info body unknown



回答2:


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…




回答3:


One thing you should know is that ls is not a Bash command. It's a standalone utility. The clue for how tclsh runs such utilities is right there in its name - sh means "shell". So it's the rough equivalent to Bash in that Bash is also a shell. Tcl != tclsh so you have to use exec.



来源:https://stackoverflow.com/questions/4116903/how-to-call-bash-commands-from-tcl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!