9

In Programming in Scala, it gives a description on how to run Scala scripts from batch files (link).

For Windows

 ::#! @echo off call scala %0 %* goto :eof ::!# 

I'm having a problem googling ::#!. What does this mean? I know :: denotes a comment and in Unix #! is a direction to the shell to be used, but what is it exactly here? And the ::!#?

What exactly does %0 %* mean, and is it necessary to express it like this?

Is it possible to run multiple scripts from the same batch file?

2 Answers 2

15

This is a gimmick, but it works. It intends to replicate Unix shell's ability to invoke a particular command to process a shell file. So, here's the explanation:

::#! 

Lines starting with :: are comments in Windows shell, so this is just a comment.

@echo off 

Don't show lines executed from here on. The @ at the beginning ensure this line itself won't be shown.

call scala %0 %* 

Transfer execution to the scala script. The %0 means the name of this file itself (so that scala can find it), and %* are the parameters that were passed in its execution.

For example, say these lines are in a file called count.bat, and you invoked it by typing count 1 2 3. In this case, that line will execute scala count 1 2 3 -- in which case you'll get an error. You must invoke it by typing count.bat.

goto :eof 

Finish executing the script.

::!# 

Another comment line.

So, here's the trick... Scala, once invoked, will find the file passed as the first argument, check if the first line is ::#!, ignore everything up to the line ::!# if so, and then execute the rest of the file (the lines after ::!#) as a Scala script.

In other words, it is not the Windows shell that is smart, it's Scala. :-)

Sign up to request clarification or add additional context in comments.

Comments

2

%0 indicates for the program name(the script file name maybe), %* indicates for the command line parameters list. %1 means the first parameter...

1 Comment

The same arguments are used in this Javascript batch file stackoverflow.com/questions/4999395/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.