如何从shell脚本中运行TCL脚本?

6

我对TCL脚本和shell脚本都很陌生。我想从shell脚本中调用一个TCL脚本。我已经尝试了以下方法。

#!/bin/sh

for i in {1..5}
do
   my_script
   test_script
done

如果我运行脚本,会抛出以下错误:

./sample.sh: line 5: my_script: command not found
./sample.sh: line 5: test_script: command not found

有人能帮我解决这个问题吗?

提前感谢您的帮助。

3个回答

8
如果您没有将脚本设为可执行(使用chmod +x),那么您需要使用以下命令:
tclsh my_script.tcl

或者可能是 tclsh8.5 /path/to/script.tcl 或其它变体。
如果你已经将脚本设置为可执行的,请检查包含脚本的目录是否在你的 PATH 中(如果不在,请使用完整的文件名或调整你的 PATH),并且你有一个适当的 #! 行。通常推荐的行是:
#!/usr/bin/env tclsh8.5

这将搜索您的路径以查找tclsh8.5可执行文件,而不是硬编码它。


7

如果在您的$PATH中找不到它们,您必须提供指向脚本的路径,例如:

./my_myscript         # current directory
/path/to/test_script  # absolute path

0

来自man tclsh。我猜第二个块回答了你的问题。

If you create a Tcl script in a file whose first line is #!/usr/local/bin/tclsh then you can invoke the script file directly from your shell if you mark the file as executable. [...]

An even better approach is to start your script files with the following three lines:

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" ${1+"$@"}

This approach has three advantages over the approach in the previous paragraph [...]

You should note that it is also common practice to install tclsh with its version number as part of the name.This has the advantage of allowing multiple versions of Tcl to exist on the same system at once, but also the disadvantage of making it harder to write scripts that start up uniformly across different versions of Tcl.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接