Resolving Bash Script Execution Error: In-depth Analysis of Exit Code 126 and CPD Integration in iOS Projects

Dec 03, 2025 · Programming · 15 views · 7.8

Keywords: Bash script | exit code 126 | CPD integration | iOS development | Xcode build | permission management | environment variable debugging

Abstract: This article provides an in-depth analysis of the Bash script execution error (exit code 126) encountered when integrating CPD (Copy-Paste Detection) tools in iOS development. By dissecting the original script issues, exploring permission and executability checks, and offering corrected solutions based on best practices, it details how to configure run script phases in Xcode for automated code duplication detection. The content covers environment variable debugging, file permission management, and script optimization strategies to help developers avoid common pitfalls and enhance build process reliability.

Problem Background and Error Analysis

In iOS development, integrating CPD (Copy-Paste Detection) tools for automated code duplication detection is a common practice. However, developers often encounter exit code 126 errors when executing Bash scripts for CPD, leading to build failures. Exit code 126 in Unix-like systems typically indicates "command not executable," pointing to permission or path issues with the script or related files.

Analysis of the Original Script Issues

The core part of the original script is as follows:

echo "Checking files in ${SOURCE_ROOT}"
JARS_DIR=${PROJECT_DIR}/CPD
FULL_PATH_TO_CPD_XML_OUTPUT=${PROJECT_DIR}/cpd-output.xml

# Running CPD
java -classpath "${JARS_DIR}/ObjCLanguage-0.0.5-SNAPSHOT.jar:${JARS_DIR}/pmd.jar" net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files "${SOURCE_ROOT}" -v --language ObjectiveC --encoding UTF-8 --format net.sourceforge.pmd.cpd.XMLRenderer > "${FULL_PATH_TO_CPD_XML_OUTPUT}"

# Running self :)
${BUILT_PRODUCTS_DIR} -cpd-xml "${FULL_PATH_TO_CPD_XML_OUTPUT}"

The error occurs in the last line: ${BUILT_PRODUCTS_DIR} -cpd-xml "${FULL_PATH_TO_CPD_XML_OUTPUT}". Here, ${BUILT_PRODUCTS_DIR} is an environment variable that typically points to the Xcode build products directory, but it is not an executable file itself. Attempting to execute it as a command results in exit code 126, as the system cannot recognize it as a valid executable program.

Solutions and Best Practices

Based on community feedback and the best answer, the corrected script should include the following key improvements:

  1. Environment Variable Debugging: Add debug outputs to verify paths and file existence, such as using echo [DEBUG] statements.
  2. Executable File Check: Explicitly specify the path to the CPD executable file, rather than relying on potentially incorrect environment variables. In the corrected script, use CPD_EXECUTABLE="${CPD_DIR}/CPDObjective-C" and check for file existence.
  3. Permission Management: Ensure that the script and related files have execution permissions. In Unix systems, use the chmod +x script.sh command to add permissions, avoiding exit code 126 due to insufficient permissions.

An example of the corrected script is as follows:

echo "Checking files in ${SOURCE_ROOT}"
CPD_DIR=${PROJECT_DIR}/CPD
JARS_DIR=${PROJECT_DIR}/CPD
FULL_PATH_TO_CPD_XML_OUTPUT=${PROJECT_DIR}/cpd-output.xml
OBJC_JAR_LIBRARY=${JARS_DIR}/ObjCLanguage-0.0.5-SNAPSHOT.jar

echo [DEBUG] CPD_DIR = ${CPD_DIR}
echo [DEBUG] JARS_DIR = ${JARS_DIR}
echo [DEBUG] FULL_PATH_TO_CPD_XML_OUTPUT = ${FULL_PATH_TO_CPD_XML_OUTPUT}
echo [DEBUG] OBJC_JAR_LIBRARY = ${OBJC_JAR_LIBRARY}
echo [DEBUG] SOURCE_ROOT = ${SOURCE_ROOT}

# Running CPD
java -classpath "${OBJC_JAR_LIBRARY}:${JARS_DIR}/pmd.jar" net.sourceforge.pmd.cpd.CPD --minimum-tokens 200 --files "${SOURCE_ROOT}" -v --language ObjectiveC --encoding UTF-8 --format net.sourceforge.pmd.cpd.XMLRenderer > "${FULL_PATH_TO_CPD_XML_OUTPUT}"

CPD_EXECUTABLE="${CPD_DIR}/CPDObjective-C"
if [ ! -f "${CPD_EXECUTABLE}" ];
then
echo "CPD executable file is not found: " ${CPD_EXECUTABLE}
fi
echo "Running ${CPD_EXECUTABLE} -cpd-xml ${FULL_PATH_TO_CPD_XML_OUTPUT}"
"${CPD_EXECUTABLE}" -cpd-xml "${FULL_PATH_TO_CPD_XML_OUTPUT}"

This script avoids the original error by specifying explicit paths and adding checks, improving maintainability.

Integration into Xcode Build Process

In Xcode, integrate the CPD script by following these steps:

  1. Open the project and select the target.
  2. Go to the "Build Phases" tab.
  3. Click the "+" button to add a "New Run Script Phase."
  4. Paste the corrected Bash script into the script editor area.
  5. Ensure that script files and related resources (e.g., JAR files and executables) are in the correct directories and have appropriate permissions set.

This allows CPD to run automatically during each build, generating XML reports for code duplication analysis.

Conclusion and Recommendations

Exit code 126 errors often stem from permission issues or incorrect paths in scripts or files. When integrating third-party tools like CPD, it is recommended to:

Through systematic debugging and adherence to these guidelines, developers can effectively resolve such issues, enhancing development efficiency and code quality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.