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 2009/07/14 14:53:16 UTC

Excute shell command

Hi All,

I am stuck to shell command execution in my build.xml file, I want to execute the following command from my build.xml file:

KAR=`echo $var | awk -F. '{print $4}'`

Can anyone help me out in this?

Thanks in advance.

Re: Excute shell command

Posted by David Weintraub <qa...@gmail.com>.
I am not aware of the <shellscript> task being able to use the output
parameter property. It's certainly isn't documented. However, if it works,
it works.

The issues are still the same. You now are dependent on both Ant and the
AntContrib tasks. Many people install Ant, but fewer have the AntContrib
package installed too.

The other issue is a bit more important. You are parsing a file to get the
${act-dir}, and then trying to parse out a particular value from ${act-dir}.
I just feel there must be a more "Ant Way" of doing this without spawning a
large process just to retrieve a tiny piece of data.

If you don't mind using AntContrib, the following does the same thing
without having to spawn a new shell process:
*
    <property name="act-dir" value="one.two.three.four"/>

    <propertyregex
        input="${act-dir}"
        property="KAR"
        regexp="([^.]+\.){2}([^.]+)"
        global="true"
        select="\2"/>
    <echo>Output = "${KAR}"</echo>
*

On Thu, Jul 16, 2009 at 2:02 AM, <jh...@netscape.net> wrote:

>
> Thanks for reply and detail explanation, I have soemthing else with
> shellscript
> How about this:
> ?
> <shellscript shell="bash"
> ????????? inputstring="echo ${act-dir} | awk -F. '{print $2}'"
> ????????? outputproperty="value"/>
>
> <echo message="First: ${value}"/>
>
>
> I am ble to get the output what I want, like to know apart from becoming
> this code platform specific where else it can fail, because I am least
> bothered of platform as the things will always remain on Linux we are not
> going to move out from Linux.
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: David Weintraub <qa...@gmail.com>
> To: Ant Users List <us...@ant.apache.org>
> Sent: Thu, Jul 16, 2009 2:52 am
> Subject: Re: Excute shell command
>
>
>
> I just tested this:
>
> *    <property name="act-dir" value="one.two.three.four"/>
>    <exec executable="/bin/bash"
>        outputproperty="KAR">
>        <arg value='-c'/>
>        <arg value="echo ${act-dir} | awk -F. '{print $3}'"/>
>    </exec>
>    <echo>Output = ${KAR}</echo>*
>
> The problem is you can't use "arg line" because arg line parses each word
> as
> a separate argument. You need to make the whole command a single argument.
>
> The <shellscript> is not going to work because the variables are defined in
> a subshell that Ant calls, and their values disappear as soon as you return
> to your ant script.
>
> Still, there should be a way of doing this without using an exec or
> <shellscript> call.
>
> If the ${act-dir} line exists inside a file, you should be able to do a
> <loadproperty> task with a filter and a regexp to pull out the value you
> want. That way, it'll work on any system, and not just Unix boxes where
> /bin/bash is installed.
>
> On Wed, Jul 15, 2009 at 5:07 AM, <jh...@netscape.net> wrote:
>
> >
> > I combined xmltask and ant-contrib's shellscript like this
> >
> > <target name="readme-replace" depends="init">
> > ?????
> > ?? <copy file="${src.xml.path}/pom.xml" todir="${dest.xml.path}"/>
> > ?
> > ???? <!-- active-directory -->
> > ???? <xmltask source="${act.dir.path}/pom.xml">
> > ???????? <copy path="//:project/:version[1]/text()"
> property="active-dir"/>
> > ???? </xmltask>
> >
> > ???? <property name="act-dir" value="${active-dir}"/>
> > ??????????? <echo message="ACTIVE: ${act-dir}"/>
> >
> >
> > ???? <shellscript shell="bash">
> > ??????????????? KAR=`echo ${act-dir} | awk -F. '{print $1}'`
> > ??????????????? MAR=`echo ${act-dir} | awk -F. '{print $4}'`
> > ???? echo "KAR: $KAR"
> > ???? echo "MAR: $MAR"
> > ???? </shellscript>
> >
> > ??<echo message="First: ${KAR}"/>
> > ??<echo message="Fourth: ${MAR}"/>
> > </target>
> >
> > I am able to achieve what I want i.e. able to get values in variables
> "KAR"
> > and "MAR" but the problem here is I am unable to use variable "$KAR" and
> > "$MAR" outside of <shellscript> task.
> > you can see "echo" task outside shellscript
>  task is not able to get the
> > value of "KAR" and "MAR"
> > Can anyone give me an idea on how I can use "KAR" and "MAR" outside of
> > shellscript task?
> >
> >
> > -----Original Message-----
> > From: David Weintraub <qa...@gmail.com>
> > To: Ant Users List <us...@ant.apache.org>
> > Sent: Tue, Jul 14, 2009 11:20 pm
> > Subject: Re: Excute shell command
> >
> >
> >
> >
> > I could simply tell you to use the exec task:
> >
> > <exec executable="/bin/bash"
> >    outproperty="KAR">
> >    <arg line="echo $var | awk -F '{print $4}'"/>
> > </exec>
> >
> > However, there must be a better way to get this particular information:
> >
> > You can use <property env="ENV"/>, and that would put the value of $var
> > into
> > a property called "ENV.var". Now, to parse it a bit. That could be done
> > with
> > resources and token filtering. See <
> > http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
> > http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
> > more information.
> >
> > If ${var} came from a file that you're now attempting to parse, it should
> > be
> > fairly easy to setup a filter reader on a file, find the line, and pull
> out
> > the fourth value on that line. Otherwise, you'll be attempting to parse a
> > property, and that's a bit harder to setup.
> >
> > On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:
> >
> > > Hi All,
> > >
> > > I am stuck to shell command execution in my build.xml file, I want to
> > > execute the following command from my build.xml file:
> > >
> > > KAR=`echo $var | awk -F. '{print $4}'`
> > >
> > > Can anyone help me out in this?
> > >
> > > Thanks in advance.
> > >
> >
> >
> >
> > --
> > David Weintraub
> > qazwart@gmail.com
> >
> >
>
>
> --
> David Weintraub
> qazwart@gmail.com
>
>


