- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scritps and functions
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
Forums
Discussions
Discussions
Discussions
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
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
06-09-2004 09:41 AM
06-09-2004 09:41 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 09:52 AM
06-09-2004 09:52 AM
Re: scritps and functions
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 09:54 AM
06-09-2004 09:54 AM
Re: scritps and functions
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 09:56 AM
06-09-2004 09:56 AM
Re: scritps and functions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 03:41 PM
06-09-2004 03:41 PM
Re: scritps and functions
Kaps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 05:42 PM
06-09-2004 05:42 PM
Re: scritps and functions
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2004 06:32 PM
06-09-2004 06:32 PM
Re: scritps and functions
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2004 01:07 AM
06-10-2004 01:07 AM
SolutionOn 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