Re: change environment variable via script

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Scott
Date:  
To: Main PLUG discussion list
Subject: Re: change environment variable via script
Alex,

The script itself is a shell session, so as soon as it ends, all
information contained within would be eliminated and not carried over to
your current environment. A solution is to functionalize the script,
like this:

----------------------------------------------------------
cvsswitch() {
echo "Selected server : "$1;

case "$1" in
  one)
    CVSROOT=":ext: 
<mailto:alex@cvs.one.net>:/var/lib/cvs"; export CVSROOT;
  ;;
  two)
    CVSROOT=":ext: 
<mailto:alex@cvs.two.net>:/var/lib/cvs"; export CVSROOT;
  ;;
  *)
    echo '???';
    exit 0;
esac


echo "CVSROOT value at end of script : "$CVSROOT;
exit 0;
}
----------------------------------------------------------

As you can see, the command name remains the same, and you would just
have to write the script *into* your ~/.bash_profile and source it and
it would then work within any environment instead of within its own.
Hope this helps.

-Scott


Alex Dean wrote:

> I'm using bash (on OSX). In ~/.bash_profile, I have a CVSROOT
> variable set so I can connect to 1 CVS server. I occasionally want to
> connect to another cvs server, so I wrote a shell script to set a new
> CVSROOT value for me. The script appears to set the value correctly,
> but it doesn't change my environment settings outside of the script.
> It seems like it's a global vs. local variable issue. (CVSROOT is set
> differently within the context of the script, but is the change is
> forgotten when the script exits.) I don't do shell scripts very
> often, so I'm probably missing something really obvious. How do I
> tell the script I want to change the value 'for real'?
>
> This is the script :
> #!/bin/sh
> echo "Selected server : "$1;
>
> case "$1" in
>   one)
>     CVSROOT=":ext: 
> <mailto:alex@cvs.one.net>:/var/lib/cvs"; export CVSROOT;
>   ;;
>   two)
>     CVSROOT=":ext: 
> <mailto:alex@cvs.two.net>:/var/lib/cvs"; export CVSROOT;
>   ;;
>   *)
>     echo '???';
>     exit 0;
> esac

>
> echo "CVSROOT value at end of script : "$CVSROOT;
> exit 0;
>
> And here's a session which -should- switch from server 'one' to server
> 'two', but it doesn't...
>
> sod:~/scripts alex$ echo $CVSROOT
> :ext: <mailto:alex@cvs.one.net>:/var/lib/cvs
> sod:~/scripts alex$ ./cvsswitch two
> Selected server : 'two'
> CVSROOT value at end of script : ':ext:
> <mailto:alex@cvs.two.net>:/var/lib/cvs'
> sod:~/scripts alex$ echo $CVSROOT
> :ext: <mailto:alex@cvs.one.net>:/var/lib/cvs
> sod:~/scripts alex$
>
> You can see the script set the variable correctly, but the change
> didn't apply outside of the script. Why doesn't this work?!
>
> thanks,
> alex
> .
>
>
>
>------------------------------------------------------------------------
>
>---------------------------------------------------
>PLUG-discuss mailing list -
>To subscribe, unsubscribe, or to change you mail settings:
>http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
>


---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss