0

We have a shell script which we invoke it from an ant script. We need to pass input to shell script from ant script. It can be done as follows

<target name="help">
  <exec executable="cmd">
    <arg value="Hello"/>
    <arg value="Welcome"/>
    <arg value="World"/>
  </exec>
</target>

But We are not able to figure out how to access the values passed from ant script in shell script. Could anyone please suggest me with the right information.Thanks.

4
  • Just like any other script variables? E.g. %1, %2 and so on - I assume you use Windows batch scripts. Commented Jul 23, 2015 at 6:14
  • Iam on Linux Machine and it is a sh file. Commented Jul 23, 2015 at 6:46
  • Then do it like $1, $2 and so on. Commented Jul 23, 2015 at 7:08
  • I followed the way you suggested but iam not able to access the values. Commented Jul 23, 2015 at 7:43

1 Answer 1

2

Use properties as input, something like :

<project>
 <property name="foobar" location="C:/foobar" />
 <property name="foo" value="bar" />

 <exec executable="cmd">
   <arg value="/c" />
   <env key="PATH" path="${env.PATH};${foobar}/bin" />
   <arg value="set" />
 </exec>

 <exec executable="cmd">
  <arg value="/c" />
  <arg value="echo" />
  <arg value="${foo}" />
 </exec>
</project> 

You have to use /c as first arg value.
When calling a batfile which expects %1 ... %9 as input, first arg is <arg value=/c">,
second arg <arg value="yourbatfile.bat/>.
The following args <arg value=.../> would be %1 and so on, f.e. :

foobar.bat

@echo off
echo First argument %1
echo Second argument %2

build.xml

<project>

<exec dir="dir="path/to/batfile" executable="cmd">
 <arg value="/c"/>
 <arg value="foobar.bat"/>
 <arg value="foo"/>
 <arg value="bar"/>
</exec>

</project>

output

[exec] First argument foo
[exec] Second argument bar

Example for calling a shellscript, first arg has to be <arg value="/path/to/shellscript.sh"/>, the following args <arg value="..."/> will be $1 ...

foobar.sh

#!/bin/bash

echo "\$# = $#"
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"

build.xml

<project>
 <exec executable="/bin/bash">
  <arg value="/path/to/foobar.sh"/>
  <arg value="foo"/>
  <arg value="bar"/>
 </exec>
</project>
Sign up to request clarification or add additional context in comments.

6 Comments

Iam not using a bat file iam using an sh file. Is it going to have any impact?Do I have to change anything from what you have suggested?
Iam not able to clearly understand your example. Iam pasting my code below please have a look <exec executable="status.sh" outputproperty="output.value" resultproperty="exec.result" failonerror="false"> <arg value="${mbean.name}" /> <arg value="55011" /> </exec>
Sorry for reposting please find the code below build.xml <exec executable="status.sh" outputproperty="output.value" resultproperty="exec.result" failonerror="false"> <arg value="55011" /> </exec> status.sh PORT= $1
@Sunayana Look at the example from Rebse. It's <exec executable="/bin/bash">, not <exec executable="status.sh" ...>. To have Bash execute status.sh, insert <arg value="status.sh"/> under <exec>.
@Chad I need to also specify the location of the status.sh file ,so where do i do it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.