-- 
David Weintraub
qazwart@gmail.com

Re: Excute shell command

Posted by jh...@netscape.net.
Thanks for reply and detail explanation, I have soemthing else with shellscript
How about this:
?
<shellscript shell="bash"
????????? inputstring="echo ${act-dir} | awk -F. '{print $2}'"
????????? outputproperty="value"/>

<echo message="First: ${value}"/>


I am ble to get the output what I want, like to know apart from becoming this code platform specific where else it can fail, because I am least bothered of platform as the things will always remain on Linux we are not going to move out from Linux.









-----Original Message-----
From: David Weintraub <qa...@gmail.com>
To: Ant Users List <us...@ant.apache.org>
Sent: Thu, Jul 16, 2009 2:52 am
Subject: Re: Excute shell command



I just tested this:

*    <property name="act-dir" value="one.two.three.four"/>
    <exec executable="/bin/bash"
        outputproperty="KAR">
        <arg value='-c'/>
        <arg value="echo ${act-dir} | awk -F. '{print $3}'"/>
    </exec>
    <echo>Output = ${KAR}</echo>*

The problem is you can't use "arg line" because arg line parses each word as
a separate argument. You need to make the whole command a single argument.

The <shellscript> is not going to work because the variables are defined in
a subshell that Ant calls, and their values disappear as soon as you return
to your ant script.

Still, there should be a way of doing this without using an exec or
<shellscript> call.

If the ${act-dir} line exists inside a file, you should be able to do a
<loadproperty> task with a filter and a regexp to pull out the value you
want. That way, it'll work on any system, and not just Unix boxes where
/bin/bash is installed.

On Wed, Jul 15, 2009 at 5:07 AM, <jh...@netscape.net> wrote:

