How to setenv in script?

How to setenv in script?

Post by pkq » Sat, 12 Jul 2003 04:32:19



Hello,

I want to setup some environment variables in a script:

#!/bin/sh
echo setenv XYZ xyz > mysource
source mysource

But after the running of the script, the env XYZ has not been setup yet.
The XYZ can be setup if I just type in the commands.

Do you have any idea on how to keep it after the script?

Thanks,
Bo

 
 
 

How to setenv in script?

Post by Chris F.A. Johnso » Sat, 12 Jul 2003 05:36:50



> Hello,

> I want to setup some environment variables in a script:

> #!/bin/sh
> echo setenv XYZ xyz > mysource
> source mysource

> But after the running of the script, the env XYZ has not been setup yet.
> The XYZ can be setup if I just type in the commands.

> Do you have any idea on how to keep it after the script?

    It will not be set anywhere; Bourne-type shells (sh, ksh, bash,
    etc.) do not use setenv. Use:

XYZ=xyz
export XYZ

    Also, among Bourne-type shells, only bash and zsh use the source
    command; the standard method is a period:

. mysource

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

How to setenv in script?

Post by pkq » Sat, 12 Jul 2003 05:52:53


Chris,

sorry, the first line should be :
#!/usr/bin/tcsh

I use tcsh which support setenv.

Thanks



>>Hello,

>>I want to setup some environment variables in a script:

>>#!/bin/sh
>>echo setenv XYZ xyz > mysource
>>source mysource

>>But after the running of the script, the env XYZ has not been setup yet.
>>The XYZ can be setup if I just type in the commands.

>>Do you have any idea on how to keep it after the script?

>    It will not be set anywhere; Bourne-type shells (sh, ksh, bash,
>    etc.) do not use setenv. Use:

>XYZ=xyz
>export XYZ

>    Also, among Bourne-type shells, only bash and zsh use the source
>    command; the standard method is a period:

>. mysource

 
 
 

How to setenv in script?

Post by Barry Margoli » Sat, 12 Jul 2003 06:18:48




>Chris,

>sorry, the first line should be :
>#!/usr/bin/tcsh

>I use tcsh which support setenv.

csh and tcsh are poor shells for scripting.

Anyway, the script should work if it uses tcsh.  But the environment
variable will only be set in the shell that's executing the script.  When
that shell exits, you'll be back to your interactive shell, which isn't
affected.

If you want your interactive shell to see the environment changes, then you
must source the script.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

How to setenv in script?

Post by James T. Denni » Sun, 13 Jul 2003 19:01:44





>>Chris,
>>sorry, the first line should be :
>>#!/usr/bin/tcsh
>>I use tcsh which support setenv.
> csh and tcsh are poor shells for scripting.
> Anyway, the script should work if it uses tcsh.  But the environment
> variable will only be set in the shell that's executing the script.  When
> that shell exits, you'll be back to your interactive shell, which isn't
> affected.
> If you want your interactive shell to see the environment changes, then you
> must source the script.

 Specifically consider this: when you run any external command (any
 command, whether it's a binary or script) you've implicitly created a
 subprocess.  That subprocess has a COPY of your environment, which it can
 modify.  However, changes to a child process' environment don't affect
 the parents.  It's a COPY.

 Only an built-in command (such as 'read', 'set' or 'unset') or operator
 (such as = --- for variable assignments) can modify a shell's own
 environment.

 When you source a file (or eval a string) those changes occur within the
 current process.   Those commands are read and interpreted by the current
 shell process as though you'd typed them.

 Incidentally there is a subtly you should be aware of if you're going to
 learn shell scripting.  consider the following:

        unset bar; echo foo | read bar; echo $bar

 We see a sequence of three commands, unset (internal), echo (could be
 internal or external, depends on the shell), read (must be internal) and
 another echo.  The unset is in the current process.  The echo | read
 is interesting because ANY PIPE CREATES AN IMPLICIT SUBSHELL, and then the
 'echo $bar' -- might be internal or external.  

 The key question here is will $bar be set to foo subprocess created to the
 left of the pipe, or unset (suprocess was created to the right of the
 pipe, and ends/exits at the semicolon.

 (The answer depends on your shell.  Korn shell 88 or later, and zsh do it
 *correctly* --- reading values *into* the current process by creating
 subprocesses to the left of each pipe.  Bash and some other's to it
 "wrong" forcing us to use backticks and subprocess braces and other
 hackery).

 (The problme is easy with just one value and variable; just use the
 command substitution --- backticks; it get's uglier when you want to:

        do_something | read a b c d ; ....

 you need to:

        do_something | { read a b c d ; .... ; }

 or:

        do_something | ( read a b c d ; .... )

 ... to force all processing to occur in the subshell :(

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered