Calling external function in a shell

hi guys, how r u???

please I need you, help me please.

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 :smiley:

-----------------------

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
}

-----------------------

Put them all together into one file, lets say "gathered_functions" and in script1.sh you just source it like

. ./gathered_functions

best somewhere in the beginning of your script.

This will source the functions and script1.sh can use these. Do the same for script2.sh, ... etc.

This is called "sourcing", putting a single dot in front of the file you want to "source".

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:

. gathered_functions

thaaaaaaaaaankss friends

the guys in this forum are wonderfull........ :b:

I tested the two options and both of them are ok

thanks, very nice to meet you

Please put code inside

 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:

: $( cal "$month" "$year" )
day=$_

Ditto.

You don't need those quotes:

fecha_before=$day-$month-$year

You can only return integers (255 or less).

To get yesterday's day, I use a function, _yesterday, which uses functions from my book Shell Scripting Recipes: A Problem-Solution Approach:

_yesterday () 
{ 
    _date2julian "$1" || return 2;
    _julian2date $(( $_DATE2JULIAN - 1 )) || return 2;
    _YESTERDAY=$_JULIAN2DATE
}

The chapter that deals with dates is online at The Dating Game.