Solved

Setting an Environment Variable from a shell script

Posted on 2002-04-11
9
320 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

776 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