- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: how to call a function defined in other script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-19-2008 11:03 PM
тАО11-19-2008 11:03 PM
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
Solved! Go to Solution.
- Tags:
- function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-19-2008 11:13 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 12:07 AM
тАО11-20-2008 12:07 AM
Re: how to call a function defined in other script
#!/bin/sh
. ./script2.scr
dd=$(givedate 2008-11-13)
echo "Date us $dd"
Best regards
Fredrik Eriksson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 12:30 AM
тАО11-20-2008 12:30 AM
Re: how to call a function defined in other script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 12:42 AM
тАО11-20-2008 12:42 AM
Re: how to call a function defined in other script
My post should have read
Put ". ./script2.scr" in script1.scr
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 01:19 AM
тАО11-20-2008 01:19 AM
Re: how to call a function defined in other script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 01:34 AM
тАО11-20-2008 01:34 AM
Re: how to call a function defined in other script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 01:38 AM
тАО11-20-2008 01:38 AM
Re: how to call a function defined in other script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 01:42 AM
тАО11-20-2008 01:42 AM
Re: how to call a function defined in other script
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")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 02:05 AM
тАО11-20-2008 02:05 AM
Re: how to call a function defined in other script
Thanks a ton,
Echo worked fro me, let me do some testing on different scenarios, let you know if required any help.