Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala scripts in Windows batch files

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?

like image 761
Luigi Plinge Avatar asked Jul 12 '11 22:07

Luigi Plinge


2 Answers

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. :-)

like image 82
Daniel C. Sobral Avatar answered Nov 19 '22 08:11

Daniel C. Sobral


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

like image 36
jilen Avatar answered Nov 19 '22 06:11

jilen