>
> I combined xmltask and ant-contrib's shellscript like this
>
> <target name="readme-replace" depends="init">
> ?????
> ?? <copy file="${src.xml.path}/pom.xml" todir="${dest.xml.path}"/>
> ?
> ???? <!-- active-directory -->
> ???? <xmltask source="${act.dir.path}/pom.xml">
> ???????? <copy path="//:project/:version[1]/text()" property="active-dir"/>
> ???? </xmltask>
>
> ???? <property name="act-dir" value="${active-dir}"/>
> ??????????? <echo message="ACTIVE: ${act-dir}"/>
>
>
> ???? <shellscript shell="bash">
> ??????????????? KAR=`echo ${act-dir} | awk -F. '{print $1}'`
> ??????????????? MAR=`echo ${act-dir} | awk -F. '{print $4}'`
> ???? echo "KAR: $KAR"
> ???? echo "MAR: $MAR"
> ???? </shellscript>
>
> ??<echo message="First: ${KAR}"/>
> ??<echo message="Fourth: ${MAR}"/>
> </target>
>
> I am able to achieve what I want i.e. able to get values in variables "KAR"
> and "MAR" but the problem here is I am unable to use variable "$KAR" and
> "$MAR" outside of <shellscript> task.
> you can see "echo" task outside shellscript
 task is not able to get the
> value of "KAR" and "MAR"
> Can anyone give me an idea on how I can use "KAR" and "MAR" outside of
> shellscript task?
>
>
> -----Original Message-----
> From: David Weintraub <qa...@gmail.com>
> To: Ant Users List <us...@ant.apache.org>
> Sent: Tue, Jul 14, 2009 11:20 pm
> Subject: Re: Excute shell command
>
>
>
>
> I could simply tell you to use the exec task:
>
> <exec executable="/bin/bash"
>    outproperty="KAR">
>    <arg line="echo $var | awk -F '{print $4}'"/>
> </exec>
>
> However, there must be a better way to get this particular information:
>
> You can use <property env="ENV"/>, and that would put the value of $var
> into
> a property called "ENV.var". Now, to parse it a bit. That could be done
> with
> resources and token filtering. See <
> http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
> http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
> more information.
>
> If ${var} came from a file that you're now attempting to parse, it should
> be
> fairly easy to setup a filter reader on a file, find the line, and pull out
> the fourth value on that line. Otherwise, you'll be attempting to parse a
> property, and that's a bit harder to setup.
>
> On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:
>
> > Hi All,
> >
> > I am stuck to shell command execution in my build.xml file, I want to
> > execute the following command from my build.xml file:
> >
> > KAR=`echo $var | awk -F. '{print $4}'`
> >
> > Can anyone help me out in this?
> >
> > Thanks in advance.
> >
>
>
>
> --
> David Weintraub
> qazwart@gmail.com
>
>


-- 
David Weintraub
qazwart@gmail.com


Re: Excute shell command

Posted by David Weintraub <qa...@gmail.com>.
I just tested this:

*    <property name="act-dir" value="one.two.three.four"/>
    <exec executable="/bin/bash"
        outputproperty="KAR">
        <arg value='-c'/>
        <arg value="echo ${act-dir} | awk -F. '{print $3}'"/>
    </exec>
    <echo>Output = ${KAR}</echo>*

The problem is you can't use "arg line" because arg line parses each word as
a separate argument. You need to make the whole command a single argument.

The <shellscript> is not going to work because the variables are defined in
a subshell that Ant calls, and their values disappear as soon as you return
to your ant script.

Still, there should be a way of doing this without using an exec or
<shellscript> call.

If the ${act-dir} line exists inside a file, you should be able to do a
<loadproperty> task with a filter and a regexp to pull out the value you
want. That way, it'll work on any system, and not just Unix boxes where
/bin/bash is installed.

On Wed, Jul 15, 2009 at 5:07 AM, <jh...@netscape.net> wrote:

