背景
最近工作中需要用scala开发一个简单的脚本,希望做到每次只输入入参。如果进入scala RELP交互式界面,则需要每次都输入所有代码。
1 // ProductCategoryAppid.scala
2 object ProductCategoryAppid {
3 def main(args:Array[String]): Unit = {
4 val str = args(0) // input a
5 val tmp = str.split(",").map(x=>x.split(":")(0))
6 val res = tmp.map(x=>"\"".concat(x).concat("\""))
7 println(res.mkString(","))
8 }
9 }
方法1
$ scala ProductCategoryAppid.scala "1426584:3.0,2801778:3.0"
$ "1426584","2801778"
方法2
$ scalac ProductCategoryAppid.scala
可以看到,编译之后,会生成如下若干文件
ProductCategoryAppid$$anonfun$1.class
ProductCategoryAppid$$anonfun$2.class
ProductCategoryAppid$.class
ProductCategoryAppid.class
ProductCategoryAppid.scala
然后
$ scala ProductCategoryAppid "1426584:3.0,2801778:3.0"
$ "1426584","2801778"
总结
在Linux环境中直接运行scala,可以直接scala xx.scala args。也可以先scalac xx.scala,然后再scala xx args。