environment variable
am 17.01.2007 16:59:25 von ogomezhow can i export a variable from program perl to shell script through environment
variable.
Thank you
--
Open WebMail Project (http://openwebmail.org)
how can i export a variable from program perl to shell script through environment
variable.
Thank you
--
Open WebMail Project (http://openwebmail.org)
Oscar,
Short answer - you cannot (sort of). This is because your shell script
will execute in a sub shell of your perl program.
However, you can do something like this:
# untested
system("export VAR=3Dval; /path/to/your/shell/script.sh");
I think that might work for you.
--
Ron Reidy
Lead DBA
Array BioPharma, Inc.
-----Original Message-----
From: Oscar Gomez [mailto:ogomez@epq.com.co]=20
Sent: Wednesday, January 17, 2007 8:59 AM
To: dbi-users@perl.org
Subject: environment variable
how can i export a variable from program perl to shell script through
environment=20
variable.
Thank you
--
Open WebMail Project (http://openwebmail.org)
This electronic message transmission is a PRIVATE communication which =
contains
information which may be confidential or privileged. The information is =
intended=20
to be for the use of the individual or entity named above. If you are =
not the=20
intended recipient, please be aware that any disclosure, copying, =
distribution=20
or use of the contents of this information is prohibited. Please notify =
the
sender of the delivery error by replying to this message, or notify us =
by
telephone (877-633-2436, ext. 0), and then delete it from your system.
On Wed, Jan 17, 2007 at 8:59AM, Oscar Gomez wrote:
> how can i export a variable from program perl to shell script through
> environment variable.
This is not really a DBI question, so you will have better luck
posting this type of question to perl-monks
(http://www.perlmonks.org/) or similar forum.
If you want to spawn a process from your Perl script and have that
have an altered environment, you can change the environment variable
in your Perl program and then call the program (using system, exec, or
backticks):
$ENV{XYZ} = 1;
system(...) == 0 or die "system call failed";
If you don't want the environment of the Perl script altered, you can
set the environment variable in the command:
system("XYZ=1 && export XYZ && ...") == 0 or die "system call failed";
If you want the Perl program to alter the environment of the process
that executed it (a shell, cron, another script, etc.), that is not
possible.
--
David Dooling
Reidy, Ron wrote:
> Oscar,
>
> Short answer - you cannot (sort of). This is because your shell script
> will execute in a sub shell of your perl program.
>
> However, you can do something like this:
>
> # untested
> system("export VAR=val; /path/to/your/shell/script.sh");
The shell also takes a series of zero or more key-value pairs at the start of any command:
system("VAR=val VAR2=anotherval /path/to/any/program");
This has the same effect as exporting when you're doing it with Perl's system call, but it's good to
keep in mind when you don't want to keep the variable setting or override in your general shell
session. i.e.
LD_PRELOAD=/usr/lib/special_old_library.so /usr/bin/oldprogram
Scott Smith
Genome Sequencing Center
------=_Part_134201_28937484.1169236889819
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
The solution I proposed works - and I tested it.
#!/bin/perl -w
$ENV{MY_ENVIRONMENT_VARIABLE} = "Quixotic Response";
system("env");
However, this is Perl - TMTOWTDI!
If you want to undo the setting after running the shell, either localize
%ENV or delete the new variable.
{
local(%ENV) = %ENV;
$ENV{MY_ENVIRONMENT_VARIABLE} = "Quixotic Response";
system("env");
}
or
#!/bin/perl -w
$ENV{MY_ENVIRONMENT_VARIABLE} = "Quixotic Response";
system("env");
delete $ENV{MY_ENVIRONMENT_VARIABLE};
Ron's solution won't work unless you are running a sane shell (C shell
doesn't like export, a true Bourne shell doesn't like export with the
assignment!).
And, if you're using a sane shell, you don't need the export, you can simply
write:
system("VAR=val /path/to/your/shell/script");
Well, that worked nicely for me with Korn Shell (and would work with Bourne
Shell), but won't work with C shell (again). You can have multiple
environment variables set if you need to:
system("VAR1=val1 VAR2=val2 /usr/bin/env");
And Scott's response arrived before I sent this but after I'd typed it.
Find "Csh Programming Considered Harmful" via Google if you don't understand
why C shell is not a good idea.
On 1/19/07, Reidy, Ron
> Short answer - you cannot (sort of). This is because your shell script
> will execute in a sub shell of your perl program.
>
> However, you can do something like this:
>
> # untested
> system("export VAR=val; /path/to/your/shell/script.sh");
>
> I think that might work for you.
>
> --
> Ron Reidy
> Lead DBA
> Array BioPharma, Inc.
>
> -----Original Message-----
> From: Oscar Gomez [mailto:ogomez@epq.com.co]
> Sent: Wednesday, January 17, 2007 8:59 AM
> To: dbi-users@perl.org
> Subject: environment variable
>
> how can i export a variable from program perl to shell script through
> environment
> variable.
>
> Thank you
>
> --
> Open WebMail Project (http://openwebmail.org)
>
>
> This electronic message transmission is a PRIVATE communication which
> contains
> information which may be confidential or privileged. The information is
> intended
> to be for the use of the individual or entity named above. If you are not
> the
> intended recipient, please be aware that any disclosure, copying,
> distribution
> or use of the contents of this information is prohibited. Please notify
> the
> sender of the delivery error by replying to this message, or notify us by
> telephone (877-633-2436, ext. 0), and then delete it from your system.
>
>
--
Jonathan Leffler
Guardian of DBD::Informix - v2005.02 - http://dbi.perl.org
"I don't suffer from insanity - I enjoy every minute of it."
------=_Part_134201_28937484.1169236889819--
The standard Unix semantics is this: a child process inherits the
environment from its parent. Therefore if you (export and) set an
environment variable in a process and then run a script, it will see
that environment variable.
Furthermore, _Programming Perl, 3rd ed_ by Wall et all says this about
%ENV on p. 661:
Setting a value in %ENV changes the environment for both your process
and child processes launched after the assignment.
I tried this test program and it worked:
$ENV{MCGILLICUDY} =3D 'corncob pipe';
system( qq{echo \$MCGILLICUDY} );
-Will
> -----Original Message-----
> From: Scott Smith [mailto:ssmith@watson.wustl.edu]
> Sent: Friday 19 January 2007 14:53
> To: Reidy, Ron
> Cc: Oscar Gomez; dbi-users@perl.org
> Subject: Re: environment variable
>
>
> Reidy, Ron wrote:
> > Oscar,
> >
> > Short answer - you cannot (sort of). This is because your
> shell script
> > will execute in a sub shell of your perl program.
> >
> > However, you can do something like this:
> >
> > # untested
> > system("export VAR=3Dval; /path/to/your/shell/script.sh");
>
> The shell also takes a series of zero or more key-value pairs
> at the start of any command:
>
> system("VAR=3Dval VAR2=3Danotherval /path/to/any/program");
>
> This has the same effect as exporting when you're doing it
> with Perl's system call, but it's good to
> keep in mind when you don't want to keep the variable setting
> or override in your general shell
> session. i.e.
>
> LD_PRELOAD=3D/usr/lib/special_old_library.so /usr/bin/oldprogram
>
> Scott Smith
> Genome Sequencing Center
>
- - - - - Appended by Scientific Atlanta, a Cisco company - - - - - =
This e-mail and any attachments may contain information which is confiden=
tial,
proprietary, privileged or otherwise protected by law. The information is=
solely
intended for the named addressee (or a person responsible for delivering =
it to
the addressee). If you are not the intended recipient of this message, yo=
u are
not authorized to read, print, retain, copy or disseminate this message o=
r any
part of it. If you have received this e-mail in error, please notify the =
sender
immediately by return e-mail and delete it from your computer.
Once more, there is more than one way to do it ...
Modifying %ENV, perhaps with local, seems to be the cleanest and fastest
way. do { local $ENV{'FOO'}='bar'; local $ENV{'GOD'}='Larry';
system('/usr/local/bin/shellscript.sh','--do-something'); };
Fiddling with the shell in system() looks AT LEAST dangerous to me
(assuming some of the environment values may come from the network).
system "FOO=$bar /usr/local/bin/shellscript.sh --do-something" may look
harmless, but what happens if $bar is "x rm -rf $HOME;" ?
On most Unix-like systems, there is /usr/bin/env, which does not only
dump the environment, but may also modify or reset it before passing
control to another program. Type man env to learn what env can to for
you. Something like
system('/usr/bin/env','-','FOO=bar','GOD=Larry','/usr/local/ bin/shellscript.sh','--do-something')
should work.
On Windows, think about porting the "shell" script to Perl.
Oh, by the way: What is the relation of this question to Perl's Database
Interface?
Alexander
Oscar Gomez wrote:
> how can i export a variable from program perl to shell script through environment
> variable.
>
> Thank you
>
> --
> Open WebMail Project (http://openwebmail.org)
>
--
Alexander Foken
mailto:alexander@foken.de http://www.foken.de/alexander/
On Fri, 19 Jan 2007 13:52:40 -0600, David Dooling wrote:
Hi David
> If you want the Perl program to alter the environment of the
> process that executed it (a shell, cron, another script, etc.),
> that is not possible.
Errr, actually it is possible, at least under Windows.
And that's another good reason to discuss this in another forum, where=
you'll
get expert advice.
--
Cheers
Ron Savage, ron@savage.net.au on 20/01/2007
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company
--eHhjakXzOLJAF9wJ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On 2007-01-19 12:01:29 -0800, Jonathan Leffler wrote:
> system("VAR=3Dval /path/to/your/shell/script");
>=20
> Well, that worked nicely for me with Korn Shell (and would work with Bour=
ne
> Shell), but won't work with C shell (again).
On Unix-like systems the system() function always invokes /bin/sh, not
the user's login shell, so you don't have to worry about that.
hp
--=20
_ | Peter J. Holzer | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR | I'd be programming in Java.
| | | hjp@wsr.ac.at | I don't, and I'm not.
__/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users
--eHhjakXzOLJAF9wJ
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iQDQAwUBRbR921LjemazOuKpAQLBfwXSAn43iHKPOOCa9AzF0sBxR/ff9ATN BQFc
smH9ZDLiPbg5EQ7Veyt+/DClXbuizb3K1PRqBO4fEVmUQmf9J5SFTeQJmr2y Jl3L
Ll6MIu6JUsJs7DVnNRAy9gyBZ/3QQTfjdUtAtT3utKyWX5qY7wile7gWwCwe jZT3
2Ehk6UeaAUHppWX1ftySdX2hdWR3Hdm8fbZ7ysJNR3SEFVkoTkJ6Hh1jijhd KBfO
QD2QvjYP6E2xQIvbC4m0W3S0Bw==
=6ama
-----END PGP SIGNATURE-----
--eHhjakXzOLJAF9wJ--