Get a specific preprocessor macro value

In my build settings, I defined some preprocessor macros

i.e. SANDBOX_ENV = 1

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

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

but it gives me all macro values ​​like DEBUG=1 SANDBOX_ENV=1 COCOAPODS=1

I want to use the value assigned by SANDBOX_ENV

+6
source share
4 answers

Try the following:

 #!/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

+7
source

You should easily deal with awk or something, but here's how I do it:

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

In the context of your echo:

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

I basically passed the contents of GCC_PREPROCESSOR_DEFINITIONS and cut out the SANDBOX_ENV part.

 grep -P 

- use Perl regex \ d + because I don't like POSIX. Just a preference. In fact, that

 grep -P 'SANDBOX_ENV=\d+' 

it is to find the line in the content passed to it, which contains the line "SANDBOX_ENV =" and any number of digits following it. If the value can contain alphanumeric characters, you can change \ d for digits to \ w for a word that covers a-zA-Z0-9, and you get:

 grep -Po 'SANDBOX_ENV=\w+' 

The + symbol simply means that there must be at least one character of the type specified by the character, including all subsequent characters that match.

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

This output is then passed to the sed command, where I do a simple search and replace, where I replaced "SANDBOX_ENV =" with "", leaving only the value behind it. There are probably simpler ways to do this, as with awk, but you must find out for yourself.

+3
source

If you want something contained in the build settings, and you do not mind minor indirection, then:

  • Create custom settings SANDBOX_ENV=1 (or any other value)
  • In preprocessor macros add SANDBOX_ENV=${SANDBOX_ENV}

In your shell, to check, run

 echo ${SANDBOX_ENV} 

Using custom parameters, you can still change the value for the configuration and architecture of the assembly. So, for example, you can make the Debug configuration equal to SANDBOX_ENV = 0, and Release - SANDBOX_ENV = 1.

enter image description here

+2
source

This might be the obvious answer, but you just tried:

 echo ${SANDBOX_ENV} 

If this does not work, try using eval:

 eval "${GCC_PREPROCESSOR_DEFINITIONS}" echo ${SANDBOX_ENV} 
+1
source

Source: https://habr.com/ru/post/979162/


All Articles