views:

26

answers:

2

Hello,

I'm trying to run the following command using exec 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?

+1  A: 

You need someone to recognize and build the pipe. Try sh:

<exec executable="sh" outputproperty="noOfFiles">
    <arg value="-c" />
    <arg value="ls" />
    <arg value="-l" />
    <arg value="/foo/bar" />
    <arg value="|" />
    <arg value="wc" />
    <arg value="-l" />
</exec>
Aaron Digulla
Hi Aaron - It's almost working! It seems to be ignoring the /foo/bar directory and just counting the amount of files in the current directory you are in on bash.
Steve Griff
Try `/bin/ls -1` (one, not i or L) and always use an absolute path.
Aaron Digulla
+2  A: 

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>
martin clayton
Thanks Martin, this solves the issue I was facing. Thanks Aaron too.
Steve Griff