bashshellargumentscommand-line-arguments

How to intercept and remove a command line argument in bash


After "upgrading" to Mavericks and Xcode 5, I have a variety of minor problems to deal with to make Xcode compile some of my older projects.

It appears that Xcode is passing a new argument to the ld linker, and there's really no stopping Xcode from doing so. An older version of ld, which I need for a variety of reasons, gives an error when seeing an argument it doesn't know (so my projects cannot compile).

What I need is a thin wrapper over my older version of ld to remove the "bad" arguments under certain circumstances. I thought that a bash shell script would be perfect, but bash is not my forte.

Here's what I've got:

# Look for conditions necessary to use older ld
... # (placeholder, obviously)

# Run older ld (pseudo condition)
if [ <old_ld_condition> ]; then
    ARGS=''
    for var in "$@"; do
        # Ignore known bad arguments
        if [ "$var" = '-dependency_info' ]; then
            continue
        fi

        ARGS="$ARGS $var"
    done

    /path/to/old/ld "$ARGS"
else
    /path/to/new/ld "$@"
fi

However, running /path/to/old/ld "$ARGS" results in ld interpreting the entire $ARGS string as one argument. Running /path/to/old/ld $ARGS results in ld receiving unescaped versions of previously escaped strings.

Clearly, I'm misunderstanding something about the nature of $@, how to manipulate it, and how to pass that manipulation to the older ld. Thanks everyone.


Solution

  • This should work:

    # Run older ld (pseudo condition)
    if [[ <old_ld_condition> ]]; then
        args=()
        for var; do
            # Ignore known bad arguments
            [[ $var != '-dependency_info' ]] && args+=("$var")
        done
    
        /path/to/old/ld "${args[@]}"
    else
        /path/to/new/ld "$@"
    fi