Reputation: 171
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?
Upvotes: 0
Views: 1268
Reputation: 252
There is no need to use neither awk nor perl. E.g.:
#!/usr/bin/ksh93
[[ -z $1 ]] && print -u2 "No Arguments" && exit 1
typeset -i LASTUPDATE S
X=( ${ pwdadm -q "$1" ; } )
LASTUPDATE=${X[3]}
X=${ lsuser -a maxage "$1" ; }
S=${X#*=}
(( S *= 604800 ))
(( S += LASTUPDATE ))
printf "$S\n%T\n" "#$S"
Upvotes: 0
Reputation: 95315
As @JeffY said, your problem is the quotes. You can also do it without perl (assuming your date
command is the GNU version):
expdate=`date -d @$expdateseconds`
Although, since you're using ksh - and really, any modern POSIX shell - I recommend that you avoid `...`, which can cause confusing behavior with quoting, and use $(
...)
instead.
expdate=$(date -d @$expdateseconds)
This isn't codereview, but I have a few other tips regarding your script. The usual rule is to send error messages (like "No arguments") to standard error instead of standard out (with print -u2
) and exit with a nonzero value (typically 1) when there's a usage error.
Whenever passing a parameter to a command, like pwadm -q $1
, you run the risk of funny characters messing things up unless you double-quote the parameter: pwadm -q "$1"
.
You have an odd mixture of let
, ((
, and expr
in your arithmetic. I would suggest that you declare all your numeric variables with typeset -i
varname and just use ((
...))
for all arithmetic. Inside ((
...))
, you don't have to worry about globbing messing things up (let a=b*c
will expand into a syntax error if you have a file in the current directory named a=b.c
, for instance; (( a=b*c ))
won't). You also don't need to put dollar signs on the variables (which just makes the shell convert them to a string and then parse their numeric value out again), or add 0 to them just to make sure they're numbers.
Upvotes: 0
Reputation: 2456
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))"`
Upvotes: 1