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.
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
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:
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.