Search code examples
perldatetimekshaixepoch

ksh epoch to datetime format


I have a script for find the expire date of any user password. Script can find the expire date in seconds (epoch) but cannot convert this to datetime format.

#!/usr/bin/ksh

if (( ${#} < 1 )) ; then
   print "No Arguments"
   exit
fi

lastupdate=`pwdadm -q $1|awk '{print $3;}'`
((lastupdate=$lastupdate+0))
maxagestr=`lsuser -a maxage $1`
maxage=${maxagestr#*=}
let maxageseconds=$maxage*604800
expdateseconds=$(expr "$maxageseconds" + "$lastupdate")
((expdateseconds=$expdateseconds+0))
expdate=`perl -le 'print scalar(localtime($expdateseconds))'`
echo $expdateseconds
echo $expdate

In this script, expdateseconds value is true. If I type the value of expdateseconds as a parameter of localtime() function, the function show the date in datetime format.

But if I type the $expdateseconds variable, the function does not work true and return 01.01.1970 always.

How can I enter a variable as a parameter of localtime() function?


Solution

  • Shell variables are not expanded within single quotes. So in your code, perl is not "seeing" the shell variable, it is instead seeing an uninitialized perl variable whose value defaults to zero. Shell variables are expanded within double quotes, so in this case that's all you need to do:

    expdate=`perl -le "print scalar(localtime($expdateseconds))"`