How to intercept and remove command line argument in bash

After “upgrading” to Mavericks and Xcode 5, I have many small problems that I have to encounter in order for Xcode to compile some of my old projects.

Xcode seems to pass a new argument to the ld linker, and Xcode does not actually stop. The old version of ld , which I need for various reasons, gives an error when looking at an argument that it does not know (so my projects cannot compile).

I need a thin shell on top of the old version of ld to remove the “bad” arguments under certain circumstances. I thought the bash script shell would be perfect, but bash is not my forte.

Here is what I have:

 # 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, when running /path/to/old/ld "$ARGS" result of ld interprets the entire line of $ARGS as a single argument. Running /path/to/old/ld $ARGS causes ld receive unshielded versions of previously escaped strings.

It is clear that I misunderstand something about the nature of $@ , about how to manipulate it and how to pass this manipulation to the senior ld . Thanks to everyone.

+7
bash shell arguments command-line-arguments
source share
2 answers

This should work:

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

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 as " $@ " . man bash for more information.

+3
source share

All Articles