Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Ant exec task to run piped commands?

Tags:

I'm trying to run the following command using the 'exec' task in Ant:

ls -l /foo/bar | wc -l 

Currently, I have my exec looking like this:

<exec executable="ls" outputproperty="noOfFiles">     <arg value="-l" />     <arg value="/foo/bar" />     <arg value="|" />     <arg value="wc" />     <arg value="-l" /> </exec> 

The 'ls' command looks to be working but it's having a hard time piping the output to 'wc'. Any suggestions?

like image 762
Steve Griff Avatar asked Oct 14 '10 08:10

Steve Griff


People also ask

How do I call ant target from command line?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is an ant task?

Ant tasks are the units of your Ant build script that actually execute the build operations for your project. Ant tasks are usually embedded inside Ant targets. Thus, when you tell Ant to run a specific target it runs all Ant tasks nested inside that target.


1 Answers

If you use sh -c as Aaron suggests, you can pass the whole pipeline as a single arg, effectively doing:

sh -c "ls -l foo/bar | wc -l" 

If you use separate args, they are consumed by sh, not passed through to ls (hence you see just the current directory).

Note that on my system, ls -l includes a total as well as a list of the files found, which means the count shown is one more than the number of files. So suggest:

<exec executable="sh" outputproperty="noOfFiles">     <arg value="-c" />     <arg value="ls foo/bar | wc -l" /> </exec> 
like image 64
martin clayton Avatar answered Oct 01 '22 09:10

martin clayton