ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Weintraub <>
Subject Re: Using shell command in ANT script
Date Wed, 24 Nov 2010 16:48:30 GMT
> Hi All,
> I want to use the following shell command in my build.xml file
> ls -l ../../../jobs/ | awk '{ print $9 }' > content.txt

The problem is that your particular command requires two shells. When
you pipe the output of the "ls" command to the "grep" command, you
literally need two executables.

The easiest way to handle this is to create a shell script, and then
call that shell script. You might have to put a shebang on the first
line (#! /bin/sh). But, it should generally work.

You can use the <exec> task, but, because you're doing a pipe, you
have to use "/bin/sh" with the "-c" parameter as your command and not
"/bin/ls". If you put "/bin/ls", the <exec> task calls "/bin/ls"
directly. The problem is that "/bin/ls" cannot spawn a pipe. That's
the shell's responsibility.

Here's what I have:

        <exec executable="ksh">
            <arg value="-c"/>
            <arg line="&quot;ls -la .. | awk '{print $9}' &quot;"/>

By the way, is that exactly what you want done, or were you simply
asking how can your exec a command with a pipe. If you really want
that, you can use Ant tasks:

        <fileset dir=".." id="dir">
            <include name="*"/>
        <property name="dir.prop" refid="dir"/>
        <echo message="${dir.prop}" file="foo"/>
        <replace file="foo" token=";" value="&#x000A;"/>
        <fixCRNL file="foo"/>

The <fileset> selects the files you want and puts them in the
reference ID "dir". I can then use <properties> to turn that fileset
list into a property. Then, I can use <echo> to print that property
into a file.

Once it's in a file, I need to change the fileset separators from ";"
to "\n". The <fixCRNL> should fix the line endings to fit your

This is probably faster and more universal since it doesn't depend
upon external commands and spawning two separate shells.

By the way, if you prefer the <exec> command, use "ls -1" (with a one)
instead of "ls -l" (with an el). The "-1" (one) parameter  will list
the files in a single column downward, and you don't have to pipe your
command through "awk" just to pull off the file names.

David Weintraub

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message