Search code examples
scalalynx

Scala: executing external process, string escaping, and errors


I want to execute the following command:

lynx -useragent='Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1' -source 'http://localhost'

as a Process in Scala. I used both:

val cmd = ... // command to execute
cmd #> new java.io.File( filename ) !

and:

val proc = Process( cmd )                                                  
proc !!

And in both cases I get errors from lynx:

Can't Access `file://localhost/home/me/Work/23.lynx/2.multidownload/(X11;'
Alert!: Unable to access document.

The command run alone works well, when I run it in Scala — I get errors. Which means that something strange happens between Scala and lynx, and the strings are not interpreted correctly… Any idea?


Solution

  • I am guessing you set cmd to a String. !! then splits up the string at each space character to get the arguments to pass to lynx, not caring about your ' characters.

    import scala.sys.process._
    
    val cmd = Seq("lynx",
                  "-useragent='Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1'",
                  "-source" "http://localhost")
    val out = cmd.!!