MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Run Shell Scripts from Apache Ant (and Set Unix File Permissions Safely)

This guide explains how to combine Apache Ant with Unix shell scripts for build tasks such as setting file permissions and processing files. It is aimed at engineers maintaining legacy Ant-based automation who need reliable, repeatable behavior in CI/CD environments.

Originally published on 2009-06-20; fully revised and updated.

Original post for reference: Ant Script with Shell script, How to run shell script from Ant Script?.

Ant and shell script integration

What this Ant + shell integration typically solves

  • Set execute permissions on scripts before running them.
  • Call shell utilities that are not available as native Ant tasks.
  • Run file cleanup or text normalization during builds.

Choose the right approach: native Ant task vs shell execution

Prefer native Ant tasks when they exist, and use shell commands through exec when you need OS-specific tooling.

ApproachBest forAdvantagesCaveats
Ant chmod taskSetting Unix permissions on files/directoriesPortable inside Ant builds; easy to readUnix-like environments only
Ant exec calling a script fileReusable, multi-step shell logicKeeps shell logic versioned and testableRequires shell/runtime on build agent
Ant exec with inline -c commandSmall one-linersFast to addHarder to maintain and quote safely

Execution flow when Ant runs shell commands

flowchart TD
    A[Ant target starts] --> B{Native Ant task available}
    B -- Yes --> C[Run built in Ant task]
    B -- No --> D[Use exec task]
    D --> E[Call shell script with args]
    E --> F[Script returns exit code]
    F --> G{Exit code is zero}
    G -- Yes --> H[Continue build]
    G -- No --> I[Fail build when failonerror is true]

Set file permissions directly in Ant

Use Ant’s chmod task to ensure scripts are executable before calling them.

<project name="example" default="fix-perms" basedir=".">
  <target name="fix-perms">
    <chmod perm="755">
      <fileset dir="${basedir}/scripts">
        <include name="**/*.sh"/>
      </fileset>
    </chmod>
  </target>
</project>Code language: HTML, XML (xml)

Run a shell script from Ant

Call a script file (recommended)

<target name="run-cleanup" depends="fix-perms">
  <exec executable="/bin/sh" failonerror="true">
    <arg value="${basedir}/scripts/cleanup-crlf.sh"/>
    <arg value="${basedir}/input"/>
  </exec>
</target>Code language: HTML, XML (xml)

Run a one-liner inline

<target name="list-text-files">
  <exec executable="/bin/sh" failonerror="true">
    <arg value="-c"/>
    <arg value="find '${basedir}/input' -type f -name '*.txt' -print"/>
  </exec>
</target>Code language: HTML, XML (xml)

Example cleanup script for carriage-return removal

The original post referenced removing special characters. The script below shows a safer pattern for removing carriage returns from text files and replacing files atomically.

#!/usr/bin/env bash
set -euo pipefail

root_dir="${1:-.}"

find "$root_dir" -type f -print0 | while IFS= read -r -d '' file; do
  tmp="${file}.tmp"
  tr -d '\r' < "$file" > "$tmp"
  mv "$tmp" "$file"
  echo "updated: $file"
doneCode language: PHP (php)
chmod +x scripts/cleanup-crlf.sh
ant run-cleanup

Best practices

  • Use failonerror="true" so shell failures stop the build immediately.
  • Keep complex shell logic in versioned .sh files, not inline command strings.
  • Apply cleanup only to known text files to avoid corrupting binaries.
  • Use Ant properties for paths instead of hardcoding directories.
  • Validate shell availability (/bin/sh or bash) on each CI runner image.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Junior Linux Administrator: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The **Junior Linux Administrator** supports the availability, security, and day-to-day operability of Linux-based infrastructure used by an enterprise IT organization to run internal services and business-critical applications. The role focuses on executing standard administration tasks, responding to tickets and alerts, performing routine maintenance, and following established runbooks under the guidance of senior administrators and SRE/Platform teams.

Read More

Interview Questions & Answers Complete Guide for Linux

Linux What is your experience with Linux? Explain what each of the following commands does and give an example on how to use it: Explain each field…

Read More

Linux Interview Questions and Answer – Part 7

What is the name and the UID of the administrator user? How to list all files, including hidden ones, in a directory? What is the Unix/Linux command…

Read More

Shell Bash Scripting: Assignment & Excercise – 16

This exercise asks for a Bash script that packs a file or directory into a compressed tar archive and then encrypts it with a passphrase. The solution…

Read More

Shell Bash Scripting: Assignment & Excercise – 15

This exercise builds a Bash script that takes an IP address, sends it to the free ip-api.com geolocation API with curl, and uses jq to print the…

Read More

Shell script to git add and commit and push each directory at a time

This page shows how to use a Bash script to stage, commit and push changes one directory at a time. That can mean a folder full of…

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month ago

Combining Ant with shell scripts can help automate builds and reduce repetitive manual work. It is important to pass arguments carefully and quote file paths, especially when they contain spaces. The script should also check Ant’s exit status and stop if a build step fails. Keeping environment settings separate from the build logic makes the workflow easier to reuse across machines. These practices make automation more reliable and simpler to troubleshoot.

Jason Mitchell
Jason Mitchell
4 months ago

When Ant and shell scripts are used together, the real complexity comes from maintaining consistent behavior across different environments. A script that works correctly on one machine may fail on another due to differences in operating systems, permissions, installed utilities, or environment configurations. These dependencies can become difficult to troubleshoot when automation grows and more teams rely on the same build process.
For long-term stability, teams should focus on making these workflows easier to manage by adding clear error handling, meaningful logs, and proper documentation. As organizations modernize their delivery processes, reviewing older script-based automation and improving its structure can help reduce maintenance effort and avoid unexpected failures in CI/CD pipelines.

2
0
Would love your thoughts, please comment.x
()
x