>
> I combined xmltask and ant-contrib's shellscript like this
>
> <target name="readme-replace" depends="init">
> ?????
> ?? <copy file="${src.xml.path}/pom.xml" todir="${dest.xml.path}"/>
> ?
> ???? <!-- active-directory -->
> ???? <xmltask source="${act.dir.path}/pom.xml">
> ???????? <copy path="//:project/:version[1]/text()" property="active-dir"/>
> ???? </xmltask>
>
> ???? <property name="act-dir" value="${active-dir}"/>
> ??????????? <echo message="ACTIVE: ${act-dir}"/>
>
>
> ???? <shellscript shell="bash">
> ??????????????? KAR=`echo ${act-dir} | awk -F. '{print $1}'`
> ??????????????? MAR=`echo ${act-dir} | awk -F. '{print $4}'`
> ???? echo "KAR: $KAR"
> ???? echo "MAR: $MAR"
> ???? </shellscript>
>
> ??<echo message="First: ${KAR}"/>
> ??<echo message="Fourth: ${MAR}"/>
> </target>
>
> I am able to achieve what I want i.e. able to get values in variables "KAR"
> and "MAR" but the problem here is I am unable to use variable "$KAR" and
> "$MAR" outside of <shellscript> task.
> you can see "echo" task outside shellscript task is not able to get the
> value of "KAR" and "MAR"
> Can anyone give me an idea on how I can use "KAR" and "MAR" outside of
> shellscript task?
>
>
> -----Original Message-----
> From: David Weintraub <qa...@gmail.com>
> To: Ant Users List <us...@ant.apache.org>
> Sent: Tue, Jul 14, 2009 11:20 pm
> Subject: Re: Excute shell command
>
>
>
>
> I could simply tell you to use the exec task:
>
> <exec executable="/bin/bash"
>    outproperty="KAR">
>    <arg line="echo $var | awk -F '{print $4}'"/>
> </exec>
>
> However, there must be a better way to get this particular information:
>
> You can use <property env="ENV"/>, and that would put the value of $var
> into
> a property called "ENV.var". Now, to parse it a bit. That could be done
> with
> resources and token filtering. See <
> http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
> http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
> more information.
>
> If ${var} came from a file that you're now attempting to parse, it should
> be
> fairly easy to setup a filter reader on a file, find the line, and pull out
> the fourth value on that line. Otherwise, you'll be attempting to parse a
> property, and that's a bit harder to setup.
>
> On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:
>
> > Hi All,
> >
> > I am stuck to shell command execution in my build.xml file, I want to
> > execute the following command from my build.xml file:
> >
> > KAR=`echo $var | awk -F. '{print $4}'`
> >
> > Can anyone help me out in this?
> >
> > Thanks in advance.
> >
>
>
>
> --
> David Weintraub
> qazwart@gmail.com
>
>


-- 
David Weintraub
qazwart@gmail.com

Re: Excute shell command

Posted by jh...@netscape.net.

I combined xmltask and ant-contrib's shellscript like this

<target name="readme-replace" depends="init">
 
 <copy file="${src.xml.path}/pom.xml" todir="${dest.xml.path}"/>

  <!-- active-directory -->
  <xmltask source="${act.dir.path}/pom.xml">
   <copy path="//:project/:version[1]/text()" property="active-dir"/>
  </xmltask>

  <property name="act-dir" value="${active-dir}"/>
      <echo message="ACTIVE: ${act-dir}"/>


   <shellscript shell="bash">
                KAR=`echo ${act-dir} | awk -F. '{print $1}'`
                MAR=`echo ${act-dir} | awk -F. '{print $4}'`
     echo "KAR: $KAR"
     echo "MAR: $MAR"
    </shellscript>

  <echo message="First: ${KAR}"/>
  <echo message="Fourth: ${MAR}"/>
</target>

I am able to achieve what I want i.e. able to get values in variables "KAR" and 
"MAR" but the problem here is I am unable to use variable "$KAR" and "$MAR" 
outside of <shellscript> task.
you can see "echo" task outside shellscript task is not able to get the value of 
"KAR" and "MAR"
Can anyone give me an idea on how I can use "KAR" and "MAR" outside of 
shellscript task?


-----Original Message-----
From: David Weintraub <qa...@gmail.com>
To: Ant Users List <us...@ant.apache.org>
Sent: Tue, Jul 14, 2009 11:20 pm
Subject: Re: Excute shell command




I could simply tell you to use the exec task:

<exec executable="/bin/bash"
    outproperty="KAR">
    <arg line="echo $var | awk -F '{print $4}'"/>
</exec>

However, there must be a better way to get this particular information:

You can use <property env="ENV"/>, and that would put the value of $var into
a property called "ENV.var". Now, to parse it a bit. That could be done with
resources and token filtering. See <
http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
more information.

If ${var} came from a file that you're now attempting to parse, it should be
fairly easy to setup a filter reader on a file, find the line, and pull out
the fourth value on that line. Oth
erwise, you'll be attempting to parse a
property, and that's a bit harder to setup.

