温馨提示×

scala怎么调用shell脚本

小亿
168
2024-01-11 23:08:11
栏目: 编程语言

在Scala中调用shell脚本可以使用Process对象来执行shell命令。

以下是一个示例:

import sys.process._ object ShellCommand { def main(args: Array[String]): Unit = { val command = "sh /path/to/script.sh" // 替换成你的shell脚本路径和文件名 val exitCode = command.! if (exitCode == 0) { println("Shell command executed successfully.") } else { println(s"Shell command failed with exit code $exitCode.") } } } 

在上面的示例中,我们使用!操作符来执行shell命令,并通过返回的退出码来判断命令是否执行成功。如果退出码为0,则表示命令执行成功。

0