1

I run a .tcl script using a .bat file as per the simplified example below:

script.tcl

set a "this is the script"
puts $a

script.bat

echo off
set tclpath="C:\tclsh.exe"
set filepath="C:\script.tcl"
%tclpath% %filepath%

I wonder whether I can include in the .bat file the commands of the .tcl script, so instead of having two files I just have one .bat file that runs tclsh.exe and passes the commands to the tcl shell.

Is this possible? and how can I do it?

6
  • 1
    I think you'll have to read the tclsh manual for that. That's not batch related Commented Feb 4, 2017 at 22:25
  • Since I code in tcl I have set the .tcl extension to be opened by Notepad++ or any other similar text editor. I also use, as a personal shortcut, to define the extension .tclmine associated to tclsh.exe, in that way I just doubleclick on it and it executes the code I want. But this approach lacks of portability to other machines, while .bat file are supported by any MS Windows PC. That's why I asked whether it was possible to pass commands from a .bat file to the tcl shell. Commented Feb 4, 2017 at 22:46
  • You probably can. We have a running thread over on Dostips that shows how to run different scripting languages with batch files in one single script file. Commented Feb 4, 2017 at 23:05
  • 1
    tclsh seems to accept commands from its standard input also (according to this site ). So things of the form command | tclsh should work. You'll just have to make the command write the correct tcl-commands to its standard output. command can be a simple batch script or even a call to a function that echos those tcl commands. But as I am not used to tcl I don't know if it will work (is also the reason why I didn't put this as an answer). I'll let tcl-experts handle this Commented Feb 4, 2017 at 23:06
  • @J.Baoby, it's the batch experts we need. If this was unix shell, I'd recommend a here document, but I don't know how to do that in DOS batch. Commented Feb 4, 2017 at 23:23

2 Answers 2

1

For many things, there are pages on the Tcler's Wiki that can be looked to for interesting things. In particular, this old page has some really useful techniques. As you read through, you'll see a history of techniques tried. They depend on the fact that Tcl commands can be prefixed with :: usually (marking them as a weird label in the batch file language) and you can comment out blocks of code in Tcl with if 0 (with Tcl not parsing the contents, beyond ensuring that it is brace-balanced, which code usually is).

The best technique is one that doesn't just make the code multilingual, but also makes it easily readable. Preserving readability is the key to not going crazy.

::if 0 {
@rem This code in here is pure batch script

echo off
set tclpath="C:\tclsh.exe"
%tclpath% "%~f0" %*

@rem Put this at the end; it means terminate since the “eof” label is end-of-file
@goto eof
}

# This code is the pure Tcl code

set a "this is the script"
puts $a

The other bits to be aware of:

  • "%~f0" — This gets the full path to the “zero-th argument”, which is the name of the script you're running.
  • %* — This is all the remaining arguments. It's a good idea to pass them on, and you can access them from Tcl using the list in the global argv variable.
Sign up to request clarification or add additional context in comments.

1 Comment

I think it has to be @goto :eof (with colon) to work properly.
1

I wonder whether I can include in the .bat file the commands of the .tcl script, so instead of having two files I just have one .bat file that runs tclsh.exe and passes the commands to the tcl shell.

Easy peasy. . .

You can use a CALL to a subroutine in a batch script that will append the commands to the dynamically created script file which you specify with the set filepath variable.

This way you have everything in the batch script and you do not need to worry about the tcl script file other than ensuring the :tclshScript routine that creates it has the correct syntax, etc.

You essentially build the tcl script logic with batch ECHO commands and it'll create it per run.

Use caution with special characters though as the carat ^ symbol may be needed to escape certain character to the tcl script if batch interprets those otherwise or you notice an issue.

echo off
set tclpath="C:\tclsh.exe"
set filepath="C:\script.tcl"
IF EXIST "%filepath%" DEL /Q /F "%filepath%"
CALL :tclshScript
%tclpath% %filepath%
EXIT

:tclshScript
ECHO set a "this is the script">>%filepath%
ECHO puts $a>>%filepath%
GOTO EOF

Further Resources

1 Comment

Just as a comment the goto eof didn't work at the beginning, so I changed it to exit /b, and then I realised I just needed to write goto :eof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.