LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-03-2004, 01:27 PM   #1
dtheorem
Member
 
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Rep: Reputation: 15
exporting variables under bash isn't working!


Hey, I'm trying to figure this one out. Maybe you can help me. Here's what I got:
-----------------------------
#!/bin/bash
echo "Enter the partition where LFS is to be installed (/mnt/lfs):"
read LFS
if [ ! $LFS ]
then
LFS="/mnt/lfs"
fi
export LFS
-----------------------------

Now, when I run this, it works fine, however, when it exits back to the shell, I type in "echo $LFS" and it prints an empty line. It's my undestanding that LFS should be a global variable and should be available after the program exits. Am I wrong?
 
Old 03-03-2004, 02:05 PM   #2
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Are shell scripts not run in a new instance of the shell? Why not just follow the LFS book's way of doing it via sourcing files?
 
Old 03-03-2004, 02:52 PM   #3
dtheorem
Member
 
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Original Poster
Rep: Reputation: 15
Actually, I haven't yet started the installation of LFS, this is just a generic question regarding global variables. But, yes, my plans are to create an automatic install script. Why? Because I've gone through the whole thing 4 times now, and it's getting boring.
 
Old 03-03-2004, 02:55 PM   #4
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
Quote:
Originally posted by dtheorem
Actually, I haven't yet started the installation of LFS, this is just a generic question regarding global variables. But, yes, my plans are to create an automatic install script. Why? Because I've gone through the whole thing 4 times now, and it's getting boring.
There is already a full project towards automating the build, you can find links through the LFS homepage. There is also a Makefile available at Christophe Devine's (?) home directory on the main LFS server.

To answer your question more fully: the script is launched in a new instance of the shell, and thus is not affecting your current instance. However, any script run from within that sub-shell will have that environmental variable ( as far as I know ).
 
Old 03-03-2004, 11:24 PM   #5
ugenn
Member
 
Registered: Apr 2002
Posts: 549

Rep: Reputation: 30
You seem to be misunderstanding how environment variable works. They are not global to the system. Rather, each process inherits a copy of the parent's envvars. So in your case, when the child script exits, any envvars it creates are also lost.
 
Old 03-04-2004, 10:55 PM   #6
dtheorem
Member
 
Registered: Sep 2003
Location: Winnipeg, MB, CA
Distribution: LinuxMint
Posts: 101

Original Poster
Rep: Reputation: 15
Thank-you for your explanations. They were very helpful. In regards to ALFS, I've tried many times to install it, but I run Mandrake 9.2 and there's a universal law that says ALFS does not install under Mandrake 9.2. We always get an error with libxml2. Anyway, my script is teaching me a lot about bash programming, which is always a plus.
 
Old 03-11-2004, 08:35 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
exports only work in the script as it runs.
everything until the script ends will 'see' the export.
When the script dies so do it's exports.


But FYI

to make the export work you can 'dot' the file.

. script.sh

this, sort of 'in lines' a script without running explicitly.

it will work, but be careful you don't put exit or return commands in
or it will log you out!

but one generally uses dot files just for environment and aliases and such.
simple stuff.

you can also make it as a function:

o_func_it()
{
read input
export LFS="$input" # quotes are safer!
}

put that in a file and 'dot' it and then you can type
"o_func_it"
like you would a command.

functions are a good idea in scripting, but generally one doesn't
do them as above, just from the login shell. but they are interesting to
try out.

have a really *good* read of 'man bash' and try it all.


regards, billy


billy
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exporting variables gecoool Programming 2 10-25-2005 01:17 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
Exporting variables through scripts rammya Programming 1 04-14-2004 10:24 AM
exporting variables in Perl nbp Programming 1 11-18-2003 08:35 AM
exporting global variables neo77777 Programming 6 09-18-2002 01:46 PM


All times are GMT -5. The time now is 10:58 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