Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

prompt date string with bash

SOLVED
Go to solution
kazu_1
Occasional Contributor

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.
5 REPLIES
Devesh Pant_1
Esteemed Contributor
Solution

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


A. Clay Stephenson
Acclaimed Contributor

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.
Devesh Pant_1
Esteemed Contributor

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
kazu_1
Occasional Contributor

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

Highlighted
kazu_1
Occasional Contributor

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.