I'm trying to figure out a way to read a C header file that contains some #define's and convert those macros to bash variables that can be accessed by a shell script.
What I tried first was this:
gcc -E -dM header_file.h|grep '^#define'|awk '{var=$2;$1=$2="";sub(/^[ \t]+/, "");print "export " var "=\"" $0 "\""}' > /tmp/tmp.sh
source /tmp/tmp.sh
Which worked, but I really didn't like that I was generating this temporary file
What I tried next was similar, in hopes of getting rid of the temporary file:
gcc -E -dM header_file.h|grep '^#define'|awk '{var=$2;$1=$2="";sub(/^[ \t]+/, "");print "export " var "=\"" $0 "\""}' |while read i
do
$i
done
This did not work, however... It seems like there should be another way to do this, but I can't think of it, and I'm pretty sure once somebody posts how I'm going to feel really stupid, but I'd still appreciate the kickstart that anyone can offer to point me in the right direction.
(while read -r def var val; do printf "export %s=\"%s\"\n" "$var" "$val"; done < <(gcc -E -dM header_file.h|grep '^#define')) >tmp/tmp.sh && source /tmp/tmp.sh? You can probably drop thetmpfile altogether and just addsource <before the rest of the subshell expression(...)(...)to source as a file. Meaning redirect the entire commandsource < (while read -r ...^#define'))