CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How can I read UNIX nvironment varaibles in Perl script?

 



digioleg54
User

Oct 25, 2017, 1:44 PM

Post #1 of 21 (5053 views)
How can I read UNIX nvironment varaibles in Perl script? Can't Post

I have a lot of environment variables in different environment files, where can be something like:

Code
export JAVA_HOME=/usr/jdk/latest


and many other lines, which export variables

How can I read them from Perl script?

Thanks


FishMonger
Veteran / Moderator

Oct 25, 2017, 2:25 PM

Post #2 of 21 (5049 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

The 2 easiest methods are:

1) Write a shell script that sources each of the environment files, then its last statement would call your perl script. Since the perl script is running as a child process of the shell script, it will inherit the environment set in those other files.

2) Define the environment vars in 1 or more perl scripts, then in your main perl script load those files via do or require statements.


BillKSmith
Veteran

Oct 25, 2017, 2:32 PM

Post #3 of 21 (5048 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Perl's special hash %ENV reads and writes the environmental variables.

Your post suggests that you are trying to use a shell script to do the same thing that you previously tried to do in perl. Both FishMonger and I told you that this will not work in any language (including shell script).
Good Luck,
Bill


FishMonger
Veteran / Moderator

Oct 25, 2017, 2:57 PM

Post #4 of 21 (5046 views)
Re: [BillKSmith] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Actually, it will work as long as the script that needs that environment is a child process of the one that set the environment.


BillKSmith
Veteran

Oct 25, 2017, 3:56 PM

Post #5 of 21 (5043 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Here is a pure perl implementation of the child process concept. It is demonstrated under windows 7, but I believe it will run the same under any OS.

Code
C:\Users\Bill\forums\guru>type env.pm 
use strict;
use warnings;
$ENV{JAVA_HOME} = '/usr/jdk/latest';
1

C:\Users\Bill\forums\guru>type Dispatch.pl
use strict;
use warnings;
use lib ".";
require "env.pm";
exec 'perl myApp.pl';


C:\Users\Bill\forums\guru>type myApp.pl
use strict;
use warnings;
print "\nThis is you application speaking\n";
print "JAVA_HOME = $ENV{JAVA_HOME}\n";

C:\Users\Bill\forums\guru>perl Dispatch.pl

C:\Users\Bill\forums\guru>
This is you application speaking
JAVA_HOME = /usr/jdk/latest


The file env.pm defines the environmental variable.

The file dispatch.pl 'require's env.pm to define the variable.
It then executes myApp.pl as a child process.

The file myApp.pl simulates your application. It needs the environmental variable in order to print it. This file expects the variable to already be defined.

Output (the last few lines above) proves that the application can access the variable correctly
Good Luck,
Bill


digioleg54
User

Oct 26, 2017, 6:47 AM

Post #6 of 21 (5038 views)
Re: [BillKSmith] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

I am not sure if it will work, because I already tried to put to %ENV such variables, but OS didn't get all of them. The second, if for each Perl script I will have Dispatch.pl, I have 20 Perl scripts, I must have 20 Dispatch. It is too many


digioleg54
User

Oct 26, 2017, 7:18 AM

Post #7 of 21 (5036 views)
Re: [BillKSmith] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

That is what I did:
perlenv_add.pl

Code
#!/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl -w 

use lib '/export/applications/dte/perl';
use lib '/export/applications/dte/perl-lib';

#Set the Perl environment into %ENV

$ENV{'ORACLE_USER'} = '"dteweb/fiss12web@nyfissad"';
$ENV{'MONITOR_USER'} = 'dtemonitor/monitor567web@nyfisswu';
$ENV{'SQLPLUS'} = '"$ORACLE_HOME/bin/sqlplus -s "';
$ENV{'DTE_WORK_PLACE'} = '$DTE_WORKING_DIR';
$ENV{'SCRIPT_HOME'} = '$DTE_WORK_PLACE/sh';
$ENV{'SQL_HOME'} = '$DTE_WORK_PLACE/sql';
$ENV{'PERL_HOME'} = '$DTE_WORK_PLACE/perl';
$ENV{'CTL_HOME'} = '$DTE_WORK_PLACE/ctls';
$ENV{'PRG_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'EXE_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'REPORT_HOME'} = '$DTE_WORK_PLACE/rpts';
$ENV{'PERLEXE'} = '/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl';
$ENV{'PERLEXE5X'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XI'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XD'} = '"${PERLEXE}"';

exit;

Dispatch.pl

