您可以在可由外部程序调用的 Bash 脚本或 Python 脚本中嵌入 TCL 脚本吗?

发布于 2024-09-27 15:38:11 字数 1375 浏览 5 评论 0原文

我正在编写一个脚本来提取有关我一直在运行的一系列化学模拟的一些有用数据。

为了获取此数据,我需要 (1) 一个 C 程序,用于根据名为 *.pdb 的文件类型计算密度。我已经有了(1)。 (2) 我需要使用一个名为 vmd 的程序来获取该 pdb。为了从命令行完成 (2),我可以提交一个 tcl 脚本,因为 vmd 在 tcl 解释器中有一个构建。

这些函数——调用 vmd 来运行 tcl 脚本,然后运行编译后的 C 程序——将是我的包装器数据提取脚本的关键活动。

我想消除多余的 TCL 脚本,将我的计数从 2 个脚本(包装脚本 + vmd 的 tcl 脚本)减少到 1 个。但我不太确定如何做到这一点。一个潜在的解决方案似乎是将我的 TCL 脚本嵌入到我的包装脚本中,如果有一种方法可以使此类嵌入脚本可从外部程序调用。

到目前为止,我的大多数数据收集脚本都是使用 BASH 编写的,因此理想情况下,我希望坚持使用 BASH 脚本,因为我非常熟悉 bash 脚本,而不是对 Python/Perl 仅有初步了解。

这是我的问题:
1. 能否将 TCL 脚本嵌入到 Bash 脚本中?
2. 你能让这个脚本可由外部程序调用吗?
例如,伪代码:

#!/bin/bash
....
tclembed extract {
   #tcl script
   ...
}
...
vmd -dispdev text -e extract.tcl >& extract_results.log #where vmd is 
                                                        #an external program

3. 如果#2 的答案是否定的,您可以在 Python 中(也许使用 Minotaur 库)执行此操作吗?如果是的话,我会考虑切换到 python...
http://markmail.org/message/6kogjphzqtn4ilch
4. 如果没有,您建议如何尝试将这两个脚本(一个 tcl 例程和一个调用它的 bash 脚本)合并到一个文件中?
5. 如果有人收到这种性质的外部调用来使用 Minotaur,您可以发布一些解释性代码吗?

我想到了一种非嵌入解决方案,即#4,即在我的 Bash 脚本中编写一个函数,该函数用整个 tcl 脚本编写一个文件。这样我就会有一个脚本,但可以转储下标以供外部程序使用,然后将其删除。我感觉这个解决方案有点笨拙,尽管我确信它与嵌入式解决方案相比是有效的。

I'm writing a script to extract some useful data about a series of chemical simulations I've been running.

To get this data I need (1) a C-program that calculates the density from a file type called *.pdb. I already have (1). And (2) I need to use a program called vmd to get that pdb. In order to accomplish (2) from the command line, I can submit a tcl script, as vmd has a build in tcl interpreter.

These functions -- calling the vmd to run the tcl script, then running the compiled c-program -- will be the key activities of my wrapper data extraction script.

I would like to eliminate the superfluous TCL script, reducing my count from 2 scripts (wrapper script + tcl script for vmd) down to 1. But I'm not sure quite how to do this. One potentially solution seems to be to embed my TCL script within my wrapper script, if there's a way to make such an embedded script callable from external programs.

Most of my data collection scripts so far have been in BASH, so ideally I would like to stick to a BASH script as I'm very familiar with bash scripting versus having only beginning knowledge of Python/Perl.

Here are my questions:
1. Can you embed a TCL script inside a Bash script?
2. Can you make this script callable by an external program?
e.g. in pseudocode:

#!/bin/bash
....
tclembed extract {
   #tcl script
   ...
}
...
vmd -dispdev text -e extract.tcl >& extract_results.log #where vmd is 
                                                        #an external program

3. If the answer to #2 is no, can you do this in Python, perhaps with the Minotaur library? I would consider the switch to python, if so...
http://markmail.org/message/6kogjphzqtn4ilch
4. If not, how would you suggest trying to merge these two scripts (a tcl routine and a bash script that calls it) into a single file?
5. If anybody HAS gotten external calls of this nature to work using Minotaur, can you post some explanatory code?

I've thought of one non-embedding solution which to #4, which would be to write a function in my Bash script that writes a file with the entire tcl script. That way I would have a single script, but could dump the subscript for use with external programs, later deleting it. I have a feeling this solution is kinda kludgy though I know for sure that it works, vs. embedded solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-10-04 15:38:11

已有多种 Tcl-Python 合金。正如 Rafe Kettler 上面的评论所概述的那样,从标准 Python 安装开始。这包括 Tkinter,它构建在完整的 Tcl 解释器中,可以按照提到的 Wiki 页面中的描述进行访问。所以,是的,“用Python来做这件事”是可行的。

