1

I'm new to Scala programming. I have one .sh file. I want to run this file using Scala. I have tried solutions provided in the below link. But those are not working for me.

Execute shell script from scala application

I have tried simple echo command in scala REPL and it's working fine. But when I used the same line of code in Scala program I am getting java.io.IOException like below.

Exception in thread "main" java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified

And my sample code looks like below

import java.io._
import sys.process._
object POC {  
  def main( args: Array[String]) {     
    val p = "echo 'hello world'".!!
    println("#############################################       "+ p)   
  }
}

EDIT: As per the Tom's response, I have modified above code like below and it's working fine now and getting Hello world in console.

    import java.io._
    import sys.process._
    object POC {  
      def main( args: Array[String]) {     
        val p = Seq("echo", "hello world")
        val os = sys.props("os.name").toLowerCase
        val panderToWindows = os match {
           case x if x contains "windows" => Seq("cmd", "/C") ++ command
           case _ => command
        }
        panderToWindows.!  
      }
    }

Now my exact issue is to execute my script.sh file. I have directory like below.

src
- bin
  - script.sh
- scala
  - POC.scala

script.sh code:

#!/bin
echo "Hello world"

And my POC.scala consists below code.

        import java.io._
        import sys.process._
        object POC {  
          def main( args: Array[String]) {     
            val command = Seq("/bin/script.sh")
            val os = sys.props("os.name").toLowerCase
            val panderToWindows = os match {
               case x if x contains "windows" => Seq("cmd", "/C") ++ command
               case _ => command
            }
            panderToWindows.!  
          }
        }

I didn't get any console output after executing above code. Please let me know if I missed anything. Thanks.

Valli69
  • 8,856
  • 4
  • 23
  • 29
  • Which scala version are you using ? I ran the same code and got the correct output. I am using scala version 2.12.4 – Chaitanya Oct 24 '18 at 11:15
  • @ChaitanyaWaikar I'm using Scala 2.11.2. Is it problem with version I'm using? It's working fine when I executed though scala REPL, but unable to execute though scala IDE. – Valli69 Oct 24 '18 at 11:34
  • 1
    Maybe you're facing this problem: https://stackoverflow.com/questions/24320446/run-shell-commands-in-scala-code-on-windows-seems-to-require-the-full-absolute-p – Tom Oct 24 '18 at 12:23
  • 1
    Your code compiles and runs fine for me. Try putting in the full path to the `echo` program: `/bin/echo` (Or whatever it is on your system.) – jwvh Oct 24 '18 at 23:52
  • @Tom - Thanks for your reply. It's working fine for simple linux command like 'echo'. But it's not working when I'm trying to execute .sh file. Any thoughts? – Valli69 Oct 25 '18 at 06:10
  • You haven't posted enough information to re-create the problem. I ran your code on a simple shell script of my own making and it ran without error. – jwvh Oct 25 '18 at 06:45
  • 1 - Your `script.sh` isn't a shell script because `/bin` isn't a proper interpreter for the rest of the file (i.e. it's not a shell). 2 - Your posted Scala code can't work because you define `p`, but don't use it, and `command` is used but not defined. 3 - Are you trying to invoke `script.sh` or `/bin/prod_pull.sh`? 4 - Are you running this on a windows machine? Do you need that extra "pandering" code? – jwvh Oct 25 '18 at 19:17

1 Answers1

2

Assuming Linux is used, one can start with a simple "pwd"!, which will display the working directory, and then invoke the shell script using relative or absolute path. E.g.:

import sys.process._

object POC extends App{
  val path = "pwd".!!.trim
  println(path)
  s"$path/src/main/bin/test.sh".!
  "src/main/bin/test.sh".!
}

returns:

/home/user/temp/scala-shell-script
Hello shell
Hello shell

BTW, shell scripts usually have #!/bin/sh (not #!/bin) in the shebang line:

#!/bin/sh
echo "Hello shell"
jihor
  • 2,478
  • 14
  • 28
  • Thanks for your reply. I'm trying to run it on windows. Is there any fix? – Valli69 Oct 26 '18 at 05:51
  • @Valli69, you can't run a shell script on Windows out of the box because shell interpreter is missing. You'll have to either rewrite the script as a Windows batch file or install an interpreter (see e.g. https://stackoverflow.com/questions/26522789/how-to-run-sh-on-windows-command-prompt) – jihor Oct 26 '18 at 08:44