Code
#!/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl -w 
use strict;
use lib '/export/applications/dte/dbax';
use lib '/export/applications/dte/perl';
use lib '/export/applications/dte/perl-lib';
require 'perlenv_add.pl';

system "./myApp.pl";

myApp.pl

Code
#!/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl -w 
use lib '/export/applications/dte/dbax';
use lib '/export/applications/dte/perl';
use lib '/export/applications/dte/perl-lib';
print "\nThis is you application speaking\n";
print "ORACLE_USER = $ENV{ORACLE_USER}\n";

perl ./Dispatch.pl

nothing happened

Thanks


FishMonger
Veteran / Moderator

Oct 26, 2017, 7:32 AM

Post #8 of 21 (5034 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

You don't need the dispatch script.

perlenv_add.pl

Code
#!/usr/bin/perl 

use warnings;
use strict;

$ENV{'ORACLE_USER'} = '"dteweb/fiss12web@nyfissad"';
$ENV{'MONITOR_USER'} = 'dtemonitor/monitor567web@nyfisswu';
$ENV{'SQLPLUS'} = '"$ORACLE_HOME/bin/sqlplus -s "';
$ENV{'DTE_WORK_PLACE'} = '$DTE_WORKING_DIR';
$ENV{'SCRIPT_HOME'} = '$DTE_WORK_PLACE/sh';
$ENV{'SQL_HOME'} = '$DTE_WORK_PLACE/sql';
$ENV{'PERL_HOME'} = '$DTE_WORK_PLACE/perl';
$ENV{'CTL_HOME'} = '$DTE_WORK_PLACE/ctls';
$ENV{'PRG_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'EXE_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'REPORT_HOME'} = '$DTE_WORK_PLACE/rpts';
$ENV{'PERLEXE'} = '/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl';
$ENV{'PERLEXE5X'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XI'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XD'} = '"${PERLEXE}"';


myApp.pl

Code
#!/usr/bin/perl 

use warnings;
use strict;
do 'perlenv_add.pl';

print "\nThis is you application speaking\n";
print "ORACLE_USER = $ENV{ORACLE_USER}\n";


Outputs:

Quote

This is you application speaking
ORACLE_USER = "dteweb/fiss12web@nyfissad"


Be aware that your use of the shell vars in those assignments won't work like that.


(This post was edited by FishMonger on Oct 26, 2017, 7:37 AM)


digioleg54
User

Oct 26, 2017, 8:29 AM

Post #9 of 21 (5029 views)
Re: [FishMonger] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

I didn't understand your last comment, but it is not printing anything


(This post was edited by digioleg54 on Oct 26, 2017, 8:32 AM)


BillKSmith
Veteran

Oct 26, 2017, 8:34 AM

Post #10 of 21 (5026 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

You have several errors. The worst of which is that the perlenv_add file is a script rather than a module. Even if everything works as you expect up to the exit statement, it would terminate everything.

I cannot tell from your post if perlenv_add.pl is in one of the @INC directories.

None of your "use lib" statements appear to be necessary. The only one that I used is necessary, under some versions of perl, to find a module in the current directory.

Your use of 'system' rather than 'exec' would probably work, but exec does exactly what is appropriate.

I had another idea. Perhaps you could ask you system administrator to create a new 'user' just for these twenty applications. He could initialize the environment at login. You would not have to be concerned about it at all.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Oct 26, 2017, 8:36 AM

Post #11 of 21 (5026 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Is perlenv_add.pl in the same directory as myApp.pl? If not, then you'll need to specify the full path in the do statement.


FishMonger
Veteran / Moderator

Oct 26, 2017, 8:56 AM

Post #12 of 21 (5021 views)
Re: [BillKSmith] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Using a module and dispatch script adds additional complications and given digioleg54's level of perl knowledge, IMO it causes more problems than it solves.

At times like this it's best to follow the KISS method like the example I gave. Since it didn't work as expected for digioleg54, it would appear that the files are in different locations so the path will need to be specified.


FishMonger
Veteran / Moderator

Oct 26, 2017, 9:02 AM

Post #13 of 21 (5018 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post


Quote
root@99-91-rkb02:~/test# cat perlenv_add.pl
#!/usr/bin/perl

use warnings;
use strict;

