Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Re: how to call a function defined in other script

 
SOLVED
Go to solution
Highlighted
Frequent Advisor

how to call a function defined in other script

Hi All,

I have script1.scr. I am calling datetime function which is defined in script2.scr and trying to get return value:

cat script1.scr
./script2.scr
echo " Please call datetime function "

dd=datetime("2008-11-13")
echo " date is $dd "
********************************************

cat script2.scr

givedate()
{
echo " welcome to date time fun "
val=$1
dd=`echo "$val" | cut -f3 -d'-'`
return $dd
}
*******************************************

When i call function datetime from script1.scr it gives error that can not find function.

Can some one assists what is correct way to call function get return value?

Thanks
9 REPLIES 9
Highlighted
Respected Contributor
Solution

Re: how to call a function defined in other script

Put ". ./script2.scr" in script2.scr

This will source the functions defined in script2.scr.

You should then be able to used the functions defined in script2.scr within script1.scr

Regards
The Devil is in the detail.
Highlighted
Valued Contributor

Re: how to call a function defined in other script

Just to make it abit clearer.

#!/bin/sh
. ./script2.scr

dd=$(givedate 2008-11-13)
echo "Date us $dd"



Best regards
Fredrik Eriksson
Highlighted
Acclaimed Contributor

Re: how to call a function defined in other script

>Can someone assist what is correct way to call function get return value?

Either you source it as KU suggests. You need to have ". ./script2.scr".

Or you don't use call functions, you execute the whole other script.

Or if you want to have functions in a file with the same name, you can use FPATH to point to a directory that contains the functions.
Highlighted
Respected Contributor

Re: how to call a function defined in other script

Typo

My post should have read

Put ". ./script2.scr" in script1.scr

Cheers
The Devil is in the detail.
Highlighted
Frequent Advisor

Re: how to call a function defined in other script

Hi All,

Many thanks, it is working after importing the path.
Now i am facing probelm in term of returning the code.
I am now passing lone date_time string as argument.
************************************
cat script1.scr
./script2.scr
echo " Please call datetime function "

dd=datetime("2008-09-12:20300000")
echo " date is $dd "



In script2.scr i want to return

cat script2.scr

givedate()
{
echo " welcome to date time fun "
val=$1
--"after formating the val"
dd=$date$Month$$Year_$hour$Min$Sec
return $dd
}


Here dd conatins value like "12012008183000". When i am returning this i am not getting full sting.
I think it is not able to handle such long value. Can we do someting?

Thanks
Highlighted
Honored Contributor

Re: how to call a function defined in other script

Hi,

Functions either use the syntax
function FUNCTION { COMMANDS; }
or
FUNCTION () { COMMANDS; }

See the example:

pathmunge () {
if ! echo $PATH | /bin/egrep â q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# Path manipulation
if [ `id â u` = 0 ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
fi
pathmunge /usr/X11R6/bin after
unset pathmunge

Suraj
Highlighted
Valued Contributor

Re: how to call a function defined in other script

Diwakar,

Since you're executing this within a variable (<3 that btw... DCL doesn't have that) you could just aswell use echo instead of return. In my experience this usually works better then using return.

Best regards
Fredrik Eriksson
Highlighted
Acclaimed Contributor

Re: how to call a function defined in other script

>I think it is not able to handle such long value. Can we do something?

Functions can only return numbers 0..255.
Otherwise you would need to use echo:

In function:
echo "$val" | cut -f3 -d'-'

Caller:
dd=$(datetime "2008-11-13")

Highlighted
Frequent Advisor

Re: how to call a function defined in other script

Fredrik/Dennis,

Thanks a ton,

Echo worked fro me, let me do some testing on different scenarios, let you know if required any help.