如何从 Scala 应用程序将参数传递给 shell 脚本?
How to pass arguments to shell script from a scala application?
我有一个 Scala 应用程序,它需要通过向它传递一些参数来调用 shell 脚本。
我遵循了以下答案,并且能够在不传递任何参数的情况下从 scala 应用程序调用 shell 脚本。但是我不知道如何传递参数。
object ScalaShell {
def main(args : Array[String]): Unit = {
val output = Try("//Users//xxxxx//Scala-workbench//src//main//scala//HelloWorld.sh".!!) match {
case Success(value) =>
value
case Failure(exception) =>
s"Failed to run: " + exception.getMessage
}
print(output)
}
}
HelloWorld.sh
#!/bin/sh
# This is a comment!
echo Hello World
当前输出:
你好世界
预期输出:
Hello World,arg1 arg2(其中 arg1 和 arg2 是从 scala 传递的)
在Scala docs中有很详细的解释,但我最喜欢的方式是:
List("echo", "-e", "This \nis \na \ntest").!!
只需在第一个元素为 script/command 且其余元素为 args/options.
的列表上调用 !!
方法
除了@goosefand 的回答,我还在 shell 脚本中添加了如何接收从 scala 传递的参数。
Scala:
object ScalaShell {
def main(args : Array[String]): Unit = {
val output = Try(Seq("//Users//xxxxx//Scala-workbench//src//main//scala//HelloWorld.sh", "arg1","arg2").!!) match {
case Success(value) =>
value
case Failure(exception) =>
s"Failed to run: " + exception.getMessage
}
print(output)
}
}
Shell 脚本:
#!/bin/sh
echo Processing files
输出:
Hello world arg1 arg2
我有一个 Scala 应用程序,它需要通过向它传递一些参数来调用 shell 脚本。
我遵循了以下答案,并且能够在不传递任何参数的情况下从 scala 应用程序调用 shell 脚本。但是我不知道如何传递参数。
object ScalaShell {
def main(args : Array[String]): Unit = {
val output = Try("//Users//xxxxx//Scala-workbench//src//main//scala//HelloWorld.sh".!!) match {
case Success(value) =>
value
case Failure(exception) =>
s"Failed to run: " + exception.getMessage
}
print(output)
}
}
HelloWorld.sh
#!/bin/sh
# This is a comment!
echo Hello World
当前输出:
你好世界
预期输出:
Hello World,arg1 arg2(其中 arg1 和 arg2 是从 scala 传递的)
在Scala docs中有很详细的解释,但我最喜欢的方式是:
List("echo", "-e", "This \nis \na \ntest").!!
只需在第一个元素为 script/command 且其余元素为 args/options.
的列表上调用!!
方法
除了@goosefand 的回答,我还在 shell 脚本中添加了如何接收从 scala 传递的参数。
Scala:
object ScalaShell {
def main(args : Array[String]): Unit = {
val output = Try(Seq("//Users//xxxxx//Scala-workbench//src//main//scala//HelloWorld.sh", "arg1","arg2").!!) match {
case Success(value) =>
value
case Failure(exception) =>
s"Failed to run: " + exception.getMessage
}
print(output)
}
}
Shell 脚本:
#!/bin/sh
echo Processing files
输出:
Hello world arg1 arg2