Solved

Setting an Environment Variable from a shell script

Posted on 2002-04-11
9
317 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:SafeSql
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Accepted Solution

by:
jlevie earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
is the graded answer realy the one which solved your problem?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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 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.:

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now