System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help in Shell scripting

 
SOLVED
Go to solution
Whitehorse_1
Frequent Advisor

Need help in Shell scripting

Hi ,

I just born new in Shell scripting., I gota to know, If Im giving "cal 7 1950", it should gimme the number of days in that calender.
Anyways please..

WH..
Reading is a good course medicine for deep sleep !!
11 REPLIES 11
Cem Tugrul
Esteemed Contributor

Re: Need help in Shell scripting

That's correct...you can see July of 1950
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: Need help in Shell scripting

and if you give just "cal 1950"
and you see all month's of day of the year
1950
Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Highlighted
Whitehorse_1
Frequent Advisor

Re: Need help in Shell scripting

Cem, Thxs for reply, I hope I need to clarify my Qn. a bit..

I need a script/command/function(interactive), using which, If I give the "Month and Year" as input, I should just give the output as "The month/Yr which you have typed has got XY days"...I just need the number of days, if I give any month/yr..

WH..
Reading is a good course medicine for deep sleep !!
Cem Tugrul
Esteemed Contributor

Re: Need help in Shell scripting

i may misunderstood..Do you need to manipulate "date" if so here are some examples;
baan01:/#date
Mon Aug 28 10:01:44 EETDST 2006

## Work out what yesterday's date was

if [ "${DAY}" = "01" ]
then
case ${MTH} in
Jan)SEARCH_MTH=Dec;((SEARCH_YEAR=YEAR-1));SEARCH_DAY=31;
;
Feb)SEARCH_MTH=Jan;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
Mar)SEARCH_MTH=Feb;SEARCH_YEAR=${YEAR};SEARCH_DAY=28;;
Apr)SEARCH_MTH=Mar;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
May)SEARCH_MTH=Apr;SEARCH_YEAR=${YEAR};SEARCH_DAY=30;;
Jun)SEARCH_MTH=May;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
Jul)SEARCH_MTH=Jun;SEARCH_YEAR=${YEAR};SEARCH_DAY=30;;
Aug)SEARCH_MTH=Jul;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
Sep)SEARCH_MTH=Aug;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
Oct)SEARCH_MTH=Sep;SEARCH_YEAR=${YEAR};SEARCH_DAY=30;;
Nov)SEARCH_MTH=Oct;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;
Dec)SEARCH_MTH=Nov;SEARCH_YEAR=${YEAR};SEARCH_DAY=30;;
esac
else
SEARCH_MTH=${MTH}
SEARCH_YEAR=${YEAR}
((SEARCH_DAY=DAY-1))
if [ "${SEARCH_DAY}" -lt "10" ]
then
SEARCH_DAY="0${SEARCH_DAY}"
fi
fi

ONE_DAY_BACK="${SEARCH_YEAR}-${SEARCH_MTH}-${SEARCH_DAY}"
TODAY="${SEARCH_YEAR}-${SEARCH_MTH}-${DAY}"

OR;

How to schedule a job on last friday;
## get actual day
DAY=`date +"%d"`

## get the last friday of curren month
EXEC_DAY=`cal | awk 'NF >=6 { last = $6 }; END{ print last }'`

if [ $DAY -ne $EXEC_DAY ]
then
echo " - Not run because is not the last friday - "
exit;
fi

## Here must write your script
## your commands

Our greatest duty in this life is to help others. And please, if you can't
Whitehorse_1
Frequent Advisor

Re: Need help in Shell scripting

Hi Cem,

Jan)SEARCH_MTH=Dec;((SEARCH_YEAR=YEAR-1));SEARCH_DAY=31;;
Feb)SEARCH_MTH=Jan;SEARCH_YEAR=${YEAR};SEARCH_DAY=31;;

In the above sample lines of yours, from wheres the ${YEAR} taken from..?


WH
Reading is a good course medicine for deep sleep !!
Cem Tugrul
Esteemed Contributor

Re: Need help in Shell scripting

oppsss....sorry..
DAY=`date +%d`
MTH=`date +%m`
YEAR=`date +%Y`
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: Need help in Shell scripting


baan01:/#DAY=`date +%d`
baan01:/#echo $DAY
28
baan01:/#MTH=`date +%m`
baan01:/#echo $MTH
08
baan01:/#YEAR=`date +%Y`
baan01:/#echo $YEAR
2006
baan01:/#
Our greatest duty in this life is to help others. And please, if you can't
Joel Girot
Trusted Contributor
Solution

Re: Need help in Shell scripting

hi WH,

is this script appropriate ?

#!/usr/bin/sh
# count days of a month
MYNAME=${0##*/}
if [ $# != 2 ] ; then
echo "usage : $MYNAME month year"
exit 1
fi
cal $1 $2 | tail +3 | wc -w

Joel
Arturo Galbiati
Esteemed Contributor

Re: Need help in Shell scripting

Hi,
you can use:

#/user/bin/ksh
# day_of_month
MM=${1:-$(date +%m)}
YYYY=${2:-$(date +%Y)}
TEMP=$(cal $MM $YYYY)
DAYS=$(echo $TEMP|awk '{print $NF}')
echo "$MM $YYYY has $DAYS"
#eof

day_of_month
08 2006 has 31

day_of_month 7 1950
7 1950 has 31

it runs fine on hp hp-ux 11i. (It take care leap years of course).

HTH,
Art