【问题标题】:Execute UNIX shell command with TCL version 8.5 commands使用 TCL 8.5 版命令执行 UNIX shell 命令
【发布时间】:2021-08-13 12:03:38
【问题描述】:

我正在尝试计算文件夹中不同 TCL 脚本的 SHA1 以进行版本检查(我的 TCL 版本是 8.5)。我想在其中一个 TCL 脚本中执行此操作,并将此信息打印到单独的文件中。我以为我可以这样做:

set repoDir "some/repo/dir"
set scripts_hash [cd $repoDir/scripts; exec sha1sum ./* | sha1sum]
puts $file $scripts_hash

但是,exec 不允许这样做吗?如何从 .t​​cl 脚本中执行 sha1sum shell 命令?

【问题讨论】:

    标签: shell unix tcl exec sha1sum


    【解决方案1】:

    您可以显式循环遍历每个元素,也可以将它们放在一起。这是一个循环选项:

    # ... open $file
    set repoDir "some/repo/dir"
    foreach f [glob -directory $repoDir -- "*"] {
      puts $file [exec sha1sum $f]
    }
    close $file
    

    这里是给sha1sum的一次性电话:

    # ... open $file
    set repoDir "some/repo/dir"
    puts $file [exec sha1sum {*}[glob -directory $repoDir -- "*"]]
    close $file
    

    {*} 前缀强制 TCL (v8.5+) 扩展每个生成的 glob 列表项以成为 sha1sum 的单独参数。

    【讨论】:

    • 作为(更糟糕的)替代方案,启动一个 shell 来扩展 glob:exec sh -c {sha1sum ./* | sha1sum}
    猜你喜欢
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    相关资源
    最近更新 更多