Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i call function in shell script.

Status
Not open for further replies.

ranjank

IS-IT--Management
May 9, 2003
176
IN
Hi All,

Can i call a function in shell script.If it is possible can anyone explain me with the small example.I'm it confused.Any help in this regard will be highly appreciated.

Regards,
Rony.
 
Yes you can.
That should be explained in your shell man page.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV...thanks for the reply...but if u can explain with some example then it would be gr8.....

Rgds,
Rony.
 
Example of timestamp function and call to it:
myTimeStamp() { date "+%Y-%m-%d %T $*"; }
myTimeStamp Start of process
# some processing
myTimeStamp End of process

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Example of timestamp function and call to it:
myTimeStamp() { date "+%Y-%m-%d %T $*"; }
myTimeStamp Start of process
# some processing
myTimeStamp End of process

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PHV

I' tried but it is giving me the error ShowUsage command not found.The sample of my code--

if [ $1 == "-h" ] ; then

ShowUsage
exit
fi
ShowUsage()
{
echo " The help is here"
}
 
And this ?
ShowUsage()
{
echo " The help is here"
}
if [ $1 == "-h" ] ; then
ShowUsage
exit
fi


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Normally in Bourne Shell and perhaps in most of Korn Shell implementations, functions have to be defined BEFORE all the coding using them.
You can also create a your own "function library" to recall in your script in such way
. /path_to_your_function_library/your_function_library
... blah
.... blah
..... blah

Keep in mind shell functions doesn't return a value in their name, they behave more like the GOSUB in the old BASIC.
Shell functions can modify your script variable or return an output, they can use INPUT parameters like
MyFunction () {
if [ $1 = "goofie" ];then blah blah blah;fi
}
#
# main code
#
you can use some trick to return a value inside a variable you like and whose name can change time by time like:

MyFunction () {
eval ${2}=$1'-mouse'
}
#
# main code
#
MyFunction "mickey" "goofie"

This Function will insert in the variable goofie the value of $1 (mickey) followed by a "-minnie"
As you can see the variable "goofie" is not represented like $goofie, because in that case the shell will set to "mickey-mouse"a variable whose name is contained in $goofie
 
Thanks guys its working now.....

--RK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top