Unix Programming - How do I bring in ENV

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2004 > How do I bring in ENV





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How do I bring in ENV
Billy N. Patton

2004-02-11, 11:34 pm

How do I bring in environment variables into a shell?

In a csh I can do a source my_file

Here's an example
file -> init
#!/usr/bin/sh
set -x;
TOOLBOX=some_path;
export TOOLBOX;

file -> go
#!/usr/bin/sh
set -x
printf "This is sh\n";

eval init gs40;

echo "TOOLBOX : '$TOOLBOX'";

exec "perl"<<DO_PERL

use Data::Dumper;

@_ = qw ( This is PERL );

print Dumper(\@_);
print "TOOLBOX = '$ENV{TOOLBOX}'\n";

DO_PERL;

printf "This is sh again\n";
echo "TOOLBOX : '$TOOLBOX'"





WHere the eval init it will eval the file but the TOOLBOX variable does
not exists.

I've tried exec init but the script stops after the exec.

How can I get the variables exported int he init script into the current
environment?


here is from the man page
sh
The exec command specified by the arguments is executed in
place of this shell without creating a new process.
Input/output arguments may appear and, if no other arguments
are given, cause the shell input/output to be modified.

The arguments to the eval built-in are read as input to the
shell and the resulting command(s) executed.

--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-patton@ti.com

Jens.Toerring@physik.fu-berlin.de

2004-02-12, 12:34 am

Billy N. Patton <b-patton@ti.com> wrote:
> How do I bring in environment variables into a shell?


> In a csh I can do a source my_file


> Here's an example
> file -> init
> #!/usr/bin/sh
> set -x;
> TOOLBOX=some_path;
> export TOOLBOX;


export TOOLBOX=some_path

will also do.

> I've tried exec init but the script stops after the exec.


Of course, because the shell gets _replaced_ by the script (running in its
own shell) and when it's done you don't get back into the old shell (which
does not exist anymore).

> How can I get the variables exported int he init script into the current
> environment?


You do the same in (ba)sh as in csh, you just source the 'init' script
instead of running it by

# . init

or, in the long version

# source init
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
Jeff Schwab

2004-02-12, 12:34 am

Jens.Toerring@physik.fu-berlin.de wrote:
> Billy N. Patton <b-patton@ti.com> wrote:
>
>
>
>
>
>
>
> export TOOLBOX=some_path
>
> will also do.


Only in Bash. On most Unices, only the long form is correct. On Linux,
of course, "sh" is really bash, so both forms work.

Jens.Toerring@physik.fu-berlin.de

2004-02-12, 12:34 am

Jeff Schwab <jeffplus@comcast.net> wrote:
> Jens.Toerring@physik.fu-berlin.de wrote:
[color=blue]
> Only in Bash. On most Unices, only the long form is correct. On Linux,
> of course, "sh" is really bash, so both forms work.


Thanks, I probably should try to get a "real" sh, the above works the
bash way even if it's invoked as sh (which I did assume to make bash
more sh-like but which mostly seems to just change the start-up
behaviour as I found out after I now took a closer look).

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
Fletcher Glenn

2004-02-12, 7:36 am



Billy N. Patton wrote:
> How do I bring in environment variables into a shell?
>
> In a csh I can do a source my_file
>
> Here's an example
> file -> init
> #!/usr/bin/sh
> set -x;
> TOOLBOX=some_path;
> export TOOLBOX;
>
> file -> go
> #!/usr/bin/sh
> set -x
> printf "This is sh\n";
>
> eval init gs40;
>
> echo "TOOLBOX : '$TOOLBOX'";
>
> exec "perl"<<DO_PERL
>
> use Data::Dumper;
>
> @_ = qw ( This is PERL );
>
> print Dumper(\@_);
> print "TOOLBOX = '$ENV{TOOLBOX}'\n";
>
> DO_PERL;
>
> printf "This is sh again\n";
> echo "TOOLBOX : '$TOOLBOX'"
>
>
>
>
>
> WHere the eval init it will eval the file but the TOOLBOX variable does
> not exists.
>
> I've tried exec init but the script stops after the exec.
>
> How can I get the variables exported int he init script into the current
> environment?
>
>
> here is from the man page
> sh
> The exec command specified by the arguments is executed in
> place of this shell without creating a new process.
> Input/output arguments may appear and, if no other arguments
> are given, cause the shell input/output to be modified.
>
> The arguments to the eval built-in are read as input to the
> shell and the resulting command(s) executed.
>


In many shells, anything enclosed in single quotes is immune to
variable substitution.

--

Fletcher Glenn

Michael Kerrisk

2004-02-12, 6:34 pm

On Thu, 12 Feb 2004 08:18:15 -0500, Jeff Schwab <jeffplus@comcast.net>
wrote:

>Jens.Toerring@physik.fu-berlin.de wrote:
>
>Only in Bash. On most Unices, only the long form is correct. On Linux,
>of course, "sh" is really bash, so both forms work.


This is simply not true. POSIX specifies both forms, and IIRC
correctly, in early Bourne shells, it was the LONG form that couldn't
be used.

Cheers,

Michael
Chris F.A. Johnson

2004-02-12, 7:35 pm

On Fri, 13 Feb 2004 at 07:38 GMT, Michael Kerrisk wrote:
> On Thu, 12 Feb 2004 08:18:15 -0500, Jeff Schwab wrote:
[snip][color=blue]
>
> This is simply not true. POSIX specifies both forms, and IIRC
> correctly, in early Bourne shells, it was the LONG form that couldn't
> be used.


Which are you calling the LONG form?

TOOLBOX=some_path; export TOOLBOX

export TOOLBOX=some_path

The latter (which looks shorter to me) is specified in POSIX, and
works in ksh (pd, 88 and 93) as well as bash and ash (as in
FreeBSD sh), but not in a Bourne shell.

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

2004-02-15, 6:33 pm

On 13 Feb 2004 08:31:11 GMT, "Chris F.A. Johnson"
<c.fa.johnson@rogers.com> wrote:

>On Fri, 13 Feb 2004 at 07:38 GMT, Michael Kerrisk wrote:
>[snip]
>
> Which are you calling the LONG form?


GOod popint -- I may have completely misunderstood
the previous poster!

By Long, I was thinking of

expot NAME=VALUE

Of course it's clear to me (now) that someone could easily
understand the long form to refer to the other syntax.

>TOOLBOX=some_path; export TOOLBOX
>
>export TOOLBOX=some_path
>
> The latter (which looks shorter to me) is specified in POSIX, and
> works in ksh (pd, 88 and 93) as well as bash and ash (as in
> FreeBSD sh), but not in a Bourne shell.


Agreed. And sorry for the confusion.

Cheers,

Michael
Bill Marcum

2004-02-28, 4:34 pm

On Thu, 12 Feb 2004 06:49:12 -0600, Billy N. Patton
<b-patton@ti.com> wrote:
>
> WHere the eval init it will eval the file but the TOOLBOX variable does
> not exists.
>
> I've tried exec init but the script stops after the exec.
>

Naming a program "init" is almost as bad as "test", except that "init"
will probably only cause trouble if you are root.


--
Incrsease your earoning poswer and gaerner profwessional resspect.
Get the Un1iversity Dewgree you have already earned.
[from the prestigious, non-accredited university of Spam!]
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com