How to invoke a shell script in Scala?

In Scala, you can execute shell commands by using the Process object to call a shell script.

Here is an example:

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.")
    }
  }
}

In the example above, we used the ! operator to execute shell commands and determine if the command was successful based on the exit code returned. An exit code of 0 indicates that the command was successful.

广告
Closing in 10 seconds
bannerAds