如何从 scala 应用程序将参数传递给 shell 脚本?
我有一个 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
预期输出:
Hello World,arg1 arg2(其中 arg1 和 arg2 是从 scala 传递的)
I have a Scala application which needs to call the shell script by passing some arguments to it.
I followed the below answer and I am able to call the shell script from scala app without passing any arguments. But I have no idea how to pass the arguments.
Execute shell script from scala application
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
Current Output:
Hello World
Expected Output:
Hello World, arg1 arg2 (where arg1 and arg2 were passed from scala)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scala 文档<中有详细解释/a> 但我最喜欢的方法是这样的:
只需在列表上调用
!!
方法,其中第一个元素是脚本/命令,其余元素是参数/选项。It is explained in great detail in Scala docs but my favorite way is this:
simply call the
!!
method on a list where the first element is the script/command and the remaining elements are the args/options.除了 @goosefand的答案外,我还添加了如何接收来自Shell脚本中Scala的参数。
scala:
shell脚本:
输出:
In addition to @goosefand's answer, I have also added how to receive the arguments which is passed from scala in the shell script.
Scala:
Shell Script:
Output: