inspector-g
inspector-g

Reputation: 4176

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.

Upvotes: 12

Views: 12302

Answers (3)

Simon Schr&#246;ter
Simon Schr&#246;ter

Reputation: 1

So what about having this?

#!/bin/bash

if [ <old_ld_condition> ]; then
    /path/to/old/ld "${@//-dependency_info/}"
else
    /path/to/new/ld "$@"
fi

Upvotes: 0

anubhava
anubhava

Reputation: 785376

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

Upvotes: 18

user3159253
user3159253

Reputation: 17455

You should use Bash Arrays if you really want to stay with bash:

declare -a ARGS
for var in "$@"; do
    # Ignore known bad arguments
    if [ "$var" = '-dependency_info' ]; then
        continue
    fi
    ARGS[${#ARGS[@]}]="$var"
done

now "${ARGS[@]}" can be used just as "$@". man bash for more information.

Upvotes: 5

Related Questions