in reply to Re^2: Obtaining %ENV values via su cmd
in thread Obtaining %ENV values via su cmd

I've had similar situations where I had to "just get it working", and, seeing the variance in environment settings across systems, I just did something like this:
my $username = $ENV{USER} || $ENV{LOGNAME} || `whoami`; chomp $username; # in case it came from the shell command
But I never had to do this in a situation where "su" was being used, and I'd worry about whether one or the other %ENV setting gets "updated" or not across the "su" (with or without the "-" to invoke the new user's rc file(s)).

Having just tried it on freebsd, it looks like "whoami" definitely reports the "effective username" (i.e. the user that you "su" to); maybe the price of running the backtick command is worthwhile, considering the stability you get in return...

Replies are listed 'Best First'.
Re^4: Obtaining %ENV values via su cmd
by chrism01 (Friar) on May 15, 2007 at 04:57 UTC
    The prob I was having was that in the cron/su situation, $ENV{USER} wasn't getting set, although it turns out LOGNAME is. I also came up with about 3 other ways ie parse $ENV{MAIL}, $ENV{HOME} and POSIX::cuserid(). I'll prob use some or all of them similarly to your soln.

    Cheers
    Chris

    Turns out that the prod servers generally have local accts, but my Solaris desktop uses LDAP ...