1842897 Members
2827 Online
110210 Solutions
New Discussion

scritps and functions

 
SOLVED
Go to solution
SM_3
Super Advisor

scritps and functions


scritps and functions

I read that a scripter can use functions in scripts. And that the function does not do anything unless it is called.

I have seen a few scripts one a bourne shell the other a ksh script.
They both have functions within them but the script does not seem to call the function.

e.g here is an excerpt

trimlog()
{
SIZE=`ls -l ${LOGFILE} | awk '{print $5}'`
if [ ${SIZE} -gt ${MONLOGSIZE} ]
then
cp ${LOGFILE} ${LOGDIR}/${PROGRAM}.old
>${LOGFILE}
fi
}
This trimlog function is not called within the script.
So does the function need to be called for the function to execute?
I'm sure it does- just checking.
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor

Re: scritps and functions

Hi,

Yes. You will need to call the function. For ex., try with this

echo_it()
{
MESG=$1
echo "This is my echo: $MESG"
}

#echo_it "This is a message"

You will see the difference if you run the script with the comment before echo_it and without the comment.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Abdul Rahiman
Esteemed Contributor

Re: scritps and functions

I have never seena function being executed without calling it.. though I am not a guru on the subject ..

This excerpt is from a Bourn shell programming web page,

"Line 4 identifies itself as a function declaration by ending in (). This is followed by {, and everything following to the matching } is taken to be the code of that function.
This code is not executed until the function is called. Functions are read in, but basically ignored until they are actually called. "

Here is the link to the actual document, it's a good reading on shell functions..!

http://steve-parker.org/sh/functions.shtml
regds,
Abdul.
No unix, no fun
SM_3
Super Advisor

Re: scritps and functions

thanks
KapilRaj
Honored Contributor

Re: scritps and functions

Yes u need to call it in the script

Kaps
Nothing is impossible
Jeroen Peereboom
Honored Contributor

Re: scritps and functions

L.S.

Indeed a function must be called to get executed.

Sometimes you have several scripts having functions and/or environment variables in common. A way of dealing with this is to have a file, say XXX_functions that contains environment variables and function definitions. This file is 'sourced' by the other shell scripts you write by adding a line near the beginning of these scripts like:
. path_to_script/XXX_functions
(so 'dot space filespec').

This way all variables are set for the calling script and all function definitions are read (but NOT executed).

JP.
Manish Srivastava
Trusted Contributor

Re: scritps and functions

Hi,

The purpose of a function as the name suggests is to perform a job. We have to call it whenever we need to perform the job. No function should execute without it being called (and it dose'nt).

If a function is not being called from the script then you can very well remove it for better readability and maintainablity.

Just grep for the function name in the script and if it is not being called remove it.

manish
Geoff Wild
Honored Contributor
Solution

Re: scritps and functions

Yes - a function needs to be called - otherwise it won't do anything.

On top of that, function can even be in a separate file:

FUNC_FILE=/usr/local/lp/print.functions

Functions are set like:

function print_hp
{
...commands

exit
}


Main part of script (that is executed):

case $P in

chqprt)
chkprt
exit
;;

base1)
base1
exit
;;
*)
print_hp
exit
;;
esac




Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.