Reputation: 61
I would like to be able to execute a nested shell command. For example;
final String cmd = 'for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done'
I had tried the following syntax but wasn't able to get it running.
def result = cmd.execute();
def result = ['sh', '-c', cmd].execute();
def result = ('sh -c for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done').execute()
I would appreciate the help here.
Upvotes: 3
Views: 10587
Reputation: 61
Thanks for all the help. This is a great community. I was able to get this working after passing in the environment and working directory information.
def wdir = new File( "./", module ).getAbsoluteFile() ;
def env = System.getenv();
def envlist = [];
env.each() { k,v -> envlist.push( "$k=$v" ) }
final String cmd = 'for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done'
proc = ["bash", "-c", cmd].execute(envlist , wdir);
Upvotes: 3
Reputation: 171084
This should work:
def cmd = [
'bash',
'-c',
'''for i in pom.xml projects.xml
|do
| find . -name $i | while read fname
| do
| echo $fname
| done
|done'''.stripMargin() ]
println cmd.execute().text
(I've formatted the command text so it looks better here, you could keep it all in one line)
I also believe your command could be replaced by:
find . -name pom.xml -o -name projects.xml -print
Or, in Groovy:
def files = []
new File( '.' ).traverse() {
if( it.name in [ 'pom.xml', 'projects.xml' ] ) {
files << it
}
}
println files
Upvotes: 7