Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zeppenwolf

macrumors regular
Original poster
Nov 17, 2009
129
3
I've had a problem with #defines which has been nagging me for years.

It wasn't a big problem, and it always struck me that it would be a radical chore to solve it, and I'd never ever heard anyone else complain about it, and... well, anyway, I finally got around to doing something, and I have a solution, but you Awkward guys could maybe help me make it really polished.

I have a C/Objc program with some defines in a file like this:

Code:
//
//  kMyKillerApp.h
//  MyKillerApp
//
//  Created by Senior Programmer on 3/9/14.
//  Copyright 2014 ComTech. All rights reserved.

#pragma once

#define kMyKillerAppIdentity        Supercalifragilisticexpialidocious
#define kMyKillerAppIdLength        34
//#define kMyKillerAppBaseIdLength        yeah,nevermind

The project also has some bash scripts which must use the same exact "variables". Until now, I've been copy & pasting the string Supercalifragilisticexpialidocious from my ObjC.h file into my bash script(s)... and that doesn't seem like a BIG deal, but then I forgot, the other day... ( It so happens that the actual "spelling", so to speak, of Supercalifragilisticexpialidocious has changed many times in my current app, and so... here we are. )

I wanted to have just one doggone declaration of Supercalifragilisticexpialidocious somewhere, and then both my ObjC code and my bash scripts would refer to that one declaration.

So I mostly have it figgered out; I have the following bash script:

Code:
#!/bin/bash

#    Takes lines like:

#define        kFinkNottle        Gussie

# from the file passed in $1, and echos back space-separated "lines" like "kFinkNottle=Gussie". 

echo `awk '/^#define/  {print $2 "=" $3}' "$1"`

So, performed on my ObjC header above, it spits out:

Code:
kMyKillerAppIdentity=Supercalifragilisticexpialidocious kMyKillerAppIdLength=34

If I call eval() on the result of the script, like this:

Code:
eval `./bridgeFromDefineToBash kMyKillerApp.h`

Then Voila! kMyKillerAppIdentity is now a bona fide bash variable with the proper value.

But you can see where I'd like go just one step further-- I would like to avoid having to call "eval" at all, ( from outside )-- I would like the bash script to not only eval the assignment string(s) that I generate, but also, darn it, because it's a script, I need to call export to get the variable and its value pushed up out of the script to the calling environment.

IOW, can I somehow get the value of Awk's $2 and then call bash's export command on it? At this point, I've stretched my bash / Awk competency six counties past the limit... If you can help me do this, or there's some completely different other weird way... tHnaks!
 
bridgeFromDefineToBash:
Code:
export `awk '/^#define/  {print $2 "=" $3}' "$1"`
call:
Code:
.  bridgeFromDefineToBash kMyKillerApp.h

or

bridgeFromDefineToBash:
Code:
awk '/^#define/  {print $2 "=" $3}' "$1"
call:
Code:
export `./bridgeFromDefineToBash kMyKillerApp.h`
 
Last edited:
bridgeFromDefineToBash:
Code:
.  bridgeFromDefineToBash kMyKillerApp.h

Thank you DMI. This really is a nifty piece of work, I think. It's Mission Accomplished BooCoo here, even if I still have a question:

Of course I prefer Version One you gave that I quote up there-- it is only two more characters than the Holy Grail(tm) I was dreaming of, ( "bridgeFromDefineToBash kMyKillerApp.h" ).

I have confirmed that the initial ". " is essential, however-- if I edit the values in kMyKillerApp.h, then call "bridgeFromDefineToBash kMyKillerApp.h" ( script in $PATH ) or "./bridgeFromDefineToBash kMyKillerApp.h", the variables in the calling environment do not reflect the most recent changes...

If you have a minute, can you explain what that initial ". " is doing? In the case where I call ""bridgeFromDefineToBash kMyKillerApp.h", I can echo() the most recent changes, but the export() command doesn't export them. Well, golly: what use is the export() command if it doesn't actually export ???
 
"." is another name for "source" which executes the commands in the current shell.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.