- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- using "procedures" into shell scripts
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
10-28-2002 08:53 AM
10-28-2002 08:53 AM
procname () {
command1
command1
...
}
recalled by "procname" into a script
but can I pass any value to these procedures?
like:
procname (arg1, arg2)
{
....
}
If I try I get a syntax error message.
Thank You
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 08:54 AM
10-28-2002 08:54 AM
SolutionYes, you can pass arguments to functions in posix shell - see man sh-posix (there is a whole section on functions near the bottom and arguments to them).
Basically the functions is defined as;
function
and when you call it later you pass arguments like you would a script;
which are passed to the function as $1 $2 $3 and so on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 08:55 AM
10-28-2002 08:55 AM
Re: using "procedures" into shell scripts
proc()
{
echo "${1} is the first param"
}
proc Test
Also, the shift command will work just like it does in the main part of the script/
Todd Lehr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 09:02 AM
10-28-2002 09:02 AM
Re: using "procedures" into shell scripts
e.g.
my_function()
{
echo "This is 1 ${1}"
shift
echo "This is 2 ${2}"
return 0
}
my_function("One",Two")
would result in
"This is 1 One"
"This is 2 Two"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 09:13 AM
10-28-2002 09:13 AM
Re: using "procedures" into shell scripts
In the actual invocation of the function thare are no ()'s.
my_function("One",Two")
should be simply
my_function "One" "Two"
I've got to learn to read over my answers before hitting 'Submit'.
By the way, you can also capture the output of a function as well:
my_result=$(my_funct2 345)
stat=${?}
In this case ${my_result} would contain the stdout
of the function my_funct2 while ${stat} would contain the numerical result set by the 'return' in myfunct2.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 11:16 AM
10-28-2002 11:16 AM
Re: using "procedures" into shell scripts
#!/usr/bin/sh
#
# Functions
#
showit() {
echo $VAR
}
VAR="some value"
showit
VAR="some other value"
showit
man sh-posix for more info, particularly the "Functions" section.
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-28-2002 12:11 PM
10-28-2002 12:11 PM
Re: using "procedures" into shell scripts
You can kludge the passing of parameters by value by using 'typeset' declarations within the scope of the shell function. A side benefit is inline self-documentation of the arguments passed. Try running this:
#!/usr/bin/sh
#
function me
{
X=${1}.updated
echo "fct_me : X=${X}"
}
function me2
{
typeset X=$1
X=${X}.updated
echo "fct_me2: X=${X}"
}
#
X=ORIGINAL.VALUE
me $X
echo "global : X=${X}"
#
X=ORIGINAL.VALUE
me2 $X
echo "global : X=${X}"
#
exit 0
Regards!
...JRF...