Operating System - HP-UX
1830939 Members
1595 Online
110017 Solutions
New Discussion

shell script with date logic

 
SOLVED
Go to solution
vas  bolpali
Advisor

shell script with date logic

I have a script named 'xx.shl' as follows
$cat xx.shl
#!/bin/ksh
x1.shl
x2.shl

now I have to run xx.shl
daily. but I want to skip(not to execute) the x2.shl , if the date
is 25,26, 27 ...upto one day before of the last day.

for example
if the last date is 31, then
i don't want to execute the x2.shl
between 25,26,27,28,29,30.

if the last date is 28, then
i don't want to execute the x2.shl
between 25,26,27.


thanks in advance.
Vasu
keeping you ahead of the learning curve
2 REPLIES 2
Paula J Frazer-Campbell
Honored Contributor

Re: shell script with date logic

Hi

In your script :-

last=`cal | sed '/^$/d' | tail -1 | head -1 | awk '{print $NF}'`

echo $last

this will gice the last day of the month.

So you can use an if statement

if [[ $last != 31]]

then

Paula
If you can spell SysAdmin then you is one - anon
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell script with date logic

Hi Vasu"

You can leverage the 'cal' utility here:

#!/usr/bin/sh
TODAY=`date "+%d"`
LASTD=`cal|awk 'NF > 0 {LASTD=$NF};END{ print LASTD}'`
let LASTD=LASTD-1
if [ "$TODAY" -ge 25 -a "$TODAY" -le "$LASTD" ]
then
echo "skip"
else
echo "run!"
fi
exit 0

Regards!

...JRF...