You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by jh...@netscape.net on 2010/11/24 10:48:36 UTC

Using shell command in ANT script


Hi All,

I want to use the following shell command in my build.xml file
 
ls -l ../../../jobs/ | awk '{ print $9 }' > content.txt
 
is there any way I can use the above mentioned command.

Can anyone help me out in this regard?

Thanks,
Saha



Re: Using shell command in ANT script

Posted by David Weintraub <qa...@gmail.com>.
On Mon, Nov 29, 2010 at 1:22 AM,  <jh...@netscape.net> wrote:
> The shell command which I wanted to incorporate on my ANT script is only a part of lengthy
> command in actual I wanted to write the content of a folder in a file with “/*” suffix and “jobs/”
>  as prefix to each entry, so say a directory contains following folders
> Abc
> Def
> Ghi
> Jkl
> Above contents in my txt file should look like this:
> Jobs/Abc/*
> Jobs/Def/*
> Jobs/Ghi/*
> Jobs/Jkl/*
> And so on

Ah! You should look into "Mappers". A mapper can take a list of file
names, and convert them into other names. This can be used as part of
the copy process, or in selecting folders for a fileset.

Take a look at the <globmapper> at
<http://ant.apache.org/manual/Types/mapper.html> and see if that does
what you need. You could use the <regexmapper> which allows for more
powerful name transformations, but the <globmapper> is easier to use
and is good enough for 90% of this type of situations. (And look,
there's an example of using a Mapper in a <pathconvert> task!)

Using "Mappers" is a bit esoteric and most Ant users aren't experts at
them. And, most users would include me personally. I usually have to
futz with them for a while to get them to do what I want, and I have
to do a lot of heavy referencing with the on line manual. If you play
around with "Mappers" enough in order to get them to work, you'll be
one of the experts here and can help answer my questions.

If you need any help, please feel free to post here, and let us know
what you finally were able to do. It'll be a learning experience for
all of us on this list.

-- 
David Weintraub
qazwart@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by jh...@netscape.net.
Thanks to all of you for taking interest in my query.
I myself is in favor of solution which can be platform independent but was clueless on how to achieve this in ANT, the shell command which I wanted to incorporate on my ANT script is only a part of lengthy command in actual I wanted to write the content of a folder in a file with “/*” suffix and “jobs/” as prefix to each entry, so say a directory contains following folders
Abc
Def
Ghi
Jkl
Above contents in my txt file should look like this:
Jobs/Abc/*
Jobs/Def/*
Jobs/Ghi/*
Jobs/Jkl/*
And so on
So to achieve the above output I thought shell script is the best and easiest way to achieve this and which I can make a call through my ANT script, but if I get the solution in pure ant form then it will be great. Any thoughts?







-----Original Message-----
From: David Weintraub <qa...@gmail.com>
To: Ant Users List <us...@ant.apache.org>
Sent: Thu, Nov 25, 2010 1:46 am
Subject: Re: Using shell command in ANT script


On Wed, Nov 24, 2010 at 12:33 PM, Ludwig, Michael
Michael.Ludwig@delphi-mb.de> wrote:
 It's <fixcrlf>, not <fixcrnl>.

 And you could also use <pathconvert>:
Thanks for that tip of <pathconvert>. You can also use the
flattenmapper/> to remove directory names if you so choose:
        <fileset dir="../../../jobs" id="dir">
           <include name="*"/>
       </fileset>
       <pathconvert refid="dir"
           property="dir.prop"
           pathsep="${line.separator}">
           <flattenmapper/>
       </pathconvert>
       <echo message="${dir.prop}" file="foo"/>
That should be able to replace the <exec> task.
-- 
avid Weintraub
azwart@gmail.com
---------------------------------------------------------------------
o unsubscribe, e-mail: user-unsubscribe@ant.apache.org
or additional commands, e-mail: user-help@ant.apache.org



RE: Using shell command in ANT script

Posted by "Ludwig, Michael" <Mi...@delphi-mb.de>.
> Thanks for that tip of <pathconvert>. You can also use the
> <flattenmapper/> to remove directory names if you so choose:

Or even (starting with 1.8.2 or building from trunk) the newly introduced <cutdirsmapper>! Lots of options ...

http://ant.apache.org/manual/Types/mapper.html

Michael

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by David Weintraub <qa...@gmail.com>.
On Wed, Nov 24, 2010 at 12:33 PM, Ludwig, Michael
<Mi...@delphi-mb.de> wrote:
> It's <fixcrlf>, not <fixcrnl>.
>
> And you could also use <pathconvert>:

Thanks for that tip of <pathconvert>. You can also use the
<flattenmapper/> to remove directory names if you so choose:

        <fileset dir="../../../jobs" id="dir">
            <include name="*"/>
        </fileset>
        <pathconvert refid="dir"
            property="dir.prop"
            pathsep="${line.separator}">
            <flattenmapper/>
        </pathconvert>
        <echo message="${dir.prop}" file="foo"/>

That should be able to replace the <exec> task.

-- 
David Weintraub
qazwart@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Using shell command in ANT script

Posted by "Ludwig, Michael" <Mi...@delphi-mb.de>.
>         <fileset dir=".." id="dir">
>             <include name="*"/>
>         </fileset>
>         <property name="dir.prop" refid="dir"/>
>         <echo message="${dir.prop}" file="foo"/>
>         <replace file="foo" token=";" value="&#x000A;"/>
>         <fixCRNL file="foo"/>

It's <fixcrlf>, not <fixcrnl>.

And you could also use <pathconvert>:

<pathconvert refid="my.dir" pathsep="${line.separator}"
  property="out.prop"/>
<echo message="${out.prop}"/>

You'll probably get absolute pathnames then.
(At least that's what I get.)

Michael

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by David Weintraub <qa...@gmail.com>.
> 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;"/>
        </exec>

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="*"/>
        </fileset>
        <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
environment.

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
qazwart@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by Sony Antony <so...@gmail.com>.
Put the command inside a shell script and exec the shell script from
your build.xml
--sony

On Wednesday, November 24, 2010, Tim Visher <ti...@gmail.com> wrote:
> On Wed, Nov 24, 2010 at 4:48 AM,  <jh...@netscape.net> wrote:
>> I want to use the following shell command in my build.xml file
>>
>> ls -l ../../../jobs/ | awk '{ print $9 }' > content.txt
>>
>> is there any way I can use the above mentioned command.
>
> Read up on [Exec](http://ant.apache.org/manual/Tasks/exec.html).
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by Tim Visher <ti...@gmail.com>.
On Wed, Nov 24, 2010 at 4:48 AM,  <jh...@netscape.net> wrote:
> I want to use the following shell command in my build.xml file
>
> ls -l ../../../jobs/ | awk '{ print $9 }' > content.txt
>
> is there any way I can use the above mentioned command.

Read up on [Exec](http://ant.apache.org/manual/Tasks/exec.html).

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Using shell command in ANT script

Posted by Dominique Devienne <dd...@gmail.com>.
On Wed, Nov 24, 2010 at 3:48 AM,  <jh...@netscape.net> wrote:
> I want to use the following shell command in my build.xml file
> ls -l ../../../jobs/ | awk '{ print $9 }' > content.txt

AFAIK, the 9th field of ls -l is the filename, so your "ls -l path |
awk '{ print $9 }'" is better replaced with simply "ls -1".

Then this becomes (untested):

<exec executable="ls" outputproperty="content.txt">
  <arg value="-1" />
  <arg file="../../../jobs" />
</exec>

But I'm sure there's a way to do it portably in pure Ant too, without
forking to ls. --DD

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org