$ENV{'ORACLE_USER'} = '"dteweb/fiss12web@nyfissad"';
$ENV{'MONITOR_USER'} = 'dtemonitor/monitor567web@nyfisswu';
$ENV{'SQLPLUS'} = '"$ORACLE_HOME/bin/sqlplus -s "';
$ENV{'DTE_WORK_PLACE'} = '$DTE_WORKING_DIR';
$ENV{'SCRIPT_HOME'} = '$DTE_WORK_PLACE/sh';
$ENV{'SQL_HOME'} = '$DTE_WORK_PLACE/sql';
$ENV{'PERL_HOME'} = '$DTE_WORK_PLACE/perl';
$ENV{'CTL_HOME'} = '$DTE_WORK_PLACE/ctls';
$ENV{'PRG_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'EXE_HOME'} = '$DTE_WORK_PLACE/bin';
$ENV{'REPORT_HOME'} = '$DTE_WORK_PLACE/rpts';
$ENV{'PERLEXE'} = '/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl';
$ENV{'PERLEXE5X'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XI'} = '"${PERLEXE}"';
$ENV{'PERLEXE5XD'} = '"${PERLEXE}"';

root@99-91-rkb02:~/test# cat myApp.pl
#!/usr/bin/perl

use warnings;
use strict;
do 'perlenv_add.pl';

print "\nThis is you application speaking\n";
print "ORACLE_USER = $ENV{ORACLE_USER}\n";

root@99-91-rkb02:~/test# ./myApp.pl

This is you application speaking
ORACLE_USER = "dteweb/fiss12web@nyfissad"



FishMonger
Veteran / Moderator

Oct 26, 2017, 9:10 AM

Post #14 of 21 (5015 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

If perlenv_add.pl is not in the same directory as myApp.pl, then you could add a use lib statement.


Code
#!/usr/bin/perl 

use warnings;
use strict;
use lib '/path/to/environment-script(s)';
do 'perlenv_add.pl';

print "\nThis is you application speaking\n";
print "ORACLE_USER = $ENV{ORACLE_USER}\n";


Or, just add the path to the do statement.

Code
do '/path/to/perlenv_add.pl';



(This post was edited by FishMonger on Oct 26, 2017, 9:13 AM)


digioleg54
User

Oct 26, 2017, 9:30 AM

Post #15 of 21 (5009 views)
Re: [FishMonger] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

sorry, but you are definitely wrong in my case.

Code
#!/usr/bin/perl 

use warnings;
use strict;
use lib '/export/applications/dte/dbax';
use lib '/export/applications/dte/perl';
use lib '/export/applications/dte/perl-lib';

do '/export/applications/dte/perl/perlenv_add.pl';

print "\nThis is you application speaking\n";
print "ORACLE_USER = $ENV{ORACLE_USER}\n";



Code
-rwxrwxrwx   1 dswblgc  ds          1059 Oct 23 15:58 perlenv_add.pl 
$ pwd
/export/applications/dte/perl
$ perl ./myApp.pl
[dswblgc@apdtewebd1(apdtewebd1):dev]


No result


FishMonger
Veteran / Moderator

Oct 26, 2017, 9:53 AM

Post #16 of 21 (5004 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

How can you say "No result" when your output clearly shows that it did give you a result. It's just not the result you expected which tells me that the script you executed and possibly perlenv_add.pl is not the exact same code as what you posted.


FishMonger
Veteran / Moderator

Oct 26, 2017, 10:03 AM

Post #17 of 21 (5002 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

Please follow the same test procedure as I showed in post #13 and post the output just as I did.


digioleg54
User

Oct 26, 2017, 11:09 AM

Post #18 of 21 (4999 views)
Re: [FishMonger] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

OK, Sorry, I have Perl as we all use :

Code
#!/export/applications/dte/CPAN/32-5.8.9-cc/bin/perl -w


May be this is incorrect, but there are old programs, all like that. Do you know, why?


FishMonger
Veteran / Moderator

Oct 26, 2017, 11:31 AM

Post #19 of 21 (4994 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

The very first line in the script is called the shebang line and specifies the path to the perl interpretor..

When you execute your script via
perl ./myApp.pl
you're telling the system to use the first perl interpretor it finds within the path environment to run your script, which may or may not be the same interpretor specified in the shebang line.


digioleg54
User

Oct 26, 2017, 1:41 PM

Post #20 of 21 (4989 views)
Re: [FishMonger] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

I have only one question. We have for example 20 processes, which will work starting at 6 Am every day. So, before start in each program I have to start env program in order to have it in %ENV and use them?


FishMonger
Veteran / Moderator

Oct 26, 2017, 2:17 PM

Post #21 of 21 (4985 views)
Re: [digioleg54] How can I read UNIX nvironment varaibles in Perl script? [In reply to] Can't Post

If all 20 processes/scripts need that same environment, then all you need to do is to add that one do statement to each of those scripts so they can load the perlenv_add.pl environment script.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives