0

How do I execute BASH 'source' command using scala 2.11?

import scala.sys.process.Process

object putFileToHDFS {
  def main(args: Array[String]): Unit = {
    println("""In class putFileToHDFS""");

    val tester = Process("source ./trial.sh").lineStream;
    tester.foreach(println)


  }
}
  • Possible duplicate of [Execute shell script from scala aplication](https://stackoverflow.com/questions/40129780/execute-shell-script-from-scala-aplication) – Rene Knop Jul 13 '18 at 06:23
  • What does it even mean to "source" a bash script from Scala? Since Scala is by definition not bash, it obviously cannot "source" files with bash definitions. Unclear what you expected to happen. – Andrey Tyukin Jul 13 '18 at 11:33

2 Answers2

1

I think this is the equivalent of what source does.

import scala.sys.process._

              //send file contents to sh for interpretation
val tester = "/bin/cat /path/trial.sh".#|("/bin/sh").lineStream

Of course, things are much easier if trial.sh is executable.

val tester = "/bin/sh -c /path/trial.sh".lineStream
jwvh
  • 50,871
  • 7
  • 38
  • 64
1

You do not need to source the script file, since you only need to do that when you want to include a script from another one. All you need to do is run it:

Process("bash trial.sh").lineStream
cbley
  • 4,538
  • 1
  • 17
  • 32