Operating System - HP-UX
1834080 Members
2433 Online
110063 Solutions
New Discussion

using "procedures" into shell scripts

 
SOLVED
Go to solution
Mauro Gatti
Valued Contributor

using "procedures" into shell scripts

I know it is possible to use procedures in posix shell scripts like:

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
Ubi maior, minor cessat!
6 REPLIES 6
Stefan Farrelly
Honored Contributor
Solution

Re: using "procedures" into shell scripts


Yes, 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;

A B C
which are passed to the function as $1 $2 $3 and so on.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Todd Lehr
Frequent Advisor

Re: using "procedures" into shell scripts

The parameters will be passed as arguements into the procedure just like the main script, they become localized to the procedure. You also don't need to specify the params on the procedure line

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
A. Clay Stephenson
Acclaimed Contributor

Re: using "procedures" into shell scripts

Rather than procedures you have functions which can be denoted by the keyword 'function' or by simply following an identifier with () BUT there are no formal parameters. The actual parameters are simply positional variables $1,$2,$3 ... that are specific to the function.

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"
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: using "procedures" into shell scripts

Ooops, I'm an idiot.

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.




If it ain't broke, I can fix that.
Darrell Allen
Honored Contributor

Re: using "procedures" into shell scripts

Variables are also shared(usually) between functions and the calling program. Example:

#!/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
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
James R. Ferguson
Acclaimed Contributor

Re: using "procedures" into shell scripts

Hi:

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...