更短的 Scala 脚本头 [英] Shorter Scala Script header
问题描述
可以通过以下方式在 Scala 中编写一个文本文件来编写 shell 脚本:
It's possible to write shell scripts in Scala by starting a text file with:
#!/bin/sh
exec scala "$0" "$@"
!#
为了简化脚本创建,我想编写一个名为 scalash
(可能是 BASH 脚本)的可执行文件,允许将 Scala 脚本头缩短为一行:
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
有可能吗?如果我可以将可选参数传递给 scalash
,例如添加类路径依赖项,则额外加分.
Is it possible ? Extra points if I can pass optional parameters to scalash
, for instance to add classpath dependencies.
推荐答案
在 Scala 2.11 中,您可以这样做(与大多数其他语言完全一样):
In Scala 2.11, you can do it as follows (exactly as with most other languages):
#!/usr/bin/env scala
println(args.mkString(" "))
<小时>
在 Scala 2.9.0.1 中,您可以简单地创建以下脚本:
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)
用法:
# ./test.scala Hello world!
Hello world!
这篇关于更短的 Scala 脚本头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!