How can I redirect stdout into a file in tcl

前端 未结 7 1449
不思量自难忘°
不思量自难忘° 2020-12-11 16:36

how can I redirect a proc output into a file in tcl, for example, I have a proc foo, and would like to redirect the foo output into a file bar. But got this result



        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 16:48

    To accomplish this I wrapped the call to my Tcl proc in a shell script. This works on unix, not sure about other OS.

    file - foo.tcl:

    proc foo {} {puts "Hi from foo"}

    file - foo.csh (any shell script will work, I'm using csh for this example): enter code here

    #!/usr/bin/tclsh
    source foo.tcl
    eval "foo $argv"

    file - main.tcl:

    exec foo.csh > ./myoutput.txt

    Of course these commands can be made 'fancy' by wrapping them in safety measures like catch, etc... for clarity sake I didn't include them, but I would recommend their use. Also I included $argv which isn't needed since proc foo doesn't take args, but typically IRL there will be args. Be sure to use >> if you just want to append to the file rather than overwriting it.

提交回复
热议问题