Solved

Setting an Environment Variable from a shell script

Posted on 2002-04-11
9
322 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
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.

 

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 200 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

829 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