Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: backtick operator

by AndyZaft (Hermit)
on May 13, 2010 at 21:00 UTC ( [id://839905] : note . print w/replies, xml ) Need Help??

in reply to backtick operator

Any reason you have to use that very awkward ksh script to retrieve 2 text fields from a file? While at first look it should run as you expect it, that construct could be avoided, and by doing it from perl you wouldn't have to rely on result from a backtick operation, that could cause various unforeseen issues due to ENV values and many other problems. I'm thinking here something along the lines of:
open(DBFILE,"~/.passwd"); while($line = <DBFILE>) { if ($line =~ /$dbname(\w+)(\w+)/) { $dbUser = $1; $dbPass = $2; } }
If you must have an outside script because it is used from other processes also, you could tidy that one up too with awk for example. The whole writing a temp file to extract a text field is very un-unix way of doing it.
awk '/^abc01/{ print $2 " " $3; }' ~/.passwd
would give you the same fields as the getDBlogin script. The strange part about your log is that the log won't even show what came back from your backtick operation, at least you should see
by itself.

Replies are listed 'Best First'.
Re^2: backtick operator
by Anonymous Monk on May 13, 2010 at 21:32 UTC
    Yeah, I thought about that and completely agree with you - I was just reusing that ksh script someone else had written - I replaced that with:
    sub dbConnect(){ my ($dbName) = @_; LogStatus "Retrieving $dbName login info"; my ($dbUser, $dbPass) = get_db_login($dbName); LogStatus "User for $dbName is $dbUser"; die "$0: Could not find password for $dbUser" unless(defined($dbPas +s)); my $dbh = DBI->connect("dbi:DB2:$dbName", $dbUser, $dbPass, {AutoCo +mmit=> 0, RaiseError => 1}) || die "Can't connect to $database: $DBI::errstr"; LogStatus "Connected to $dbName as $dbUser"; return $dbh; } sub get_db_login { my ($dbName) = @_; my ($dbUser, $dbPass, $whoami); chomp($whoami = `whoami`); open(PASSWD,"/home/$whoami/.passwd") or die "~/.passwd does not ex +ist"; while(<PASSWD>){ if($_=~/^$dbName\s+(\w+)\s+(\w+)/i){ $dbUser = $1; $dbPass = $2; } } close PASSWD; return $dbUser, $dbPass; }

    Unfortunately, I am still getting the same behavior as I never see the result of:

    LogStatus "User for $dbName is $dbUser";
      Hmm, but it doesn't die when opening the .passwd file? That's weird, time to debug that get_db_login sub line by line right after the open statement. If for some reason you don't get anything from the file then your while loop would never get executed. Btw, you can use $ENV{USER} instead of `whoami`, one less system call :) I assume it is not dying from
      die "$0: Could not find password for $dbUser" unless(defined($dbPass)) +;
      since the $dbPass will be defined as an empty string at that point, right?

        Found the problem - I needed to increment $| to autoflush. I had a long running sql statement after the call to dbConnect that made it seem as if it was hanging.

        I added the following: