“执行源<脚本>”不适用于 tcl

发布于 2024-08-22 15:48:14 字数 315 浏览 4 评论 0原文

我尝试使用以下命令调用 Tcl 中的脚本:

exec source <script path>

并收到错误

couldn't execute "source": no such file or directory

How can I call another script from tcl?

编辑:我正在运行从办公室另一个人那里得到的命令。我被指示使用源显式运行“source”。换句话说,我如何在 Tcl 中运行可以在 cshell 中工作的命令?

I'm trying to call a script in Tcl with the command:

exec source <script path>

and I get the error

couldn't execute "source": no such file or directory

How can I call another script from tcl?

Edit: I am running a command I got from another person in my office. I was instructed to run "source " explicitly with source. So in other words, how would I run any command that would work in cshell, in Tcl?

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

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

发布评论

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

评论(6

最舍不得你 2024-08-29 15:48:14

如果给您的脚本是 cshell 脚本,您可以像这样执行它:

exec /bin/csh $path_to_script

实际上,这就是“source”命令在交互式 shell 中执行的操作。目前尚不清楚这是否真的是您想要做的(不完全是,但对于本次讨论来说足够接近)。

无法执行 source 命令的原因是 exec 只能对可执行文件起作用(因此名称为“exec”)。 source 命令不是作为可执行文件实现的,它是 shell 中内置的命令。因此,它不能被执行。

如果您确实觉得需要执行 source 命令或任何其他内置命令,您可以执行以下操作:

exec /bin/csh -c "source $path_to_script"

在上面的示例中,您正在执行 c shell,并要求它运行命令“source”。对于 source 命令的具体情况,这并没有多大意义。

但是,我不确定这是否真的能达到您的预期。通常,如果有人说“这里有一些命令,只需执行 'source ',它通常只是定义一些别名以及要在交互式 shell 中使用的内容。这些别名在 Tcl 中不起作用。

If the script you were given is a cshell script, you can exec it like this:

exec /bin/csh $path_to_script

In effect, this is what the 'source' command does from within an interactive shell. It's not clear whether this is really what you want to do or not (not exactly, but close enough for this discussion).

The reason you can't exec the source command is that exec will only work on executable files (hence the name 'exec'). The source command isn't implemented as an exectuable file, it is a command built-in to the shell. Thus, it can't be exec'd.

If you really feel the need to exec the source command or any other built-in command you can do something like this:

exec /bin/csh -c "source $path_to_script"

In the above example you are execing the c shell, and asking it to run the command "source ". For the specific case of the source command, this doesn't really make much sense.

However, I'm not sure any of this will really do what you expect. Usually if someone says "here's some commands, just do 'source ', it usually just defines some aliases and whatnot to be used from within an interactive shell. Those aliases won't work from within Tcl.

放手` 2024-08-29 15:48:14

csh 中的 source 与 bash 中的 . 一样,执行脚本而不生成新进程。

效果是该脚本中设置的任何变量在当前 csh 会话中可用。

实际上,source是csh的内置命令,因此无法从tcl exec中获得,并且在没有source的情况下使用exec code> 不会给出特定的 source 效果。

没有简单的方法可以解决您的问题。

source in csh, like . in bash, executes a script without spawning a new process.

The effect is that any variable that is set in that script is available in current csh session.

Actually, source is a built-in command of csh, thus not available from tcl exec, and using exec without source would not give the specific source effect.

There is no simple way to solve your problem.

稳稳的幸福 2024-08-29 15:48:14

source 加载你应该做的源文件

source <script path>

如果你想执行它,那么你需要调用主过程。

另一种选择是:

exec [info nameofexecutable] <scritp path>

source load the source file

you should do:

source <script path>

If you want to execute it, then you need to call the main proc.

another option would be to do:

exec [info nameofexecutable] <scritp path>
作死小能手 2024-08-29 15:48:14

这里有些混乱。 exec 运行一个单独的程序,可能带有参数。
source 不是一个单独的程序,它是另一个 Tcl 命令,它读取 Tcl 命令文件并执行它们,但不传递参数。如果您尝试调用的其他脚本被编写为从命令行运行,则它将期望在变量 argv 中以列表形式找到其参数。您可以通过在运行源之前将 argv 设置为参数列表来伪造此内容,例如。

set argv {first_arg second_arg}
source script_path

或者,您可以使用 exec 启动整个单独的 Tcl 可执行文件并向其传递脚本和参数:

exec script_path first_arg second_arg

Some confusion here. exec runs a separate program, possibly with arguments.
source is not a separate program, it is another Tcl command which reads a file of Tcl commands and executes them, but does not pass arguments. If the other script you are trying to call is written to be run on from the command line, it will expect to find its arguments as a list in variable argv. You can fake this by setting argv to the list of arguments before running source, eg.

set argv {first_arg second_arg}
source script_path

Alternatively you could use exec to start a whole separate Tcl executable and pass it the script and arguments:

exec script_path first_arg second_arg
白馒头 2024-08-29 15:48:14

错误本身就说明了问题。确保提供正确的路径名,如有必要请指定完整路径。并确保该目录中确实存在该文件

the error speaks for itself. Make sure you give the correct path name, specify full path if necessary. and make sure there is indeed the file exists in that directory

貪欢 2024-08-29 15:48:14

最近我想通过获取 shell 脚本来设置一些 UNIX 环境变量,并偶然发现了同样的问题。我发现这个简单的解决方案非常适合我:

只需使用一个小的 3 行包装脚本,在 Tcl 脚本启动之前在 UNIX shell 中执行源命令。
例子:

#!/bin/csh
source SetMyEnvironment.csh
tclsh MyScript.tcl

Recently I wanted to set some UNIX environment variables by sourcing a shell script and stumbled across the same problem. I found this simple solution that works perfectly for me:

Just use a little 3-line wrapper script that executes the source command in a UNIX shell before your Tcl script is started.
Example:

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