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)


  }
}
2
  • Possible duplicate of Execute shell script from scala aplication Commented Jul 13, 2018 at 6: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. Commented Jul 13, 2018 at 11:33

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.