为什么当我使用'source'来运行TCL脚本时出现错误?

问题描述 投票:-1回答:3

我正在处理TCL脚本。当我运行tclsh script.tcl时,脚本可以正常运行,但是当我运行source script.tcl时,将不再找到命令。

#!/usr/bin/env tclsh
proc test {} {
  set a 43
  set b 27
  set c [expr $a + $b]
  set d [expr [expr $a - $b]* $c]
  for {set k 0} {$k < 10} {incr k} {
    if {$k < 5} {
      puts "k<5, pow=[expr pow($d,$k)]"
    } else {
      puts "k>=5, mod=[expr $d % $k]"
    }
  }
}

...运行时会导致错误:

$ source myfirst.tcl

Command 'proc, not found, did you mean:

  command 'croc' from snap croc (6.4.10)
  command 'prof' from deb profphd
  command 'nproc' from deb coreutils
  command 'proj' from deb proj-bin

See 'snap info <snapname>' for additional versions.

bash: myfirst.tcl: line 7: syntax error near undexpected token `k'
ubuntu tcl ns2 tclsh
3个回答
0
投票

[source不能用于运行不是用您在其中调用它的shell的本机语言]编写的任何脚本。

即,在bash中,source仅可用于运行bash脚本。它无法运行TCL脚本。本质上是这样的:source的作用是跳过运行额外的shell或其他解释器

(因此,强制忽略#!/usr/bin/env tclsh shebang),然后在您已经在其中的shell中运行代码。

如果该shell无法本地解析您正在编写的脚本所用的语言,则可能会出现语法错误-用一种语言编写的内容将由专门设计用于支持另一种语言的解释器进行解析。错误消息上的bash:前缀清楚表明实际上是这种情况;是bash,不是tclsh,试图解释脚本。


0
投票

source(还有.)是POSIX类shell中存在的“内置”命令,它将从某个文件中执行命令当前运行中


0
投票

Tcl代码由Tcl解释器运行(通常为tclshwish,但是许多应用程序也嵌入了Tcl)。 Bash代码由bash解释器运行。两种语言仅具有极其肤浅的相似性。 source命令就是其中之一,但是proc是仅Tcl的命令(bash使用function代替了这种命令)。

© www.soinside.com 2019 - 2024. All rights reserved.