Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Unix Aliases?

by pelagic (Priest)
on Sep 07, 2004 at 10:50 UTC ( [id://388984]=note: print w/replies, xml ) Need Help??


in reply to Unix Aliases?

We had this requirement also and "invented" the following:
sub getenv { my $profile = shift; my @envlines = (`exec ksh -c ". $profile; env"`); foreach (@envlines) { chomp; next unless /=/; my ($var, $value) = split(/=/, $_, 2); $ENV{$var} = $value; } } # END - getenv
This solution is assuming to find the file $profile in the PATH of the caller.

Added some explanation:

Let's say we got a shell script called "my_env" that might look something like:
#!/bin/ksh export ORACLE_SID=testp export NLS_LANG=American_America.WE8ISO8859P1 export ORACLE=/opt/oracle
We would then call our sub like:
getenv ('my_env');
getenv would execute my_env in a shell. The second command "env" would create output like:
ORACLE_SID=testp NLS_LANG=American_America.WE8ISO8859P1 ORACLE=/opt/oracle
These will be written into @envlines and then be set into the running Perl script's environment.

Removed parameter of "my_env" that was actually not used in this example.

pelagic

Replies are listed 'Best First'.
Re^2: Unix Aliases?
by Ronnie (Scribe) on Sep 07, 2004 at 13:02 UTC
    I'm not too familiar with UNIX but I'm guessing that the line containing 'exec' is passing the contents of the .profile into the array emvlines which is then parsed in the foreach loop - (Our sites .profile standards, well don't exist, so we could have several commands on one line including export for example. What's puzzling me here is the variable declarations at the begining. Is the first parameter supplied the name of the .profile file? If so what is the purpose of @my_parms? Our .profile covers all 3 DB's we just type in the relevant name at the UNIX prompt and the appropriate environment is available. Have I misunderstood? Cheers, Ronnie
      I'm guessing that the line containing 'exec' is passing the contents of the .profile into the array emvlines

      Not quite. ksh is launched. First, ksh is told to executed .profile. It's assumed here that .profile is a valid (ksh) shell script with no nasty side effects. Once .profile finishes executed, ksh is told to execute "env", which returns something akin to the following:

      BLOCKSIZE=K CC=cc CFLAGS=-O2 -mcpu=pentiumpro -pipe ... TEST=... LIVE=... TEACHING=... ...

      That list is assigned to @envlines, and then parsed in the foreach loop.

        I understand this now except for the use of the @my_parms variable. It doesn't appear to be used for anything. I thought it may be a way of passing extra environmental variables into this sub. I tried it and though it didn't fail there was no sign of my test variable anywhere. What IS the purpose of this variable? Cheers, Ronnie

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://388984]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found