Get specific value of preprocessor macro Get specific value of preprocessor macro shell shell

Get specific value of preprocessor macro


Try this:

#!/bin/bashGCC_PREPROCESSOR_DEFINITIONS="DEBUG=1 SANDBOX_ENV=1 COCOAPODS=1" # delete everything before our value ans stuff into TMPVALTMPVAL="${GCC_PREPROCESSOR_DEFINITIONS//*SANDBOX_ENV=/}" # remove everything after our value from TMPVAL and return itTMPVAL="${TMPVAL// */}"echo $TMPVAL;  #outputs 1 

HTH,

bovako


You should be able to parse it easily with awk or something, but here's how I'd do it:

echo $GCC_PREPROCESSOR_DEFINITIONS | grep -Po 'SANDBOX_ENV=\d+' | sed 's/SANDBOX_ENV=//'

In your echo context:

echo "SANDBOX value is $(echo $GCC_PREPROCESSOR_DEFINITIONS | grep -Po 'SANDBOX_ENV=\d+' | sed 's/SANDBOX_ENV=//')"

Basically I piped the contents of GCC_PREPROCESSOR_DEFINITIONS and grepped out the SANDBOX_ENV portion.

grep -P 

is to use the Perl regex \d+, because I don't like POSIX. Just a preference. Essentially what

grep -P 'SANDBOX_ENV=\d+' 

does is to find the line in the content piped to it that contains the string "SANDBOX_ENV=" and any number of digits succeeding it. If the value might contain alphanumerics you can change the \d for digits to \w for word which encompasses a-zA-Z0-9 and you get:

grep -Po 'SANDBOX_ENV=\w+'

The + just means there must be at least one character of the type specified by the character before it, including all succeeding characters that matches.

the -o (only-matching) in grep -Po is used to isolate the match so that instead of the entire line you just get "SANDBOX_ENV=1".

This output is then piped to the sed command where I do a simple find and replace where I replaced "SANDBOX_ENV=" with "", leaving only the value behind it. There are probably easier ways to do it like with awk, but you'll have to learn that yourself.


If you want to have something self contained within the Build Settings and you don't mind slight indirection, then:

  1. Create User-Defined settings SANDBOX_ENV=1 (or whatever value you want)
  2. In Preprocessor Macros, add SANDBOX_ENV=${SANDBOX_ENV}

In your shell, to test, do

echo ${SANDBOX_ENV}

With the User-Defined Settings, you'll still be able to modify the value for Build Configuration and Architecture. So, for example, you could make the Debug config be SANDBOX_ENV=0 and Release be SANDBOX_ENV=1.

enter image description here