I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1)
Now I need put this function in another file (FILE 2) and calling this function from the FILE 1, in another words I want to save all functions in a same file, and calling them from a main file.
PLEASE GUYS HELP ME, I am new in this forum, if I have mistakes with rules I hope to do better my posts, je je je j
-----------------------
get_one_day_before_specified_date()
{ #get the command line input(date month & year)
day=$1
month=$2
year=$3
# if it is the first day of the month
if [ $day -eq 01 ]
then
# if it is the first month of the year
if [ $month -eq 01 ]
then
# make the month as 12
month=12
# deduct the year by one
year=`expr $year - 1`
if [ $year -lt 10 ]
then
year=0$year
fi
else
# deduct the month by one
month=`expr $month - 1`
fi
if [ $month -lt 10 ]
then
month=0$month
fi
# use cal command, discard blank lines,
# take last field of last line,
# first awk command is used to get the
# last useful line of the calendar cmd,
# second awk command is used to get the
# last field of this last useful line,
# NF is no. of fields,
# $NF is value of last field
day=`cal $month $year | awk 'NF != 0{ last = $0 }; END{ print last }' | awk '{ print $NF }'`
else
# deduct the day by one
day=`expr $day - 1`
fi
if [ $day -lt 10 ]
then
day=0$day
fi
fecha_before=$day'-'$month'-'$year
return $fecha_before
}
Do not use a path relative to the current directory or you will not be able to use them if you call the script when in a different directory. Put the function library in a directory in your PATH and use:
tags.
-----------------------
get_one_day_before_specified_date()
{
#get the command line input(date month & year)
day=$1
month=$2
year=$3
# if it is the first day of the month
if [ $day -eq 01 ]
then
# if it is the first month of the year
if [ $month -eq 01 ]
then
# make the month as 12
month=12
# deduct the year by one
year=`expr $year - 1`
[/quote]
[indent]
There is no need to use an external command; the shell has integer arithmetic built in:
year=$(( $year - 1 ))
See above.
It is unwise to use a command whose output is not specified and may differ among implementations.
If you really want to use cal, you don't need two calls to awk: