【问题标题】:tcl proc command not working while running file from shell从 shell 运行文件时 tcl proc 命令不起作用
【发布时间】:2013-10-27 04:17:08
【问题描述】:

我是编程新手,想运行我的第一个 tcl 代码。代码在这里

proc tempconv {} {

    set lower 0
    set upper 150
    set step 25
    set fahr $lower
    while {$fahr<$upper} {
        set celsius [expr 5*($fahr-32)/9]
        puts "Fahernheit/Celsius : $fahr/$celsius"
        incr fahr $step
                  }
          }

我已将此保存到文件 test.tcl.. 问题是当我尝试使用 shell 环境运行它时,它没有显示任何结果,但是当删除此 proc 语句并且不执行任何程序时,结果会显示..任何人都可以告诉我如何解决这个问题。 使用proc语句结果是这样的

muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ ns test.tcl
muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ 

没有程序,结果就在这里

muhammad@muhammad-HP-G62-Notebook-PC:~$ cd ns-allinone-2.35/
muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ ns test.tcl
Fahernheit/Celsius : 0/-18
Fahernheit/Celsius : 25/-4
Fahernheit/Celsius : 50/10
Fahernheit/Celsius : 75/23
Fahernheit/Celsius : 100/37
Fahernheit/Celsius : 125/51

【问题讨论】:

    标签: shell tcl ns2


    【解决方案1】:

    你需要调用proc:

    # Define a procedure called test
    proc test {} {
       puts "Test Running"
    }
    
    # Call the procedure
    test
    

    【讨论】:

    • 感谢杰克逊的快速回答..问题仍然相同..muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ ns test.tcl muhammad@muhammad -HP-G62-Notebook-PC:~/ns-allinone-2.35$............它没有显示文本测试运行......
    • muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ ns test.tcl muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone- 2.35$ 测试 muhammad@muhammad-HP-G62-Notebook-PC:~/ns-allinone-2.35$ 即使调用 proc test 也没有结果..
    • 如果我直接在 shell 的 ns 环境中为你编写代码,那么它可以工作,但是如果我将它保存到一个文件然后从 shell 运行文件,它就不起作用..即使我调用 procedure. .muhammad@muhammad-HP-G62-Notebook-PC:~$ ns % proc test {} { puts "Test Running" } % test 测试运行 %
    • 当你说'从外壳调用文件'是什么意思? tclsh myfile.tcl 或 juts myfile.tcl.
    • 我的意思是 tclsh myfile.tcl
    【解决方案2】:
    proc procedure { } {
    puts "XYZABC"
    }
    procedure
    

    然后在终端转到

    ns-allinone-2.35/ns-2.35/
    

    目录然后运行

    ns pathtoyourfile/filename.tcl
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2021-10-23
      • 2021-05-30
      相关资源
      最近更新 更多