On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:

> Hi All,
>
> I am stuck to shell command execution in my build.xml file, I want to
> execute the following command from my build.xml file:
>
> KAR=`echo $var | awk -F. '{print $4}'`
>
> Can anyone help me out in this?
>
> Thanks in advance.
>



-- 
David Weintraub
qazwart@gmail.com



Re: Excute shell command

Posted by jh...@netscape.net.
I combined xmltask and ant-contrib's shellscript like this

<target name="readme-replace" depends="init">
????? 
?? <copy file="${src.xml.path}/pom.xml" todir="${dest.xml.path}"/>
?
???? <!-- active-directory -->
???? <xmltask source="${act.dir.path}/pom.xml">
???????? <copy path="//:project/:version[1]/text()" property="active-dir"/>
???? </xmltask>

???? <property name="act-dir" value="${active-dir}"/>
??????????? <echo message="ACTIVE: ${act-dir}"/>


???? <shellscript shell="bash">
??????????????? KAR=`echo ${act-dir} | awk -F. '{print $1}'`
??????????????? MAR=`echo ${act-dir} | awk -F. '{print $4}'`
???? echo "KAR: $KAR"
???? echo "MAR: $MAR"
???? </shellscript>

??<echo message="First: ${KAR}"/>
??<echo message="Fourth: ${MAR}"/>
</target>

I am able to achieve what I want i.e. able to get values in variables "KAR" and "MAR" but the problem here is I am unable to use variable "$KAR" and "$MAR" outside of <shellscript> task.
you can see "echo" task outside shellscript task is not able to get the value of "KAR" and "MAR"
Can anyone give me an idea on how I can use "KAR" and "MAR" outside of shellscript task?


-----Original Message-----
From: David Weintraub <qa...@gmail.com>
To: Ant Users List <us...@ant.apache.org>
Sent: Tue, Jul 14, 2009 11:20 pm
Subject: Re: Excute shell command




I could simply tell you to use the exec task:

<exec executable="/bin/bash"
    outproperty="KAR">
    <arg line="echo $var | awk -F '{print $4}'"/>
</exec>

However, there must be a better way to get this particular information:

You can use <property env="ENV"/>, and that would put the value of $var into
a property called "ENV.var". Now, to parse it a bit. That could be done with
resources and token filtering. See <
http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
more information.

If ${var} came from a file that you're now attempting to parse, it should be
fairly easy to setup a filter reader on a file, find the line, and pull out
the fourth value on that line. Otherwise, you'll be attempting to parse a
property, and that's a bit harder to setup.

On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:

> Hi All,
>
> I am stuck to shell command execution in my build.xml file, I want to
> execute the following command from my build.xml file:
>
> KAR=`echo $var | awk -F. '{print $4}'`
>
> Can anyone help me out in this?
>
> Thanks in advance.
>



-- 
David Weintraub
qazwart@gmail.com


Re: Excute shell command

Posted by David Weintraub <qa...@gmail.com>.
I could simply tell you to use the exec task:

<exec executable="/bin/bash"
    outproperty="KAR">
    <arg line="echo $var | awk -F '{print $4}'"/>
</exec>

However, there must be a better way to get this particular information:

You can use <property env="ENV"/>, and that would put the value of $var into
a property called "ENV.var". Now, to parse it a bit. That could be done with
resources and token filtering. See <
http://ant.apache.org/manual/CoreTypes/filterchain.html> and <
http://ant.apache.org/manual/CoreTypes/filterchain.html#tokenfilter> for
more information.

If ${var} came from a file that you're now attempting to parse, it should be
fairly easy to setup a filter reader on a file, find the line, and pull out
the fourth value on that line. Otherwise, you'll be attempting to parse a
property, and that's a bit harder to setup.

On Tue, Jul 14, 2009 at 8:53 AM, <jh...@netscape.net> wrote:

> Hi All,
>
> I am stuck to shell command execution in my build.xml file, I want to
> execute the following command from my build.xml file:
>
> KAR=`echo $var | awk -F. '{print $4}'`
>
> Can anyone help me out in this?
>
> Thanks in advance.
>



-- 
David Weintraub
qazwart@gmail.com