Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Unix Aliases?

by Ronnie (Scribe)
on Sep 07, 2004 at 10:40 UTC ( #388981=perlquestion: print w/ replies, xml ) Need Help??
Ronnie has asked for the wisdom of the Perl Monks concerning the following question:

Can any of my cellmates answer this small poser - that's the question not me! I have a need to write some Perl code to access some Oracle Database(s). The plural here is the problem. Our UNIX administrator has set the .profile up to include aliases to the other DB's environmental variable - TEST, LIVE & TEACHING. At the UNIX prompt we just type in the alias for the DB to be accessed TEST, LIVE etc and the required PATH's, ORACLE_HOME and any other associated environment details are set up for the DB specified. Is there any way - short of writing a new Perl script to perform the same function - to call TEST/TEACH/LIVE from within a Perl script. I thought I could make a system call to this but had no success. This is probably because TEST/LIVE etc is an alias/function in UNIX and not accessable via Perl or am I talking out of my ......... The following is the code I've been using to try this out -
#!/usr/bin/perl -w # print "\n\t<***** SOR *****>\n" ; # if (exists $ENV{HOME}) { print "\n\tOH :: $ENV{ORACLE_HOME}\n" ; print "\n\tHome :: $ENV{HOME}\n" ; $file = "$ENV{HOME}/.profile" ; print "\n\tFile :: $file\n" ; $result = system "$file" ; print "\n\tResult :: $result\n" ; $result = system "ISWTEST" ; print "\n\tResult :: $result\n" ; "ISWLIVE" or warn "\n\tNay chunce min!!!\n" ; print "\n\tOH :: $ENV{ORACLE_HOME}\n" ; } else { print "\n\tOh no it's nae!\n" ; } # print "\n\t<***** EOR *****>\n" ;
This results in the semi-expected -
<***** SOR *****> OH :: /isw/tpp/oracle/test/8.1.7 Home :: /home/interface File :: /home/interface/.profile Result :: 0 Can't exec "ISWTEST": No such file or directory at xxrc_profile.pl lin +e 12. Result :: -1 OH :: /isw/tpp/oracle/test/8.1.7 <***** EOR *****>
Cheers, Ronnie

edit (broquaint): added <code> tags

Comment on Unix Aliases?
Select or Download Code
Re: Unix Aliases?
by pelagic (Curate) on Sep 07, 2004 at 10:50 UTC
    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
      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.

Re: Unix Aliases?
by Anonymous Monk on Sep 07, 2004 at 11:27 UTC
    Something like:
    #!perl if (@ARGV && $ARGV [0] eq "SECRET") {shift} else {@ARGV = map {s/'/'"'"'/g; "'$_'"} @ARGV; exec qq !bash -ic "ISWTEST; exec $0 SECRET @ARGV"!} ... rest of program ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://388981]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-07-10 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls