Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setting an Environment Variable from a shell script

Posted on 2002-04-11
9
Medium Priority
?
330 Views
Last Modified: 2013-12-27
I have been given a good reason why this is impossible but I seem to remmber that I have seen it done some where.  If you know how I can do this let me know otherwise just insult me and be done.

esentially I want to setenv JUNK /ORANGE/Dogs run the script and then echo $JUNK and see the path on the command line.  shell script doesn't matter.

Have tried borne, korn, perl, tcsh, csh.

If I am not missing something can I do what I want with a C or Java executable
0
Comment
Question by:SafeSql
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 6935616
My understanding of what you want is to set an env variable from with a script and have that variable visible in the current shell after executing the script.

That's eaily enough done if the script is correctly executed. For instance if I had a file named gork containing:

spork="dowha diddy"
export spork

I could do:

chaos> . gork
chaos> echo $spork
dowha diddy

In a like manner I could modify the PATH, i.e.,

chaos> cat >gork <<EOF
> PATH=$PATH:$JUNK
; export PATH
> EOF
chaos> JUNK=/ORANGE/Dogs; export JUNK
chaos> . gork
chaos> echo $PATH
/usr/local/bin:/usr.../bin:/ORANGE/Dogs

You could also do this with a C  or program. The important fact to remember is that for the change in the environment to persist after the script exits a sub shell can't be used to execute whatever will be changing the environment. And that's becahuse inheritance is downwards into subshells but not upwards into the parent.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6936148
more specific using jlevie's gork example:

sh# . ./gork
ksh# . ./gork
bash# . ./gork

for csh and tcsh gork must look like:

setenv spork "dowha diddy"

then use it like:

csh% source ./gork
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6936628
Just as an aside, it is possible to do this in Plan 9 through /proc.  I don't remember the exact syntax, but it'd be something akin to this:

echo /ORANGE/Dogs > /proc/$PPID/env/JUNK
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SafeSql
ID: 6936860
Both answers work which is good. Problem is that I am trying to get a "command" that I can give a less than unix savy person so that they can setup a test environment.

For instance

/home/smith>echo $target_system
production
/home/smith>go_developement
YOU ARE NOW SAFE TO RUN
/home/smith>echo $target_system
development

Thus I can maintain the settings by modifing the go_development file.  The gork solution works but I worry the user will forget the ". " or the "source"

As I understand it if go_development is a "shell script" this is not possible because inheritance.  ie. the shell script makes its own subprocess based on the parent.

So I guess the question is would a c or java executable create its own "shell" or does it run in the parent shell? and thus...
0
 
LVL 40

Accepted Solution

by:
jlevie earned 800 total points
ID: 6937094
I'm not aware of any way to do what you want other than the ". cmd" or "source cmd" method. As far as I know the same restriction that applies to executing a shell script applies to a perl, C, Java, etc. program. In each case the process that is fiddling with the environment is a sub process of the current shell and thus the current shell won't see any changes the subprocess makes in its environment.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6937277
assuming go_developement is a shell script, and switch_env is the shell script to be sourced to switch environment, you have following choices:

# csh, tcsh:
# go_developement contains following:
source switch_env

# sh, ksh, bash:
# go_developement contains following:
. switch_env


alternatively you may provide shell aliases instead, like

# csh, tcsh:
alias dev     'source switch_env; go_developement'

# ksh, bash:
alias='. switch_env; go_developement'
0
 

Author Comment

by:SafeSql
ID: 6937603
Both answers work which is good. Problem is that I am trying to get a "command" that I can give a less than unix savy person so that they can setup a test environment.

For instance

/home/smith>echo $target_system
production
/home/smith>go_developement
YOU ARE NOW SAFE TO RUN
/home/smith>echo $target_system
development

Thus I can maintain the settings by modifing the go_development file.  The gork solution works but I worry the user will forget the ". " or the "source"

As I understand it if go_development is a "shell script" this is not possible because inheritance.  ie. the shell script makes its own subprocess based on the parent.

So I guess the question is would a c or java executable create its own "shell" or does it run in the parent shell? and thus...
0
 

Author Comment

by:SafeSql
ID: 6937630
Thanks for the help
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6938514
is the graded answer realy the one which solved your problem?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question