LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-09-2005, 11:03 AM   #1
AzidStar
Newbie
 
Registered: Jan 2005
Distribution: Fedora Core 3
Posts: 12

Rep: Reputation: 0
Setting global system variables


Hello everyone.

I was wondering how to set environmet variables that are globally accessible for all processes/users that are in the system.
I saw that you can do this by staticly adding line to /etc/profile, but I want to be able to do this "on the fly" and preferably from a c-program or secondly from a shell script.
The problem right now is that when I create a variable in one shell (read one memory space) it is only visible to that shell.
How are the global variables set so that everyone can access them?

I hope I am making some sence to you out there.

Sincerly Kristoffer Nordström
 
Old 03-09-2005, 12:35 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,568

Rep: Reputation: 164Reputation: 164
Variables are set when user logs in (or runs a terminal emulator etc). There's no way provided to change variables in all sessionsa fter they're run. What you can think of is editing files in /proc/ (process files), but it may not work and cause problems.
 
Old 03-10-2005, 02:26 AM   #3
AzidStar
Newbie
 
Registered: Jan 2005
Distribution: Fedora Core 3
Posts: 12

Original Poster
Rep: Reputation: 0
Hmm .. I was thinking of using proc earlier.. can I just create new files there (read: in the RAM) or do I have to use the existing file structure?
Sorry if this seems a bit neewbish but we alll have to learn the hard way.

Sincerly Kristoffer Nordström
 
Old 03-10-2005, 03:04 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,568

Rep: Reputation: 164Reputation: 164
You need to use the existing files you want to change something - proc is an interface between the kernel and user space and it requires proper formats.
 
Old 03-11-2005, 08:12 AM   #5
AzidStar
Newbie
 
Registered: Jan 2005
Distribution: Fedora Core 3
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks for the reply. Well its back to the drawing board then.
For now I have written a library that "sets" the "variable"-information in /tmp/THISVARIABLE to have a common point for all shells and processes to store and retrieve.
However I will look further into the possibility to use some form of shared memory where variables can be set.
HopeI dont have to write the damn thing myself.

Anyway thanks for the help.

Sincerly Kristoffer Nordström
 
Old 03-11-2005, 10:03 AM   #6
wpn146
Member
 
Registered: Jan 2005
Distribution: Solaris, Linux Fedora Core 6
Posts: 170

Rep: Reputation: 30
Re: Setting global system variables

Quote:
Originally posted by AzidStar
Hello everyone.

I was wondering how to set environmet variables that are globally accessible for all processes/users that are in the system.
I saw that you can do this by staticly adding line to /etc/profile, but I want to be able to do this "on the fly" and preferably from a c-program or secondly from a shell script.
The problem right now is that when I create a variable in one shell (read one memory space) it is only visible to that shell.
How are the global variables set so that everyone can access them?

I hope I am making some sence to you out there.

Sincerly Kristoffer Nordstr�m
If you use:
Code:
source filename.sh
-- instead of --
Code:
./filename.sh
then its environment variables will be set in your current environment.

For more complex situations where you need to execute a new process to create environment variables, then they will not exist after the process terminates. One trick to get around this is to have the process write its output to stdout, then "backtick" or "$(...)" the execution of the script into your current shell.

A simple example -- assume script is named "setup_env.sh" (this is just an example, for something this simple you would just "source" a script.)
Code:
#!/bin/bash
/bin/echo "export aaa=aaa"
/bin/echo "export bbb=bbb"
/bin/echo "export ccc=ccc"
Then execute it with: (note: there are "backticks" around the command. They are not particles of dust on your screen.)
Code:
`./setup_env.sh`
Result:
Code:
bash-2.05b$ echo "$aaa $bbb $ccc"
aaa bbb ccc
This execute line could also be placed into /etc/profile for global usage. Instead of a simple shell script, this could be quite a complex perl script or compiled C program if you wish. Also handy is that during debug, you can execute it without the backticks (or $(...)) and it will just print out what it would do if executed in backticks.
 
  


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
Local vs Global variables wujee Programming 1 03-12-2005 12:43 AM
Setting global variables from a crontab job davee Programming 2 10-06-2003 08:05 AM
global variables in php champ Programming 2 10-13-2002 02:31 PM
exporting global variables neo77777 Programming 6 09-18-2002 02:46 PM
global environmental variables antadam Linux - General 2 10-11-2001 07:36 AM


All times are GMT -5. The time now is 06:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration