开发者

Shorter Scala Script header

开发者 https://www.devze.com 2023-04-09 14:32 出处:网络
It\'s possible to write shell scripts in Scala by starting a text file with: #!/bin/sh exec scala \"$0\" \"$@\"

It's possible to write shell scripts in Scala by starting a text file with:

#!/bin/sh
exec scala "$0" "$@"
!#

To ease script creation, I would like to write an executable called scalash (perhaps a BASH script) allowing to shorten Scala script header to just one line:

#!/bin/scalash

Is it po开发者_运维技巧ssible ? Extra points if I can pass optional parameters to scalash, for instance to add classpath dependencies.


In Scala 2.11, you can do it as follows (exactly as with most other languages):

#!/usr/bin/env scala
println(args.mkString(" "))

In Scala 2.9.0.1, you can simply create the following script:

test.scala

#!/usr/bin/scala
!#
println(args.mkString(" "))

and make it executable. (change the first line to path to your executable)

Usage:

# ./test.scala Hello world!
Hello world!


See this pull request (was this). There's no issue associated with it -- if you feel like it, you could open an issue and comment on the pull request.

You can also use SBT to start the scripts. See information about scalas here.

EDIT

The pull request was accepted, so this should work:

#!/usr/bin/env /path/to/scala
etc
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号