[bash] command line substitution with environmental variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] command line substitution with environmental variables
# 1  
Old 03-06-2010
[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.
-------------------------------------------------
Code:
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.
# 2  
Old 03-06-2010
Hi ASGR,it is simple.You try this,
Code:
 
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.

Code:
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

# 3  
Old 03-06-2010
Quote:
Originally Posted by ASGR
Code:
$(${FLAGS[@]})

CFLAGS="-arch: command not found
Code:
CFLAGS=${FLAGS[@]}

# 4  
Old 03-06-2010
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.
# 5  
Old 03-06-2010
Code:
eval "${FLAGS[@]}" ./configure

# 6  
Old 03-07-2010
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
Code:
function array_eval
{
        eval "\"\${$1[@]}\""
}

Any help would be appreciated.

A.
# 7  
Old 03-07-2010

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

<< Environmental Variables are not set when script completes >>

Hi Team, I have a wrapper script which i have pasted below, it internally calls one python script to generate Environmental in a file called /home/oracle/myenv.sh, when i execute this script via wrapper script, its not reflecting in my current session, still showing old env variables. any... (2 Replies)
Discussion started by: kamauv234
2 Replies

2. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (15 Replies)
Discussion started by: siegfried
15 Replies

3. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (0 Replies)
Discussion started by: siegfried
0 Replies

4. OS X (Apple)

Don't understand the practical difference between command aliases and environmental variables

Hey, I'm recently learning Unix from the video course by Kevin Scoglund. I'm stuck at the moment where he goes into Environmenat variables. I have some issues with understanding what's the essential difference between EV and command aliases: for instance, by writing the command alias ll='ls... (3 Replies)
Discussion started by: scrutinizerix
3 Replies

5. Shell Programming and Scripting

Help with manipulating environmental variables in UNIX

I am wondering if there is away to increment a date in c shell. What I need to do is basic, but I lack the knowledge. I have they following environmental variable in my job scripts setenv YYYY `date '+%Y'` I then set YYYY to be part of my output dataset name: setenv dd_OUTPUTP... (1 Reply)
Discussion started by: jclanc8
1 Replies

6. Shell Programming and Scripting

bash, command line substitution

I have one script calling another with a set of strings that includes white space. Script A calls Script B with these input strings: one two "th ree" Script B pulls apart the arguments correctly: arg0 = one, arg1 = two, arg2 = "th ree" if I call it from within Script A like so:... (10 Replies)
Discussion started by: skippyV
10 Replies

7. Shell Programming and Scripting

Oracle environmental variables in shells script

Hi, Getting below error on executing the shell script which initiates sqlplus How to set oracle enviornment variables in the shell script ? With Regards (3 Replies)
Discussion started by: milink
3 Replies

8. AIX

Best way to setup my own environmental variables ?

I am writing a few korn scripts to be used by all our operators on several 4.1/4.2 AIX servers. I want to create environmental variables that once set, can be read/modified by my scripts (ex: specific folders, file names, conventions, general values, ...). I thought this would be better then... (4 Replies)
Discussion started by: Browser_ice
4 Replies

9. UNIX for Dummies Questions & Answers

Unix passing environmental Variables

In my script when I change an env variable in the parent shell it is only changed for that session - it there away to change it permanently using a script so that when I use rlogin (create a child session) that the env variable is set correctly? Basically what I am trying to do is to pass a... (7 Replies)
Discussion started by: belfastbelle
7 Replies

10. UNIX for Advanced & Expert Users

Environmental Variables - where stored ?

Hi all ! Yesterday I defined an environmental variable PATH, but today when I restarted machine, I could not see that it was stored any place. Is there any file where I could save the settings ? I have quite a few env.variables defined, so I need a smarter way to define. regards D (5 Replies)
Discussion started by: DGoubine
5 Replies
Login or Register to Ask a Question