Operating System - HP-UX
1753516 Members
5078 Online
108795 Solutions
New Discussion юеВ

Re: changing environment variables

 
SOLVED
Go to solution
Jim Mickens
Frequent Advisor

changing environment variables

I need to make it easy for my users to change from one version of Quantum to another. To do this, I have set up a script to change the $QTHOME variable, but when the script exits, the variable has reverted back to the default set up on login. How can I get this change to take?
10 REPLIES 10
Dan Hetzel
Honored Contributor

Re: changing environment variables

Hi,

An environment variable is known in the current script and the child scripts if it's been exported.

If you vant the calling script to maintain the value of a variable exported in a child, you should "source" the child script.

Example:
in csh:
source my_script
in bourne, korn or posix shells
. my_script
(my_script)

This forces reading all variables in the currently executing shell

Cheers

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Pramod_4
Trusted Contributor

Re: changing environment variables

Hi,

What you need to do is put all your environment variables in to a file.

For Eg:-
You have a set of variable for environment A in a file called "enva" and for environment B in a file called "envb"

Upon selection by the user execute the environment file as follows:
. envb # ( remember to put a dot before the command with a space as given here )

Above execution will set the variable in the current session.

Regards,

Pramod
Kofi ARTHIABAH
Honored Contributor

Re: changing environment variables

Jim:

If you want the environment to remain, you have to "dot" the script ie.

# . /path/to/script

what this does is that it reads the script and executes it within the current shell without spawning a new process to execute it.

The parent process (ie. the shell) cannot be modified by a child process - you can only do it by "exec'ing" or "dot"ing the script.
nothing wrong with me that a few lines of code cannot fix!
Rainer_1
Honored Contributor

Re: changing environment variables

you could use the alias function

alias env1="QTHOME="
alias env2="QTHOME="

insert these lines into /etc/profile or $HOME/.profile
Rainer_1
Honored Contributor

Re: changing environment variables

you could use the alias function

alias env1="QTHOME="
alias env2="QTHOME="

insert these lines into /etc/profile or $HOME/.profile

then you be able to change QTHOME with simply entering

env1

or

env2
Jim Mickens
Frequent Advisor

Re: changing environment variables

I've tried all your suggestions, and still can't get it to work.

Some more details:
The $QTHOME variable is set with the setenv command in the logins to /opt/qtime/qt/v5e.5c.
I wanted them to switch to the new version to test, which means they need to set $QTHOME to /opt/qtime/qt/v5.7. If they run changeqver.sh, it asks them which version they want to run, and does a setenv to the appropriate $QTHOME.

I've put the setenv commands into a separate file, and have changed the changeqver.sh to source the appropriate file, but it still doesn't work. Using a . setqver just gives me a permission denied message (permission denied /opt/pd/.)

All users are using the csh.
Kofi ARTHIABAH
Honored Contributor
Solution

Re: changing environment variables

Jim:

its changeqver.sh that you should source

csh# source /path/to/changeqver.sh

I just tried it and it worked...
nothing wrong with me that a few lines of code cannot fix!
Rainer_1
Honored Contributor

Re: changing environment variables

to make it much comfortabe use alias as

alias changeqver "source changeqver.sh"

add this line into /etc/csh.login or $HOME/.login
so the usesr only have to type changeqver and get their correct variable.
Dan Hetzel
Honored Contributor

Re: changing environment variables

Hi Jim,

I said it previously, if you are using csh, you should source the script file containing the variable definitions.

example:
source changeqver.sh

or, as Rainer suggested, use an alias
alias changeqver "source changeqver.sh"

Make sure that in changeqver.sh, all variables are defined using the csh syntax for environment variables, ie 'setenv my_var value'

This has been working for ages, there's no reason for not working in your scripts.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com