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!

question about date command on HP UX 1

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi,

I was wondering if there is an equivalent command on HP UX that will produce the same output as RHEL date --date="1 day ago" does?

When I did a man date on a HP UX machine, no --date option was found.

 
I tend to use perl for this as a portable solution when GNU date isn't guaranteed to be available:

Code:
perl -e 'use POSIX;print strftime "%Y%m%d\n",localtime time-86400;'

Annihilannic.
 
Thanks. I'm not too comfortable using Perl, but I'll see if I can rewrite my kshell script as a Perl script. The HP UX version needs to do the same thing (retrieve files with a range of dates) as the RHEL version.

 
No need to rewrite your ksh script in perl if you don't want to, you can just use that perl snippet to obtain the date, i.g.

Code:
#!/usr/bin/ksh

# ...
yesterday=$(perl -e 'use POSIX;print strftime "%Y%m%d\n",localtime time-86400;')

# ...


Annihilannic.
 
Thanks. I don't think the HP UX machine I was working on understood POSIX or strftime, but I'll see what I can do. Also, if I wanted May 15, 2008, the above wouldn't work in such a nice way. I know this post was only inquiring about yesterday, but I am seeking a more flexible command.

 
Well, you would only have to parameterise (is that a word?) the 86400 part... i.e. make it time + 86400*$n where n is the number of days to go forwards in time, and of course backwards in time if it is negative. How much flexibility are you looking for?

Annihilannic.
 
I'm thinking I may need $n to be today's day of year minus the day of year for an arbitrary day of the year. This wouldn't work unless I was staying in the same year. $n would depend on what today is.

I created a ksh script with just your yesterday defined above and I get

bash-2.04$ ./test.ksh
syntax error in file /tmp/perl-ea11693 at line 1, next 2 tokens "use POSIX"
Execution of /tmp/perl-ea11693 aborted due to compilation errors.

I appreciate the help. I just can't make it do what it should even for the simple "yesterday" case.
 
What version of HP-UX and perl do you have? Works fine for me on 11.23 and 11.11 with perls 5.8.0 (from the porting and archive centre) and 5.6.1 (from HP).

Annihilannic.
 
HP version is 11.11 and multiple versions of Perl. Turns out the one being used was version 4.0!! I will make sure the 5.8 version of Perl is being used instead.

Thanks!
 
Hello.

I'm attempting to implement the above code so that I can wget files with a range of dates on an HP Unix system. I was able to successfully use the GNU date on a Linux system.

I'm using some in house code in order to get the modified Julian date and the day of the year.

It seems my script hangs. My concern is that I'm not defining the date variables correctly.

Code:
today=$(${EXECPATH2}mjd - )

${EXECPATH2}mjday $today > ${TEMP}temp1

doy1=$(awk ' {print substr($0,1,3)} ' ${TEMP}temp1)

anl1=$(${EXECPATH2}mjd 2008 01 01 )

${EXECPATH2}mjday $anl1 > ${TEMP}temp2

doy2=$(awk ' {print substr($0,3,1)} ' ${TEMP}temp2)

fcst1=$(${EXECPATH2}mjd 2008 05 15 )

${EXECPATH2}mjday $fcst1 > ${TEMP}temp3

doy3=$(awk ' {print substr($0,1,3)} ' ${TEMP}temp3)

n_anl=$doy1-$doy2

n_fcst=$doy1-$doy3

cd $ANALYSIS

# Define a variable for the analysis files.  Earliest analysis file for 2008
# is from January 1, 2008.

#date1=$(date +%m/%d/%Y --date='December 31, 2007')
date1=$(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d\n",localtime time-86400*$n_anl;')
#date2=$(date +%m/%d/%Y --date='1 day ago')
date2=$(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d\n",localtime time-86400;')

while [[ "$date1" != "$date2" ]]
do
   date1=$(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d\n",localtime time+86400;')
   dates="$dates $(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d\n",localtime time-86400;' $date1)"
done

The mjday code gives a three digit day of year, so I modified the January 1, 2008 variable to assign doy2 to 1, not 001. Is my variable subtraction even possible?

 
Your while loop seems to rely on perl remembering what date you have reached so far across separate invocations of perl. Remember that variables in perl are in a different scope to the variables in your shell script, so for example, $n_anl used when initially setting date1 does not have a value as far as perl is concerned. You will either need to pass it as a parameter to the perl scriptlet, or use fancy quoting tricks (i.e. perl -e '<perl stuff>'$shellvar'<more perl stuff>'), however I'd avoid that if possible as it makes for confusing reading.

Annihilannic.
 
I tried the quote trick since I'm not sure how I'd pass the shell variables to the perl piece. However, I noticed my n_anl and n_fcst variables are wrong

Code:
n_anl=$doy1-$doy2
echo $n_anl

n_fcst=$doy1-$doy3
echo $n_fcst

produces
282-1
282-136

when I want actual subtraction. So, the results I'd like are 281 and 146 in these cases. Any advice how to do that?
I'm learning as I go. Maybe if the shell variables are what I want them to be I can use the while loop correctly.

 
Sorry, didn't pick up on that the first time.

Look up the 'Command Substitution' and 'Arithmetic Evaluation' sections on the man page for your shell. The syntax you're looking for is var=$(( expression )).

Annihilannic.
 
Thanks! Now the arithmetic works :). I've attempted to emulate similar logic used for GNU date to compare the date variables. But my script hangs as usual. Could you look at my code and see if you can spot what I'm doing wrong (besides the messy quote trick in the perl scriptlet)?

Code:
#date1=$(date +%m/%d/%Y --date='December 31, 2007')
date1=$(${EXECPATH}perl -e 'use POSIX;print strftime "+%m/%d/%Y",localtime time-86400*'$n_anl';')
#date2=$(date +%m/%d/%Y --date='1 day ago')
date2=$(${EXECPATH}perl -e 'use POSIX;print strftime "+%m/%d/%Y",localtime time-86400;')

while [[ "$date1" != "$date2" ]]
do
   date1=$(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d",localtime time+86400;')
   dates="$dates $(${EXECPATH}perl -e 'use POSIX;print strftime "%y%m%d",localtime time;' $date1)"
done

 
Rather than calling perl multiple times, I would just use perl code to generate the list of dates, e.g.

Code:
dates=$(perl -e '
        use POSIX;

        $time_t = mktime( 0, 0, 0, 31, 11, 2007-1900 );
        do {
                print strftime "%m/%d/%Y\n",localtime $time_t;
                (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) =
                        localtime($time_t);
                $time_t = mktime( 0, 0, 0, ++$mday, $mon, $year );
        } until ($time_t > (time - 86400))
')

Note that the time structure used by mktime to convert into the $time_t "seconds since the epoch" value uses months from 0 to 11 and years from 1900. You can pass mktime seemingly invalid values (such as the 32nd of December) and it automatically corrects the other values by advancing the month and year as required (so it becomes 1st January the following year).

Annihilannic.
 
Have a look under /opt/sh_utils. It's possible that GNU date has already been installed. If not you could install the sh_utils package. It is available at HP-UX porting and archive sites.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top