Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print scalar localtime 2

Status
Not open for further replies.

phorbiuz

Technical User
Jul 22, 2004
67
GB
I've created quite a simple script shell script to produce an output on when a user last logged in to one of our systems, an IBM p570 running AIX 5.3 TL10, or at least I've produced a script that isn't doing what I want it to...

It goes something like this.

For USER in `cat /etc/passwd | cut -d: -f1`
do
LASTCHANGE1=`grep -p ${USER} /etc/security/passwd | grep lastupdate | awk '{print $3}'`
LASTCHANGE2=`/usr/bin/perl -le 'print scalar localtime ${LASTCHANGE1}'`
done

The problem is this only works if I don't script it. On the command line for example:

grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print $3}'

returns 1252399094

Then using the 2nd part

/usr/bin/perl -le 'print scalar localtime 1252399094'

returns Tue Sep 8 09:38:14 2009

So I know the actual working of it are OK, and it's to do with my scripting. :-( Using the scripting above I get:

LASTCHANGE1=`grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print $3}'`
echo $LASTCHANGE1

returns 1252399094

LASTCHANGE2=`/usr/bin/perl -le 'print scalar localtime ${LASTCHANGE1}'`
echo £LASTCHANGE2

returns Thu Jan 1 00:00:00 1970

Can anyone tell me why setting the time using a variable for this perl function fails, and even better, how to get around it?

Thanks.

 
I think this is what you're looking for:
Code:
$LASTCHANGE1=`grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print $3}'`;
print scalar localtime ${LASTCHANGE1};
 
No, I'm afraid that's not it.

If I copy your suggestion as is I get:

dunsdev:/home/forberd # LASTCHANGE1=`grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print $3}'` ; print scalar localtime ${LASTCHANGE1}
scalar localtime 1252399094
dunsdev:/home/forberd #

And if I presume you missed out the Perl part I get:

dunsdev:/home/forberd # LASTCHANGE1=`grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print $3}'` ; perl -le 'print scalar localtime ${LASTCHANGE1}'
Thu Jan 1 00:00:00 1970
dunsdev:/home/forberd #

Yet if we echo the contents of $LASTCHANGE1 and use the actual number rather than a variable we get:

dunsdev:/home/forberd # echo $LASTCHANGE1
1252399094
dunsdev:/home/forberd # perl -le 'print scalar localtime ${LASTCHANGE1}'
Thu Jan 1 00:00:00 1970
dunsdev:/home/forberd # perl -le 'print scalar localtime 1252399094'
Tue Sep 8 09:38:14 2009
dunsdev:/home/forberd #

Any more suggestions?



 
It would appear I misunderstood - you're trying to use a perl one-liner in your shell?

how about something like:
Code:
perl -e "print scalar localtime(`grep -p forberd /etc/security/passwd | grep lastupdate | awk '{print \$3}'`)"
I don't have a *nix box handy to test it on, but something like that should work.
 
The problem with your original script is that you're trying to access a shell variable from within a perl script. The single-quotes around your perl scriptlet are protecting the $LASTCHANGE1 from being evaluated.

If you simply change it to use double-quotes as follows, it should work:

Code:
    LASTCHANGE2=`/usr/bin/perl -le "print scalar localtime ${LASTCHANGE1}"`

Double quotes do not protect the contents of the string from variable substitution.

Another solution would be:

Code:
grep -p forberd /etc/security/passwd | perl -nwe 'if (/lastupdate = (.*)/) { print((scalar localtime $1) . "\n"); }'

That still makes use of the handy AIX grep -p option... I wish that was more widely available!

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top