LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-03-2004, 07:54 PM   #1
Jicksta
LQ Newbie
 
Registered: Sep 2004
Distribution: Mandrake, Debian
Posts: 11

Rep: Reputation: 0
Using Bash Script for Exporting and Returning Environmental Variables


Hi,

I'm trying to write a Bash script that checks if a specific environmental variable exists, and if not, prompts the user as to what they want its value to be.

I'm using the environmental variable to store the path to a program the user is expected to have installed. If my script does not find it, it will write the path the user inputs to the environmental variable it will use each time it's executed.

Here's what I've come up with:
Code:
if [ -z $The_Path ]; then
  echo -n "The_Path not found, enter a value:  "
  read The_Path
  export The_Path
else
  echo $The_Path
fi
(the -z argument returns boolean true if the string has 0 characters)

For some reason, each time this script runs, the environmental variable is detected to be empty and (obviously) never writes the variable permanently.

Is there something I'm overlooking?
 
Old 12-03-2004, 08:39 PM   #2
SirSlappy
Member
 
Registered: Jun 2003
Location: Glendale AZ
Distribution: Slackware 10
Posts: 153

Rep: Reputation: 30
well..

if ($ThePath | grep /home/user/blah); then
echo $ThePath
else
echo -n "The Path Not Found, enter a value: "
read The_Path
export The_Path
fi
 
Old 12-04-2004, 11:26 AM   #3
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Code:
[/home/soumen/tmp] $ cat d
if [ -z $The_Path ]; then
  echo -n "The_Path not found, enter a value:  "
  read The_Path
  export The_Path
else
  echo $The_Path
fi
[/home/soumen/tmp] $ d
The_Path not found, enter a value:  haha
[/home/soumen/tmp] $ echo $d

[/home/soumen/tmp] $ The_Path=haha
[/home/soumen/tmp] $ d
The_Path not found, enter a value:  why
[/home/soumen/tmp] $ export The_Path=haha
[/home/soumen/tmp] $ d
haha
[/home/soumen/tmp] $
I think that should make it clear.

HTH.
 
Old 12-04-2004, 04:14 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You script is never going to change its parent shell environment, unless you run it with the 'dot' builtin, like this:
Code:
$ . d
 
  


Reply



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
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
Environmental Variables danglingpointer Slackware 13 01-06-2005 12:36 AM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM
environmental variables msound Programming 0 08-21-2004 07:06 PM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:46 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration