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.

Droopycom
  • 1,831
  • 1
  • 17
  • 20
  • 2
    Use an array in shells that have it, or $@ (after saving if needed) in POSIX. See https://stackoverflow.com/questions/39585662/bash-shell-expand-arguments-with-spaces https://stackoverflow.com/questions/51205277/command-line-args-with-spaces https://stackoverflow.com/questions/7454526/variable-containing-multiple-args-with-quotes https://stackoverflow.com/questions/4753993/passing-arguments-to-a-command-in-bash-script-with-spaces https://stackoverflow.com/questions/905891/passing-argument-containing-space https://stackoverflow.com/questions/19012991/how-to-pass-quoted-arguments-from-variable – dave_thompson_085 Jul 05 '21 at 06:32

1 Answers1

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 "$@"
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 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 Jul 05 '21 at 06:33
  • @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 – Inian Jul 05 '21 at 06:36
  • 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 $@ – Droopycom Jul 05 '21 at 06:42
  • 1
    It all comes down to the possibility of using `sh` only features or other shell features using arrays – Inian Jul 05 '21 at 06:46