从另一个目录采购TCL脚本

发布于 2025-02-06 21:57:17 字数 405 浏览 1 评论 0原文

我有两个TCL脚本,其中一个叫另一个。第二个脚本“ version.tcl”位于工作目录的子目录(“综合”)中。

我目前已经以以下方式进行了设置并运行:

cd ./synthesis
source version.tcl
cd ../

我想将其凝结到一个可以源自脚本的命令中,而无需更改目录,这是:

source [file join [file join [[pwd] synthesis] version.tcl]]

source ./synthesis/version.tcl

不幸的是,上述命令都不可用。我知道这对于比我更熟悉的人来说应该很容易解决,因此,任何帮助都将不胜感激。

I have two tcl scripts, one of which calls the other. The second script, 'version.tcl' is in a sub-directory ('synthesis') of the working directory.

I currently have it set up and working in the following fashion:

cd ./synthesis
source version.tcl
cd ../

I would like to condense that to one command which can source the script without having to change directories, something along the lines of:

source [file join [file join [[pwd] synthesis] version.tcl]]

Or

source ./synthesis/version.tcl

Unfortunately neither one of the above commands works. I know this should be an easy fix for someone with more familiarity with tcl than me, so any help would be greatly appreciated.

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

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

发布评论

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

评论(1

赠佳期 2025-02-13 21:57:17

source命令在其他目录中读取文件,很好。它使用与Open来处理文件名的机器几乎完全相同(这不仅仅是“将其交给OS”)。我们知道它有效;经过测试,这是许多人在许多平台上使用的东西。

几乎可以肯定的是,该文件内部的代码期望将当前目录是包含脚本的目录。您修复的选项

  1. 执行您当前正在做的事情,或者是它更强大的版本:

    <
    设置PWD [PWD]
    CD $目录
    尝试 {
    uplevel 1 [列表源$ scriptName]
    } 最后 {
    CD $ PWD
    }
    }

  2. 更新您调用的代码,以便它相对于脚本访问资源,而不是当前目录:

     ##将其放在脚本的顶部,外部过程,但在命名空间内部
    基于变量[file dirname [file formanize [info脚本]]]
    
    #然后可以与之相对于以下方式访问:
    打开[文件加入$ asedir foo.txt]
     

The source command reads files in other directories just fine; it uses almost exactly the same machinery as open to handle filenames (and that's little more than just "give it to the OS"). We know that it works; it's tested and it is something that very many people have used across many platforms.

What's going wrong is almost certainly that the code inside that file expects to have the current directory be the one that contains the script. Your options to fix that are

  1. Do what you're currently doing, or a slightly more robust version of it:

    proc sourceInDir {directory scriptName} {
        set pwd [pwd]
        cd $directory
        try {
            uplevel 1 [list source $scriptName]
        } finally {
            cd $pwd
        }
    }
    
  2. Update the code that you call so it accesses resources relative to the script, not to the current directory:

    # Put this at the top of the script, OUTSIDE procedures but INSIDE namespace eval
    variable baseDir [file dirname [file normalize [info script]]]
    
    # Then can access relative to that with, say:
    open [file join $baseDir foo.txt]
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文