0

I am working on TCL scripts right now. When I run tclsh script.tcl the script works perfectly but when I run source script.tcl the commands are no longer found.

#!/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]"
    }
  }
}

...which, when run, causes the error:

$ 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'
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/a/285557/14122) -- content that isn't included *in the question itself* doesn't count towards towards the "complete" criteria described in the [mre] page in the Help Center. – Charles Duffy Feb 21 '20 at 23:33
  • 1
    Concretely: Someone can't copy-and-paste code from a screenshot to test it themselves, or to make a few changes and test if those changes fix your bug. And content that's only in screenshots isn't indexed by search engines, so other people can't find the problem and learn from its solutions. – Charles Duffy Feb 21 '20 at 23:35
  • ...the URL is misleading (no downvote here, as I just fixed the question by transcribing the images myself), but see also the further explanation at http://idownvotedbecau.se/imageofcode – Charles Duffy Feb 21 '20 at 23:48
  • **ns2** : The executable `ns` *is* the "OTcl" interpreter, so you can omit the first line = `#!/usr/bin/env tclsh` – Knud Larsen Feb 22 '20 at 13:39

2 Answers2

1

source cannot be used to run any script that is not written in the native language of the shell you're invoking it in.

That is, in bash, source can only be used to run bash scripts. It cannot run TCL scripts. This is by its nature: What source does is skip running an extra shell or other interpreter (thus, forcing your #!/usr/bin/env tclsh shebang to be ignored), and run the code in the shell you're already in.

If that shell cannot natively parse the language that the script you're sourcing is written in, syntax errors are to be expected -- content written in one language is being parsed by an interpreter designed exclusively to support a different one. The bash: prefix on your error message makes it clear that this is the case in practice; it's bash, not tclsh, trying to interpret the script.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • It's possible to build polyglot scripts, but it is really tricky to do all that much (especially if you want to be able to edit the script afterwards too; they degenerate into write-only nastiness to easily). – Donal Fellows Feb 22 '20 at 07:21
1

Tcl code is run by a Tcl interpreter (usually tclsh or wish, but many applications embed Tcl too). Bash code is run by a bash interpreter. The two languages have only extremely superficial similarities; the source command is one of those, but proc is a Tcl-only command (bash has function instead for that sort of thing).

This means that whatever runs that source command needs to also understand what the target file is written in.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • If you believe a community wiki answer is incomplete, feel free to edit them -- that's what they are, after all, owned by the community rather than the individual who first posted them. In this case, I posted CW on account of being absolutely certain the question was a duplicate and thus that answering would be in violation of the "Answer Well-Asked Questions" section of How to Answer, but having trouble running it down (we've had folks try to source Python or other non-bash scripts from bash, but it's a hard thing to find good search terms for!) – Charles Duffy Feb 23 '20 at 02:31
  • @CharlesDuffy I have always rejected the concept of community wiki answers (but the reasons for that don't matter here; I will not debate that matter). _Your_ answer is from the perspective of bash, mine is from the perspective of Tcl. – Donal Fellows Feb 23 '20 at 07:55