Scala scripts in Windows batch files

后端 未结 2 832
Happy的楠姐
Happy的楠姐 2021-02-04 14:00

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 %*
           


        
2条回答
  •  不知归路
    2021-02-04 14:20

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

提交回复
热议问题