不过,我真的不明白这与vmd有什么关系。 vmd 已经内置在 Tcl 解释器中。虽然我完全支持“减少移动部件”的目标,例如,当 vmd 已经公开了 Tcl 时,您可以用一个脚本而不是两个脚本在 Python 中编写某些内容似乎朝着杰森·R·米克 (Jason R. Mick) 想要走的方向迈出了一步。

后来:在与 Jason R. Mick 交换意见后,我想到他可能会觉得

#!/bin/bash


echo "Here's a bit of bash-iness."

MYSCRIPT='
puts "Here I am, inside Tcl."
puts "See?  I can do calculations:  [expr 3 + 5]."
exit 0
'

tclsh << HERE
$MYSCRIPT
HERE

有启发性。当然,它的输出是

Here's a bit of bash-iness.
Here I am, inside Tcl.
See?  I can do calculations:  8.

我用 tclsh 编写的,但是,如果我跟上,Jason R. Mick 实际上会想使用 vmd。 *vmd 的适当同源词类似于

    ...
vmd -dispdev text -eofexit << HERE > output.log
$MYSCRIPT
HERE

虽然我可以想到其他几种融合 bash 和 Tcl 的方法,但我相信这个最符合原始问题的精神。

我还想指出,根据我对 vmd 的了解,用 Python 代替 Tcl 做同样的事情是完全合适的: vmd 是同样的两者都擅长。

最后,Python 和 Tcl 都是通用语言,与 bash 具有大致相同的功能,因此该项目的另一个方向是完全用 Tcl(或 Python)编写它,而不是重击。以上述方式嵌入脚本在 Tcl(或 Python)中至少和在 bash 中一样容易。

There have been several Tcl-Python alloys. As Rafe Kettler's comment above sketches, the place to start is with a standard Python installation. This includes Tkinter, which builds in a full Tcl interpreter, accessible as described in the Wiki page mentioned. So, yes, it is feasible to "do this in Python".

I really don't get what this has to do with vmd, though. vmd builds in a Tcl interpreter already. While I entirely support the aim of "reduction of moving parts", so that you have, for example, one script, rather than two, coding something in Python, when vmd already exposes Tcl, doesn't seem like a step in the direction Jason R. Mick wants to go.

SOMEWHAT LATER: after an exchange of comments with Jason R. Mick, it occurred to me he might find

#!/bin/bash


echo "Here's a bit of bash-iness."

MYSCRIPT='
puts "Here I am, inside Tcl."
puts "See?  I can do calculations:  [expr 3 + 5]."
exit 0
'

tclsh << HERE
$MYSCRIPT
HERE

suggestive. Its output, of course, is

Here's a bit of bash-iness.
Here I am, inside Tcl.
See?  I can do calculations:  8.

I wrote this in terms of tclsh, but, if I'm keeping up, Jason R. Mick will actually want to use vmd. The appropriate homologue for *vmd is something like

    ...
vmd -dispdev text -eofexit << HERE > output.log
$MYSCRIPT
HERE

While I can think of several other ways to meld bash and Tcl, I believe this one is most in the spirit of the original question.

I want to note, too, that, from the little I know of vmd, it would be entirely appropriate to do the same with Python in place of Tcl: vmd is equally adept with either.

Finally, both Python and Tcl are general-purpose languages, with approximately the same power as bash, so yet another direction to take this project would be to write it entirely in Tcl (or Python), rather than bash. Embedding scripts in the way illustrated above is at least as easy in Tcl (or Python) as in bash.

各自安好 2024-10-04 15:38:11

1. 可以在 Bash 脚本中嵌入 TCL 脚本吗?

不容易。最好的方法是将脚本写入临时文件,并将该文件的名称传递给 tclsh(如果是 Tcl/Tk 程序,则传递给 wish)。这应该是一个“简单的编程问题”,即一些尴尬的编码,但从根本上来说并不困难。

2. 你能让这个脚本可由外部程序调用吗?

我不太明白你想在这里做什么。您可以在Tcl脚本的开头放置#!行并将文件标记为可执行。效果很好。最好的方法是这个

#!/usr/bin/env tclsh8.5
your tcl script here...

3.如果#2的答案是否定的,你能用Python来做这个吗?

这个 wiki 页面提到了一个叫做 Typcl 的东西,据报道它允许从 Python 内部执行 Tcl。我从来没有尝试过。

(根据我上面的回答,我认为问题 4 和问题 5 基本上无关紧要。)

1. Can you embed a TCL script inside a Bash script?

Not easily. The best way is to write the script to a temporary file and pass the name of that file to tclsh (or wish if it is a Tcl/Tk program). That should be a "simple matter of programming", i.e., some awkward coding but not fundamentally hard.

2. Can you make this script callable by an external program?

I don't quite understand what you want to do here. You can put a #! line at the start of a Tcl script and mark the file executable. That works well. The best way of all to do that is this:

#!/usr/bin/env tclsh8.5
your tcl script here...

3. If the answer to #2 is no, can you do this in Python?

This wiki page mentions something called Typcl, which is reported to allow doing Tcl from inside Python. I have never tried it.

(I think questions 4 and 5 are largely irrelevant based on my answers above.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文