mahesh
mahesh

Reputation: 274

Executing Shell Script through ant

I have a shell script named Call.sh which internally calls other scripts (i.e. .sh) and is working fine for me. Now i want to execute Call.sh from ant utility. I have made an build.xml which invokes the .sh. But one of the scripts asks for input, but ant doesn't give the chance to me to give the input due to which further operations fails. Please find below codes

Build.xml

<project name="Sample" default="info">
<target name="info">
<exec executable="/bin/bash">
<arg value="/full path/Call.sh"/>
<arg value="/tmp"/>
</exec>
</target>
</project>

Call.sh

    #!/bin/bash
    echo "Begining the execution......"
    sleep 1
    sh ./input.sh
    sh ./AutomateCom.sh
    sh ./Clean.sh
    echo "*****_______*****_______"

input.sh

    #!/bin/bash

    echo "enter the Organization name"
    read Orgname
    echo "Orgname is : $Orgname"
    sed "s/000/$Orgname/g" Final.sql >> ExecuteID.sql
    echo "Organization name has been replaced with $Orgname"

when i run the ant: It runs continously....below is the o/p when i say ant

[root@krog2-rhel5-64 Work]# ant
Buildfile: /root/test/Work/build.xml

info:
     [exec] enter the Organization name
     [exec] Orgname is :
     [exec] Organization name has been replaced with

BUILD SUCCESSFUL
Total time: 0 seconds
......................................

What i expect when i run ./input.sh,in same way ant should ask me for input

[root@krog2-rhel5-64 Work]# ./input.sh
enter the Organization name
**yak**
Orgname is : yak
Organization name has been replaced with yak
  However ant doesn't give me opportunity to prompt for the user input. Any suggestions.

Upvotes: 12

Views: 61417

Answers (3)

Imeksbank
Imeksbank

Reputation: 114

My idea was to generate the javadoc within Netbeans using it's "Run target -> default" ant script and after that with the same ant script (or even another one -xml) to upload to the server.

Actually the main goal should be to generate a javadoc and upload simultaneously the whole folder to the remote !!!I used the macOS 10.12.4 as client and Ubuntu 16.04 as a target.

I tried everything from belowe:

  • scp does not work really good because there is no credential
  • ftp with it's mput can not upload all folders recursively.
  • ncftpput won't be accepted from ant script

Then i decided to split into logical processes.

requires:

  1. brew install expect
  2. need login passwordless via ssh on your remote

ftp.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="ASMEMU" default="all" basedir="/bin" >
<target name="ftp">

    <exec dir="/Users/macos/NetBeansBash" executable="/bin/bash">
        <arg value="asmemu_mkdir.sh" />
        <arg value="ASMEMU" />
    </exec>

    <exec dir="/Users/macos/NetBeansBash" executable="/bin/bash">
        <arg value="asmemu_upload_zip.sh" />
        <arg value="ASMEMU" />
    </exec>

    <exec dir="/Users/macos/NetBeansBash" executable="/bin/bash">
        <arg value="asmemu_unzip.sh" />
        <arg value="ASMEMU" />
    </exec>

</target>
</project>

asmemu_mkdir.sh

#!/bin/bash
uhost="example.de"
uname="usernameusername"
upass="pwdpwd"
remote=/httpdocs/destination1/destination2
pwd
ftp -in <<EOF
open $uhost
user $uname $upass
cd $remote
mkdir $1
close
bye
EOF

asmemu_upload_zip.sh

#!/bin/bash
uhost="example.de"
uname="usernameusername"
upass="pwdpwd"
remote=/httpdocs/th/ra
cd /Users/macos/NetBeansProjects/$1/dist/
pwd
ftp -in <<EOF
open $uhost
user $uname $upass
cd $remote/$1
mput javadoc.zip
close
bye
EOF

asmemu_unzip.sh

ssh [email protected] /bin/bash <<EOT
cd /var/www/vhosts/example.de/httpdocs/th/ra/$1
unzip javadoc.zip
rm javadoc.zip
EOT

Thats it. Justt right click on ftp.xml "Run targets -> Other targets -> ftp" and the javadoc.zip will be uploaded and unzipped there.

Of course you need to create the javadoc.zip before you start.It is possible to build in the whole passage into build.xml

Worked for me 100% =)

Upvotes: 1

thekbb
thekbb

Reputation: 7924

I understand you got your question answered and have moved on. However I want to point out a few things for posterity. Why are you using ant? it seems you'd be better off with just a shell script.

Don't exec out to bash scripts. You didn't list the contents of all your scripts but call.sh and input.sh are trivial to do natively in ant. This will make your build script platform independent as well as consolidate the logging. You can handle your input directly from ant with the input task

<input
    message="Please enter organization name:"
    addproperty="org.name"
/>

However I'd strongly urge you to not have a build script that waits for user input. You can make the org.name a property and then simply specify it on the commandline when you build: ant -Dorg.name=yak

You needn't do a find replace on the sql file, you can use variables in the sql and pass them in when you execute it. (implementation will be db dependent)

It also hurts my soul that your examples are from a root shell. don't login as root.

Upvotes: 9

devnull
devnull

Reputation: 123648

Try specifying the complete path to the script in the ant target:

<target name="test">
  <exec executable="/bin/bash">
    <arg value="/complete/path/to/input.sh"/>
    <arg value="/tmp"/>
  </exec>
</target>

This is equivalent to issuing the following command in the shell:

/bin/bash /complete/path/to/input.sh /tmp

<arg value="..."/> denotes an argument. So you have 2 arguments to /bin/bash, path to script & /tmp. If your script doesn't make use of extra arguments passed to bin/bash, then those would be ignored.

If your script input.sh is located in /tmp, you can either say

  <exec executable="/bin/bash">
    <arg value="/tmp/input.sh"/>
  </exec>

or

  <exec dir="/tmp" executable="/bin/bash">
    <arg value="input.sh"/>
  </exec>

Upvotes: 24

Related Questions