- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- pass function as an argument
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
тАО03-30-2006 09:27 AM
тАО03-30-2006 09:27 AM
function f1 {
}
function f2 {
}
function f3 {
for i in $files
do
f1
done
}
function f4 {
for i in $files
do
f2
done
}
as you can tell, f3 and f4 are just the loop, the only difference is the function...
I want to consolidate f3+4 to f5
function f5 {
function = $1
for i in $files
do
function
done
}
in my main program, I can then do
f5 f1
f5 f2
is that possible?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 09:44 AM
тАО03-30-2006 09:44 AM
Solutionfunction f5 {
flag=$1
for i in $files
do
if [ $flag = "f1" ]; then
elif [ $flag = "f2"]; then
fi
done
}
f5 arg
hope it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 09:46 AM
тАО03-30-2006 09:46 AM
Re: pass function as an argument
I'll do a simplifed version:
function f1 {
echo "Function f1"
return 1
} # f1
function f2 {
echo "Function f2"
return 2
} # f2
function f5 {
typeset -i ISTAT=0
func=${1}
shift
${func}
ISTAT=${?}
return ${ISTAT}
} # f5
f5 f1
f5 f2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 09:48 AM
тАО03-30-2006 09:48 AM
Re: pass function as an argument
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 10:02 AM
тАО03-30-2006 10:02 AM
Re: pass function as an argument
but, thank you...I am glad that it can be done!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 10:15 AM
тАО03-30-2006 10:15 AM
Re: pass function as an argument
typeset -i ISTAT=0
#define a variable ISTATE right?
func=${1}
#what does it mean?
shift
${func}
#func is now variable right?
ISTAT=${?}
return ${ISTAT}
} # f5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-30-2006 10:25 AM
тАО03-30-2006 10:25 AM
Re: pass function as an argument
typeset -i ISTAT=0 #declare ISTAT to be an integer (-i) variable and local to this function so that if there is another ISTAT elsewhere it does not interfere with this ISTAT
func=${1} #
and better still:
typeset func=${1}
#what does it mean?
It means that I taking the 1st parameter and turning it into a local variable eventgough it's the name of a function.
shift # clean up the parameter stack
${func}
#func is now variable right? Yes so that
${func} is now "f1" for example.
ISTAT=${?} # get the exit status of the last command and store it in ISTAT
return ${ISTAT} # returns a valid status to the calling routine
} # f5
Much of this (especially typeset) is documented in man sh-posix.