Scalaでシェルスクリプトを呼び出す方法は何ですか?

Scalaでは、Shellスクリプトを呼び出す際には、Processオブジェクトを使用してシェルコマンドを実行することができます。

以下は例です:

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

上記の例では、シェルコマンドを実行するために!演算子を使用し、返された終了コードでコマンドの成功を判断しています。終了コードが0の場合、コマンドは成功しています。

bannerAds