- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell script with date
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2006 01:25 PM
07-18-2006 01:25 PM
#!/bin/sh
day1=`date "+%Y%m%d"`
day2=`date +%w`
if [ $day2 -eq 2 ]; then
sun_day = $day1
else
sun_day=`date +%Y%m%d-$day2|bc`
fi
mon_day=`date +%Y%m%d-$day2-6|bc`
echo $sun_day $mon_day
However what I need is the dates shown in
19-Jul-2006 format
but the command like
echo `date +"%b-""%d-""%Y"`
only can take the date but not a variable like $sun_day
I am a beginner to shell scripting so please render help
Thanks,
Rita
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2006 01:47 PM
07-18-2006 01:47 PM
Solutiontypeset -i PREV_SUN_JD=0
typeset -i MON_B4_JD=0
typeset PREV_SUN_DT=""
typeset MON_B4_DT=""
PREV_SUN_JD=$(caljd.sh -p 1 -x 1 -x 2 -x 3 -x 4 -x 5 -x 6)
PREV_SUN_DT=$(caljd.sh -S "/" ${PREV_SUN_JD})
MON_B4_JD=$((${PREV_SUN_JD} - 6))
MON_B4_DT=$(caljd.sh -S "/" ${MON_B4_DT})
echo "Last Sunday was ${PREV_SUN_DT} \c"
echo "and the Monday before that was \c"
echo "${MON_B4_DT}"
------------------------------------
This will work across month and year boundaries.
Now download the attached caljd.sh; make it executable; and install somewhere in your PATH. Invoke as caljd.sh -u to figure out how this works and for many examples.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2006 01:49 PM
07-18-2006 01:49 PM
Re: Shell script with date
MON_B4_DT=$(caljd.sh -S "/" ${MON_B4_DT})
should obviously be:
MON_B4_DT=$(caljd.sh -S "/" ${MON_B4_JD})
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2006 02:26 PM
07-18-2006 02:26 PM
Re: Shell script with date
The 6th element returned by localtime is the 'day in the week' where Sunday is 0.
So last sunday is now minus $wday
And the monday before that is 6 less still.
Multiply by the number of seconds in a day, subtract from teh current time in seconds and hope that the clock does not strike midnite while doing this...
---- Monday_b4_last_Sunday.pl -----
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
$wday = (localtime)[6];
$b4 = time - 86400 * ( 6 + $wday ) ;
($mday,$mon,$year) = (localtime($b4))[3,4,5];
printf "%02d-%s-%d\n", $mday, $abbr[$mon], $year + 1900;
#perl Monday_b4_last_Sunday.pl
10-Jul-2006
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2006 02:34 PM
07-18-2006 02:34 PM
Re: Shell script with date
use strict;
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my ($now,$b4,$mday,$mon,$year);
$now = time;
$b4 = $now - 86400 * ( 6 + (localtime($now))[6] ) ;
($mday,$mon,$year) = (localtime($b4))[3,4,5];
printf "%02d-%s-%d\n", $mday, $abbr[$mon], $year + 1900;
btw.. I cut & pasted most of this code straight from
http://search.cpan.org/dist/perl/pod/perlfunc.pod
Hein.