0

I'm writing a shell script to drive xcodebuild, the Xcode command line frontend.

xcodebuild takes build settings on the command line in the following manner:

$ xcodebuild a=value b=value

value may contain spaces, for example it could be a list of paths.

So I would invoke it like so:

$ xcodebuild "a=value1 value2 value3" "b=value4 value5"

xcodebuild will report the settings as expected:

Build settings from command line:
    a = value1 value2 value3
    b = value4 value5

However when invoked as follow:

$ xcodebuild "a=value1 b=value2"

This will be interpreted as a single argument, assigning a value to build setting a:

Build settings from command line:
    a = value1 b=value2

Now, the real question is I want to write a shell script that will have all the build settings in one variable, e.g. I can do this:

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"

xcodebuild "${SETTING_A}" "${SETTING_B}"

However that does not scale.

And I want to do something like this:

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"
SETTINGS=${SETTING_A} ${SETTING_B}

xcodebuild ${SETTINGS}

However the above doesn't work and no matter what variations I tried, I can't keep SETTING_A and SETTING_B as two separate words/arguments for the command. It's either 1 if I use "$SETTINGS" or 5 if I use just $SETTINGS.

I'm looking for standard unix shell solutions but will entertain Bash or Zsh specific one if this is not doable in standard sh.

1

1 Answer 1

1

Using standard sh (POSIX), just use set on the individual setting expansions and call "$@" to pass them, i.e.

SETTING_A="a=value1 value2 value3"
SETTING_B="b=value4 value5"

set -- "${SETTING_A}" "${SETTING_B}"
xcodebuild "$@"
Sign up to request clarification or add additional context in comments.

4 Comments

That works, but not really. It doesn't scale when the script has multiple invocations of xcodebuild with the same SETTINGS. And I don't think I can hog $@ for the whole script. It would be great if the result of that set command would affect a SETTINGS variable, rather than the special @ variable.
@Droopycom: If you want to adopt a solution without arrays, positional params are the way to go $@. Why do you say And I don't think I can hog $@ for the whole script. . You can customize your settings on the go by re-set-ting the $@ any number of times with your desired settings value
Well, the script is complex, it defines the SETTINGS at the beginning, but it actually define more than one, one for each platform. Then they are used multiple times later. So for example I may have SETTINGS_IOS and SETTINGS_MACOS. So if there is only one $@ to use, I'm kinda stuck. I also could imagine other parts of the script want to do the same trick for a different command and redefine $@
It all comes down to the possibility of using sh only features or other shell features using arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.