如何将参数/参数传递给SCALA脚本

时间:2018-06-15 13:52:19

标签: scala arguments

我们如何将参数传递给scala脚本,就像我们将参数传递给shell脚本一样。

2 个答案:

答案 0 :(得分:0)

使用bash命令在此基础上声明您的脚本

Test.scala

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

object Test {
  def main(args: Array[String]): Unit = {
    println(s"args: ${args.mkString("[", ", ", "]")}")
  }
}

它有效

[Desktop] ./Test.scala "scala is awesome" "java8 has lambdas"
args: [scala is awesome, java8 has lambdas]

有关$0$@

的更多信息
0  Expands to the name of the shell or shell script.
   This is set at shell initialization. If bash  is  
   invoked  with  a  file  of commands, $0 is set to 
   the name of that file.  If bash is started with 
   the -c option, then $0 is set to the first argument 
   after the string to  be  executed, if one is present.
   Otherwise, it is set to the file name used to invoke 
   bash, as given by argument zero.

@  Expands  to  the  positional  parameters, starting from 
   one. When the expansion occurs within double quotes, each 
   parameter expands to a separate word. That is, "$@" is 
   equivalent to "$1", "$2" ... If the double-quoted 
   expansion occurs within a word, the expansion of the first
   parameter is joined with the beginning part of the original 
   word, and the expansion of the last parameter is joined
   with the last part of the original word. When there are no 
   positional parameters, "$@" and $@ expand to nothing 
   (i.e., they are removed).

了解更多信息,请访问:Command line args for Scala scripts

答案 1 :(得分:0)

就像必须设置环境变量以传递JVM参数一样,您可以为参数设置环境变量。

set MYVARS=arg1 arg2 arg3

然后在您的scala脚本中:

val args = sys.env("MYVARS").split(" ").map(_.trim).toList
args.foreach { println }