This Bash script is designed to locate and use the...

September 2, 2025 at 05:38 AM

#!/bin/bash function usage { echo "Usage: $0 DIR..." exit 1 } # Variable that will hold the name of the clang-format command FMT="" # Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent # that the version number be part of the command. We prefer clang-format if # that's present, otherwise we work backwards from highest version to lowest # version. for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do if which "$clangfmt" &>/dev/null; then FMT="$clangfmt" break fi done # Check if we found a working clang-format if [ -z "$FMT" ]; then echo "failed to find clang-format" exit 1 fi SRC_PATH="$@" if [ -z "$SRC_PATH" ]; then SRC_PATH="../../../src" fi # Check all of the arguments first to make sure they're all directories for dir in "$SRC_PATH"; do if [ ! -d "${dir}" ]; then echo "${dir} is not a directory" usage fi done # Run clang-format -i on all of the things for dir in "$SRC_PATH"; do pushd "${dir}" &>/dev/null find . \ \( -name '*.c' \ -o -name '*.cc' \ -o -name '*.cpp' \ -o -name '*.h' \ -o -name '*.hh' \ -o -name '*.hpp' \) \ -exec "${FMT}" -style=file -i '{}' \; popd &>/dev/null done

This Bash script is designed to locate and use the clang-format tool (a code formatting tool) to recursively format C and C++ source code files in one or more specified directories. The script includes fallback mechanisms to find compatible versions of clang-format, ensuring its functionality across various system configurations. Here's a detailed breakdown of what the code does:

Steps:

  1. Shebang Header:

    #!/bin/bash
    

    This defines the script as a Bash script.

  2. Usage Function:

    function usage {
        echo "Usage: $0 DIR..."
        exit 1
    }
    

    This defines a helper function named usage to print a usage message if the supplied arguments are invalid.

  3. Find the clang-format Command:

    FMT=""
    
    for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
        if which "$clangfmt" &>/dev/null; then
            FMT="$clangfmt"
            break
        fi
    done
    
    • Initializes a variable, FMT, which will store the name of the clang-format executable.
    • Uses a loop to search for clang-format or specific versioned binaries (e.g., clang-format-4.0, clang-format-3.9, etc.) in decreasing version order.
    • It uses the which command to check if the executable is available, and if found, assigns it to FMT.
  4. Handle Missing clang-format:

    if [ -z "$FMT" ]; then
        echo "failed to find clang-format"
        exit 1
    fi
    

    If no clang-format executable was found, the script terminates with an error message.

  5. Process Input Arguments:

    SRC_PATH="$@"
    if [ -z "$SRC_PATH" ]; then
        SRC_PATH="../../../src"
    fi
    
    • Reads directories passed as arguments into the variable SRC_PATH.
    • If no arguments were provided, defaults to the relative path ../../../src.
  6. Check Validity of Directories:

    for dir in "$SRC_PATH"; do
        if [ ! -d "${dir}" ]; then
            echo "${dir} is not a directory"
            usage
        fi
    done
    
    • Iterates over all directories in SRC_PATH.
    • If any argument is not a valid directory, the script prints an error message and exits.
  7. Format Code Files:

    for dir in "$SRC_PATH"; do
        pushd "${dir}" &>/dev/null
        find . \
             \( -name '*.c' \
             -o -name '*.cc' \
             -o -name '*.cpp' \
             -o -name '*.h' \
             -o -name '*.hh' \
             -o -name '*.hpp' \) \
             -exec "${FMT}" -style=file -i '{}' \;
        popd &>/dev/null
    done
    
    • For each valid directory, navigates into it using pushd, suppressing output with &>/dev/null.
    • Uses the find command to locate files with extensions commonly used for C and C++ source or header files (.c, .cc, .cpp, .h, .hh, .hpp).
    • Executes the clang-format tool on each file (-exec "${FMT}" ...) with the -style=file option (to use style settings from a .clang-format file) and the -i option (to apply changes in-place).
    • Exits the directory using popd, suppressing output.

Summary:

The script:

  • Searches for the clang-format tool or its versioned alternatives.
  • Ensures appropriate arguments (one or more directories) are provided.
  • Defaults to ../../../src if no directories are specified.
  • Validates that all provided arguments are directories.
  • Recursively formats C and C++ source code files in the specified directories using clang-format.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node