prompt date string with bash (250 Views)
Reply
Occasional Contributor
kazu_1
Posts: 5
Registered: ‎04-18-2005
Message 1 of 6 (250 Views)
Accepted Solution

prompt date string with bash

I have stack with making shell programming. I hope somebody will help me!

[objective]
Try to prompt date string that shows the date of 2 month ago from today.

I know how to make it on Solaris and Aix.
eg. dst=`env TZ=JST+1440 date +%Y%m%d`
echo $dst
This will prompt like 20050504

However, it doesn't work on HPUX... Please does sombody give me some advice.

Thank you in advance.
Please use plain text.
Esteemed Contributor
Devesh Pant_1
Posts: 388
Registered: ‎08-06-2004
Message 2 of 6 (250 Views)

Re: prompt date string with bash

Kazu,

here is one way
echo $(sh -c "TZ=$(date +%Z)+1440; export TZ; date '+%m%d%y'")

where 1440 is the number of hours between two months which I have taken from your example above

thanks
Devesh


Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 6 (250 Views)

Re: prompt date string with bash

First of all, the date 2 months ago is a very imprecise designation. For example, what is 2 months before 30-Apr-2005. 30-Feb-2005? I don't think so. If you change your question to something like 60 before today then thats much easier.

Use the attached caljd.sh (or search for caljd.pl; it works just the same).

#!/usr/bin/sh
DT=$(caljd.sh -y -s $(caljd.sh -p 60))
echo "60 days ago was ${DT}"

Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Please use plain text.
Esteemed Contributor
Devesh Pant_1
Posts: 388
Registered: ‎08-06-2004
Message 4 of 6 (250 Views)

Re: prompt date string with bash

Kazu,
I forgot you wanted it in a script, I gave i for the command line
okay here it is

dst=`sh -c "TZ=$(date +%Z)+1440; export TZ; date '+%m%d%y'")`
echo $dst

DP
Please use plain text.
Occasional Contributor
kazu_1
Posts: 5
Registered: ‎04-18-2005
Message 5 of 6 (250 Views)

Re: prompt date string with bash

Hello Devesh and Clay.

Sorry for replying so late. I appliciate what you have given me advice and tips.

Now I have found why I couldn't get the target date. The reason is that the environmental variable TZ was set to "MST7MDT". I changed it to "JST-9", then the command "env TZ=JST+15 date" works!

Sorry for putting such a dum thread... I'm going to close this thread.

Best regards,

kazu

Please use plain text.
Occasional Contributor
kazu_1
Posts: 5
Registered: ‎04-18-2005
Message 6 of 6 (250 Views)

Re: prompt date string with bash

Before:

# env | grep TZ
# TZ=MST7MDT

on this setting, a command "env TZ=JST+15 date" does not provide the correct answer. As above command, my timezone is JST so that TZ variable must change to JST. I changed it to JST,

After:

# env | grep TZ
# TZ=JST-9

then, the command works. The command gets yesterday's date.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation