LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-06-2009, 04:47 PM   #1
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Rep: Reputation: 15
[SOLVED] exporting a variable defined inside a for loop


I posted earlier for advice on a for loop.
I got one step further thanks to ammonais, but now I'm stuck again.
(Sorry for being such a pain).

I have a defined variable:
Quote:
POTATOES="nice"
I want to turn it into:
Quote:
POTATOESX="--potatoes=nice"
The loop below does it perfectly, but the variables are not exported outside the script.
Code:
POTATOES="nice"
CARROTS="ugly"

VAR_GROUP2=("POTATOES CARROTS CUCUMBERS")
	for i in $VAR_GROUP2
	do
		eval b=\$$i
		VARm=`echo "$i"| tr A-Z a-z`
		if [ -z $b ]; then
			iX=""
		else
			iX="--$VARm=$b"
 		fi
	done
If I do
Code:
echo="$POTATOESX"
I get nothing.
Any ideas?

Last edited by musonio; 11-06-2009 at 09:11 PM.
 
Old 11-06-2009, 06:43 PM   #2
malikcpp
LQ Newbie
 
Registered: Nov 2009
Posts: 19

Rep: Reputation: 0
Hi musonio !

http://tldp.org/LDP/Bash-Beginners-G...ect_03_02.html

3.2.3. Exporting variables


Code:

export VARNAME="value"
With an example given on the site :

Code:
franky ~> full_name="Franky M. Singh"

franky ~> bash

franky ~> echo $full_name


franky ~> exit

franky ~> export full_name

franky ~> bash

franky ~> echo $full_name
Franky M. Singh

franky ~> export full_name="Charles the Great"

franky ~> echo $full_name
Charles the Great

franky ~> exit

franky ~> echo $full_name
Franky M. Singh

franky ~>
I hope i've helped you

Good luck with programming

malikcpp
 
Old 11-06-2009, 07:35 PM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Quote:
Originally Posted by musonio View Post
The loop below does it perfectly, but the variables are not exported outside the script.
You might have misunderstood what "export" really does. If you mean, that you want the variables to be visible *after* the script ends, that's impossible.

The script is run into a new subshell, when the script ends, that subshell dies, and all its environment goes to /dev/null.

There's a workaround: read into "source" or the "." dor operator. If you run the script like this:

Code:
source myscript.sh
The script is literally dumped into the current shell, instead of instantiating a new one. Just like if you write the script in the command line, line by line, character by character. Hence, the environment will also belong to the current shell.

Quote:
Code:
echo="$POTATOESX"
I get nothing.
Any ideas?
Anyway, that code doesn't do what you think it does. All that does is to define a variable called "echo", with the contents of the "POTATOESX" variable. So, the contents of "$echo" and "$POTATOESX" will be the same.

You probably mean

Code:
echo "$POTATOESX"
 
Old 11-06-2009, 08:19 PM   #4
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
Misunderstandings. My fault.

I meant that the variables are not exported outside the for loop.
I want them to remain defined once the loop is done for the rest of the script to be able to use it.

Code:
echo="$POTATOESX"
typo. I know that that defines echo as a variable.
I meant:
Code:
echo "$POTATOESX"
The thing is, to be brief, that if I do:
Code:
echo "$iN"
inside the loop, I get:
Code:
--potatoes=nice
which is what I want.
If I do:
Code:
echo "$POTATOESX"
after the loop is done I get nothing.
 
Old 11-06-2009, 08:48 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Well, then the problem has nothing to do with the scope, you are simply not setting the value of $POTATOSX in any way. When you do this:

Code:
iX="--$VARm=$b"
You are defining a variable called "iX". I guess you truly want something in the lines of

Code:
eval ${i}X="--$VARm=$b"
 
Old 11-06-2009, 08:51 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,043

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Just a quick fix, see if this helps:

Code:
#!/bin/bash   

CUCUMBERS="good"
POTATOES="bad"
CARROTS="ugly"
BANANAS=""

VAR_GROUP2=("POTATOES CARROTS CUCUMBERS BANANAS")
  for i in $VAR_GROUP2
  do
    eval b=\$$i
    VARm=`echo "$i"| tr A-Z a-z`
    if [ -z $b ]; then
      eval ${i}X=""
    else
      eval ${i}X="--$VARm=$b"
    fi
  done

echo "$CUCUMBERSX"
echo "$POTATOESX"
echo "$CARROTSX"
echo "$BANANASX"
It should output:

Code:
$ ./f
--cucumbers=good
--potatoes=bad
--carrots=ugly

$
 
Old 11-06-2009, 09:10 PM   #7
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
I'll need to look into eval.
That's what also saved me in the last mess thanks to ammorais suggestion.

A MILLION THANKS i92guboj.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exporting variable in bash deathalele Programming 12 04-04-2009 02:24 PM
Neither JAVA_HOME nor the JRE_HOME variable defined Raakh Linux - Newbie 9 06-02-2007 07:40 AM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 06:19 AM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 10:29 AM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 09:50 PM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration