[bash] command line substitution with environmental variables

Hi,

I'm using an array that contains compiler FLAGS
that need to be executed either before ./configure
or after the main 'make' command.

example of array containing compiler flags.
-------------------------------------------------

FLAGS[0]="CFLAGS=\"-arch x86_64 -g -Os -pipe -no-cpp-precomp\""
FLAGS[1]="LDFLAGS=\"-arch x86_64 -bind_at_load\""

To get the output:
CFLAGS="-arch x86_64 -g -Os -pipe -no-cpp-precomp" LDFLAGS="-arch x86_64 -bind_at_load" ./configure

Unfortunately, when I try to use the usual substitution
$(${FLAGS[@]}), it seems to terminate after the first
space following the -arch option:

CFLAGS="-arch: command not found

I've tried a number of combinations but not been able
to find a conclusive solution. Any help or ideas would
be appreciated.

A.

Hi ASGR,it is simple.You try this,

 
FLAGS[0]="CFLAGS=\"-arch x86_64 -g -Os -pipe -no-cpp-precomp\"" 
FLAGS[1]="LDFLAGS=\"-arch x86_64 -bind_at_load\""  
echo ${FLAGS[0]} ${FLAGS[1]} 

The above is the example code for your understanding.

If you have more number of strings in an array,you use for loop like below.

FLAGS[0]="CFLAGS=\"-arch x86_64 -g -Os -pipe -no-cpp-precomp\"" 
FLAGS[1]="LDFLAGS=\"-arch x86_64 -bind_at_load\""  
for((i=0;i<${#FLAGS[@]};i++))
do
echo -n "${FLAGS[$i]} "
done
CFLAGS=${FLAGS[@]}

Thanks for all the replies.

I've realised that what I'm trying to do is to define
the flag variables before ./configure is executed on
the same line which I've been told is the most recent
method.

Essentially, it's the same process as typing it on the
command line, but all the variables and commands
are held in variables.

On-the-fly or dynamic seems to be appropriate words
to describe what I'm trying to do.

A.

eval "${FLAGS[@]}" ./configure

Thanks again.

Just to nail the lid shut on this one, I'd like
to use it through a function as below, but a
straight translation from the previous post
to one that uses eval in a function seems to
generate a 'command not found' error.

use: array_eval FLAGS

function array_eval
{
        eval "\"\${$1[@]}\""
}

Any help would be appreciated.

A.

You are making the whole thing more complicated than it need be (I think).

Please post what you want the expanded ./configure command to look like.

For future reference, this is an approximate
representation of the system so far. The method
suggested works and generates identical output
whilst processing.

I may have been overwhelmed with a working
system that I may have began to think a little
too far ahead with the possibilities when trying
to incorporate it into a function. However, I
still think it *might* be of some interest.

#!/bin/bash

declare -a FLAGS
FLAGS[0]="CFLAGS=\"-arch x86_64 -g -Os -pipe -no-cpp-precomp\""
FLAGS[1]="LDFLAGS=\"-arch x86_64 -bind_at_load\""

declare -a OPTION
OPTION[0]="./configure"
OPTION[1]="--bindir=/usr/local/bin"
OPTION[2]="--datarootdir=/usr/local/share"
OPTION[3]="--mandir=/usr/local/man"

declare -a CONFIG
declare do_flags=true

## check if array is not null
if [ -n "${FLAGS}" ]; then
## check if flags prefix ./configure
    if $do_flags
        then CONFIG=( "${FLAGS[@]}" )
        fi
        fi

## append options to array
CONFIG=( "${CONFIG[@]}" "${OPTION[@]}" )

## check contents of array
echo "${CONFIG[@]}"
## execute contents of array (disabled)
## eval "${CONFIG[@]}"

## check dynamic variables
eval "${FLAGS[@]}"
echo "CFLAGS=$CFLAGS"
echo "LDFLAGS=$LDFLAGS"