Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple shell commands with wildcards

Tags:

shell

scala

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.

like image 777
Martin Berger Avatar asked Oct 22 '13 21:10

Martin Berger


People also ask

Do commands interpret wildcards?

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.

What is an * asterisk symbol in Linux?

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.


2 Answers

This should work fine

import scala.sys.process._    

"cd test".#&&("javac *.java").!

Equivalent to

"cd test" #&& "javac *.java" !
like image 25
maxmithun Avatar answered Sep 21 '22 22:09

maxmithun


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.

like image 152
Rex Kerr Avatar answered Sep 21 '22 22:09

Rex Kerr