Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

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

Apache Ant Interview Questions Answered: Concepts, build.xml Examples, and Practical Snippets

This guide converts a long Apache Ant interview question list into clear, practical answers with working build.xml examples. It covers core Ant concepts, common tasks, debugging, and Ant vs Maven vs Make.

Read More

Apache Ant Comments and echo Task: Write Clear build.xml Files and Control Log Output

Learn the correct way to add comments in Apache Ant and when to use the echo task instead. This guide covers echo attributes, log-level behavior, and practical build.xml examples.

Read More

How to Run Ant Builds in IntelliJ IDEA: Setup, Troubleshooting, and Copy Task Patterns

This guide shows how to integrate a build.xml file with IntelliJ IDEA, run Ant targets from the IDE, and troubleshoot common environment-variable issues. It also compares practical Ant copy/fileset patterns with reusable examples.

Read More

Apache Ant clean, prepare, and compile Targets: A Practical build.xml Walkthrough

Learn how to structure a simple Ant build pipeline using clean, prepare, and compile targets with explicit dependencies. This revision includes a complete build.xml example, execution commands, and a workflow diagram.

Read More

Apache Ant Copy Task Examples: File, FileSet, Include/Exclude, and Flatten Patterns

Learn practical Apache Ant copy task patterns for real build pipelines, from single-file copies to filtered FileSets and flattened library bundles. Includes reusable XML snippets and a workflow diagram.

Read More

Apache Ant Dependency Visualization: AntScript Visualizer, Grand, and Vizant

A practical, updated guide to three classic Apache Ant visualization tools and how they help you understand target dependencies. Includes a side-by-side comparison, workflow diagram, and example commands.

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Jason Mitchell
Jason Mitchell
3 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.

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