在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,则表示命令执行成功。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:shell怎么调用perl脚本