Click to See Complete Forum and Search --> : Functions in script


Linux_cat
01-12-2005, 06:19 AM
Hello, I have the following script that I am trying to run copied from a admin site:

DAY of week
#!/bin/ksh

TY=2003
TM=4
TD=25
day_str=$(get_day_string $(get_dow TM TD TY) )
echo $day_str
# end script

However the function get_dow is not included so have I have coped that also: -

# This function returns the day of week where
# 0 = sunday, 1 = monday, ... 6 = saturday
# This algorithm is from Mike Keith's World of Words & Numbers:
# http://users.aol.com/s6sj7gt/mikecal.htm
# arguments: $1 = month, $2 = day, $3 = year in format YYYY
get_dow()
{
# [.] means to truncate downward to the nearest integer
# dayofweek=([23m/9] + d + 4 + y + [z/4] - [z/100] + [z/400] - 2 (if m >= 3)) mod 7
typeset -i DOW
typeset -i z
typeset -i multpr

if [ $1 -lt 3 ]
then # year determination
z=$(($3-1))
else
z=$3
fi
if [ $1 -ge 3 ]
then # set the multiplier
multpr=2
else
multpr=0
fi
DOW=$(( ((23 * $1/9) + $2 + 4 + $3 + ($z/4) - ($z/100) + ($z/400) - $multpr) % 7))

echo $DOW
}

# return a day string
# sunday, monday, .. saturday
get_day_string()
{
case $1 in
0) echo "sunday";;
1) echo "monday";;
2) echo "tuesday";;
3) echo "wednesday";;
4) echo "thursday";;
5) echo "friday";;
6) echo "saturday";;
*) echo "error";;
esac
}

Where would I put the function to get this script to work??.

I thank you for any help that anyone can offer.

evac-q8r
01-12-2005, 09:41 AM
In bash you could simply stick it in .bashrc and start another shell login session. However I see that it uses ksh. So look for a similar file for that shell. Or you can stick it in any file and source that file within the file executing the function. If ksh does sourcing. In other words put the function in a script library file my_functions and put in the file executing the function a line like this. my_functions and it will have access to all of the defined function in my_functions. I hope I answered your question.

EVAC

Linux_cat
01-13-2005, 05:36 AM
Thanks dude u did!!!.