Is there an easy way to do the following in Scala (or Java). I want to run command line commands from a Scala process, for example:
cd test && javac *.java
as a dynamically generated shell script. The javac *.java
should
happen in the directory test
. The usual simple
import scala.sys.process._
...
"cd test && javac *.java".!
or
"cd test && javac *.java".!!
don't work, because Scala misinterpretes the &&
and the wildcard *
. I have no
idea why.
Commands can use wildcards to perform actions on more than one file at a time, or to find part of a phrase in a text file. There are many uses for wildcards, there are two different major ways that wildcards are used, they are globbing patterns/standard wildcards that are often used by the shell.
The asterisk ( * ) The asterisk represents any number of unknown characters. Use it when searching for documents or files for which you have only partial names.
This should work fine
import scala.sys.process._
"cd test".#&&("javac *.java").!
Equivalent to
"cd test" #&& "javac *.java" !
For what you want, you should enter the string as a command-line argument to bash
. (That is, Process(Seq("bash","-c","cd test && javac *.java")).!
) The reason is that there is no virtual shell into which you're entering commands that will change state like cd
. You have to create one explicitly.
The process
tools will allow you to chain calls together, but the side-effects of the calls had better be reflected in the file system or somesuch, not in the shell environment. The ProcessBuilder
scaladoc contains an example at the end of the introductory text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With