LinuxQuestions.org
Visit Jeremy's Blog.
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 02-08-2010, 07:17 AM   #1
manomohan
Member
 
Registered: Jul 2005
Location: india
Distribution: Fedora core 3
Posts: 42

Rep: Reputation: 15
how can i change the value of a environment variable through shell script ?


Hi

i have created a environment variable in .bash_rc file.

eg: PDL_VALUE= TRUE

is it possible to change the value of the above variable through a shell script.

if not, have any alternative way to change.

Actually my need to set a status of job (periodic checking) thru the variable and use that variable in another script.
 
Old 02-08-2010, 07:25 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
No, it's not possible to set anything in the environment above whatever command is running. To be able to do so would be a big security risk.

The only real way I know of to set your environment through a script is to have it sourced by your start-up script, which in essence makes it part of that start-up script.

To transfer information between scripts you need to use a temporary file or perhaps a fifo.
 
Old 02-10-2010, 06:37 AM   #3
manomohan
Member
 
Registered: Jul 2005
Location: india
Distribution: Fedora core 3
Posts: 42

Original Poster
Rep: Reputation: 15
I tried with file.

I kept the variable in a file and sourcing this file wherever i need.
And updating the value in the file

#!/bin/bash
. valuevar
echo $PDL_STATUS

if [ $PDL_STATUS = 'TRUE' ]
then
sed -e 's/FALSE/TRUE/' valuevar > newfile
mv -f newfile valuevar
else
sed -e 's/TRUE/FALSE/' valuevar > newfile
mv -f newfile valuevar
fi
 
Old 02-10-2010, 06:41 AM   #4
manomohan
Member
 
Registered: Jul 2005
Location: india
Distribution: Fedora core 3
Posts: 42

Original Poster
Rep: Reputation: 15
And my next requirement is to start another script (script B) in true condition. At the same time i need to exit from the current script (above mentioned script)

Which is the good way 2 do that.
 
Old 02-10-2010, 03:46 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Sourcing the file back into your first script does no good. All it does is set the environment of that script, meaning it's still unavailable outside of it. In order to set the environment of the shell, it needs to be sourced by the shell itself.

Or, since your purpose is to set the environment of the second script, export it to a file with the first script, then source that file into the second script.
 
Old 02-10-2010, 11:05 PM   #6
manomohan
Member
 
Registered: Jul 2005
Location: india
Distribution: Fedora core 3
Posts: 42

Original Poster
Rep: Reputation: 15
Can you just elaborate on the above solution.

Actually the content in the file valuevar is given below,

PDL_SATUS='TRUE'
export PDL_STATUS;
 
Old 02-10-2010, 11:10 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Export the variables you define in .bashrc. The file is sourced so it doesn't run in a subshell. Export moves variables from normal memory to environmental memory.

In your first post, you had a space after the equals sign. You would be clearing the variable instead and then running the true command.

If you intend for .bash_rc to be used only in certain scripts, then source .bash_rc in the beginning of the scripts that use it.

Last edited by jschiwal; 02-13-2010 at 08:11 PM. Reason: spelling
 
Old 02-10-2010, 11:50 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
PDL_SATUS='TRUE'
export PDL_STATUS;
There's a typo there ... hope its not in the real file ...
 
Old 02-11-2010, 09:55 PM   #9
manomohan
Member
 
Registered: Jul 2005
Location: india
Distribution: Fedora core 3
Posts: 42

Original Poster
Rep: Reputation: 15
The typo error is only for here..
Thanks for pointing out that..
 
  


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
shell to set environment variable ufmale Linux - Newbie 5 11-16-2009 11:35 AM
what is the difference between CGI and SHELL environment variable cnsung Linux - Server 3 09-11-2009 10:36 AM
change PS1 environment variable from c program sagsriv Linux - Newbie 4 03-10-2008 01:39 PM
Global (shell) environment variable question Seventh Linux - Newbie 1 04-05-2007 03:18 PM
how to set the Environment variable from shell Scrit Sundaram Linux - Software 2 10-13-2006 11:59 PM

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

All times are GMT -5. The time now is 11:03 PM.

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