Operating System - HP-UX
1847356 Members
4595 Online
110264 Solutions
New Discussion

Re: Script, strange results

 
SOLVED
Go to solution

Script, strange results

I???m trying to convert a list of timestamp values in a file, to a readable and normal date.
I???m using this script:

#!/bin/sh
for TIMESTAMP in $(cat /ser/tmp/timestamps.lst); do
DATE=$(perl -e 'print scalar localtime($TIMESTAMP)')
echo "The timestamp $TIMESTAMP is: $DATE"
done

This is the output:

The timestamp 1046289600 is: Wed Dec 31 18:00:00 1969
The timestamp 1046289300 is: Wed Dec 31 18:00:00 1969
The timestamp 1046289000 is: Wed Dec 31 18:00:00 1969
The timestamp 1046288700 is: Wed Dec 31 18:00:00 1969
The timestamp 1046288400 is: Wed Dec 31 18:00:00 1969
The timestamp 1046288100 is: Wed Dec 31 18:00:00 1969

And so on??? the output is always the same??? but if I run the command manually from the shell, I get the correct value.

Any idea about this behavior?

Thanks in advance for any hint!

Best regards

Rogelio
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: Script, strange results

Have you tried fully qualifying your path to perl in the script?
Carlos Fernandez Riera
Honored Contributor

Re: Script, strange results

Try exporting TIMESTAMP var.


#!/bin/sh
TIMESTAMP=" "
export TIMESTAMP
for TIMESTAMP in $(cat /ser/tmp/timestamps.lst); do
DATE=$(perl -e 'print scalar localtime($TIMESTAMP)')
echo "The timestamp $TIMESTAMP is: $DATE"
done

unsupported
Robin Wakefield
Honored Contributor
Solution

Re: Script, strange results

Hi Rogelio,

You need double-quotes in your perl statement, otherwise the $TIMESTAMP variable is not evaluated.

rgds, Robin

Re: Script, strange results

Thank's Patrick and Carlos.

I tried both options, but the results are the same.

DATE=" "
export DATE
TIMESTAMP=" "
export TIMESTAMP
for TIMESTAMP in $(cat /ser/tmp/timestamps.lst); do
DATE=$(/usr/local/bin/perl -e 'print scalar localtime($TIMESTAMP)')
echo "The timestamp $TIMESTAMP is: $DATE"
done

Re: Script, strange results

Thank you very much Robin, it works perfectly now!

Best regards

Rogelio
Jdamian
Respected Contributor

Re: Script, strange results

T yhink Robin is right.

You must use double quotes instead of single quotes:

DATE=$(perl -e "print scalar localtime($TIMESTAMP)")

I think localtime() parameter is an unsigned integer. Using double quotes the parameter value for localtime() is the string '$TIMESTAMP' ( i.e, dolar capital T, capital I...) not the value of TIMESTAMP variable.

Ramkumar Devanathan
Honored Contributor

Re: Script, strange results

Hi Rogelio,

There is a problem with using a shell variable withing single quotes. enclosing in single quotes offers a performance benefit so that the shell does not pre-evaluate the strings as the shell interpreter should normally.

You may use however the eval command to ensure that the variables get pre-evaluated, in this manner.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#!/bin/sh
for TIMESTAMP in $(cat /ser/tmp/timestamps.lst); do
DATE=$(eval "perl -e 'print scalar localtime($TIMESTAMP)'")
echo "The timestamp $TIMESTAMP is: $DATE"
done

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Just another way....

ramd.


HPE Software Rocks!