Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific value of preprocessor macro

In my build settings i have define some preprocessor macros

i.e. SANDBOX_ENV=1

I want to use the value of SANDBOX_ENV in my shell script.

I have tried echo "SANDBOX value is = ${GCC_PREPROCESSOR_DEFINITIONS}"

but its giving me all macros values like DEBUG=1 SANDBOX_ENV=1 COCOAPODS=1

I want to use value that is assigned to SANDBOX_ENV

like image 713
Mihir Mehta Avatar asked Dec 05 '14 09:12

Mihir Mehta


3 Answers

Try this:

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

HTH,

bovako

like image 112
Thijs Dalhuijsen Avatar answered Oct 01 '22 00:10

Thijs Dalhuijsen


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.

like image 25
alienchow Avatar answered Oct 01 '22 00:10

alienchow


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

like image 30
Mobile Ben Avatar answered Sep 30 '22 22:09

Mobile Ben