|
Home > Archive > Unix Shell > February 2005 > Passing parameters back from a function
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Passing parameters back from a function
|
|
|
| Can somebody show me the best way to pass set parameters back from a
function within a shell script. I know the following code should work
(see below) since parm1 and parm2 are global variables.
setit()
{
parm1="hello"
parm2="world"
}
parm1=""
parm2=""
setit parm1 parm2
At this point I would like to see $parm1="Hello"
Lets assume that setit() is in a file called y.sh and the caller is in
a file x.sh (two different files). I know I have to source in y.sh to
make the function available, and doing this will make the use of
"Global Variables" that much harder to track down that why I want to
pass them as a parmeter list and have the values in the list come back
to the caller set.
A more accurate description using "C" code would be as follows:
void
setit(int parm1)
{
*parm1=44;
return;
}
main()
{
int parm1;
setit(&parm1)
}
Am I worried about nothing ? Secondly, can what I want be done with in
KSH.
Thanks in advance to all that answer this post
| |
| Barry Margolin 2005-02-18, 2:48 am |
| In article <1108697901.614706.212320@f14g2000cwb.googlegroups.com>,
"Stu" <beefstu350@hotmail.com> wrote:
> Can somebody show me the best way to pass set parameters back from a
> function within a shell script. I know the following code should work
> (see below) since parm1 and parm2 are global variables.
>
> setit()
> {
> parm1="hello"
> parm2="world"
>
> }
>
> parm1=""
> parm2=""
> setit parm1 parm2
>
> At this point I would like to see $parm1="Hello"
setit ()
{
eval \$$1='"hello"'
eval \$$2='"world"'
}
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
|
|
Barry Margolin wrote:
> In article <1108697901.614706.212320@f14g2000cwb.googlegroups.com>,
> "Stu" <beefstu350@hotmail.com> wrote:
>
a[vbcol=seagreen]
work[vbcol=seagreen]
>
> setit ()
> {
> eval \$$1='"hello"'
> eval \$$2='"world"'
> }
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
I am not to sure I understand amd I missing something. Can you please
explain. Thanks
cat x.sh
#!/bin/ksh
setit()
{
eval \$$1='"hello"'
eval \$$2='"world"'
}
setit parm1 parm2
../x.sh
../x.sh[2]: =hello: not found
../x.sh[3]: =world: not found
| |
| Janis Papanagnou 2005-02-20, 6:19 pm |
| Stu wrote:
> Can somebody show me the best way to pass set parameters back from a
> function within a shell script. I know the following code should work
> (see below) since parm1 and parm2 are global variables.
>
> setit()
> {
> parm1="hello"
> parm2="world"
>
> }
>
> parm1=""
> parm2=""
> setit parm1 parm2
>
> At this point I would like to see $parm1="Hello"
>
> Lets assume that setit() is in a file called y.sh and the caller is in
> a file x.sh (two different files). I know I have to source in y.sh to
> make the function available, and doing this will make the use of
> "Global Variables" that much harder to track down that why I want to
> pass them as a parmeter list and have the values in the list come back
> to the caller set.
>
> A more accurate description using "C" code would be as follows:
>
> void
> setit(int parm1)
> {
> *parm1=44;
> return;
> }
>
> main()
> {
> int parm1;
> setit(&parm1)
> }
>
>
> Am I worried about nothing ? Secondly, can what I want be done with in
> KSH.
>
> Thanks in advance to all that answer this post
>
Closest to your "C" code might be to use name references (if you happen
to use ksh93)...
function setit
{
typeset -n p1=$1
typeset -n p2=$2
p1="hello"
p2="world"
}
x=abc
y=def
setit x y
print - "x=$x y=$y"
Results in:
x=hello y=world
Janis
| |
| Janis Papanagnou 2005-02-20, 6:19 pm |
| Stu wrote:
> Barry Margolin wrote:
>
>
> a
>
>
> work
>
>
>
>
> I am not to sure I understand amd I missing something. Can you please
> explain. Thanks
>
> cat x.sh
> #!/bin/ksh
>
> setit()
> {
> eval \$$1='"hello"'
> eval \$$2='"world"'
Barry meant:
eval $1='"hello"'
eval $2='"world"'
> }
>
> setit parm1 parm2
>
> ./x.sh
> ./x.sh[2]: =hello: not found
> ./x.sh[3]: =world: not found
>
Janis
| |
| Heiner Steven 2005-02-20, 6:19 pm |
| Stu wrote:
> Can somebody show me the best way to pass set parameters back from a
> function within a shell script. I know the following code should work
> (see below) since parm1 and parm2 are global variables.
>
> setit()
> {
> parm1="hello"
> parm2="world"
>
> }
>
> parm1=""
> parm2=""
> setit parm1 parm2
>
> At this point I would like to see $parm1="Hello"
>
> Lets assume that setit() is in a file called y.sh and the caller is in
> a file x.sh (two different files). I know I have to source in y.sh to
> make the function available, and doing this will make the use of
> "Global Variables" that much harder to track down that why I want to
> pass them as a parmeter list and have the values in the list come back
> to the caller set.
>
> A more accurate description using "C" code would be as follows:
>
> void
> setit(int parm1)
int *parm1
> {
> *parm1=44;
> return;
> }
>
> main()
> {
> int parm1;
> setit(&parm1)
> }
>
>
> Am I worried about nothing ? Secondly, can what I want be done with in
> KSH.
You want arguments to be passed by reference. This is not directly
possible in ksh. You can however use call-by-nmae, which lets
you pass the *names* of the variables that should get the return
code. Barry already pointed out that "eval" is needed in this
case. This kind of parameter passing is that useful, that
ksh93 even invented a new syntax for it (typeset -n, see Janis'
posting).
But often you don't need either of them. Functions often are used
in exactly the same way as external commands are, and if
you want to get and process the results of them, you usually use
command substitution, or directly use the output of the
function e.g. in a pipe:
add() { expr "$1" \+ "$2"; } # write result to standard output
result=`add 21 21`
or
add 21 21 | awk '{print "the result is", $0}'
Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner.steven@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
| |
| William Park 2005-02-20, 6:19 pm |
| Stu <beefstu350@hotmail.com> wrote:
> Can somebody show me the best way to pass set parameters back from a
> function within a shell script. I know the following code should work
> (see below) since parm1 and parm2 are global variables.
>
> setit()
> {
> parm1="hello"
> parm2="world"
> }
>
> parm1=""
> parm2=""
> setit parm1 parm2
>
> At this point I would like to see $parm1="Hello"
....
> Am I worried about nothing ? Secondly, can what I want be done with in
> KSH.
You already had 'eval' answer. You can mimick strcat(3) and strcpy(3)
in shell, using
- builtins -- http://freshmeat.net/projects/bashdiff
- shell functions -- http://home.eol.ca/~parkw/index.html#string
Eg.
a=abc; strcat a 123; echo $a --> abc123
a=abc; strcpy a 123; echo $a --> 123
--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
Slackware Linux -- because I can type.
| |
| Barry Margolin 2005-02-22, 5:52 pm |
| In article <1108697901.614706.212320@f14g2000cwb.googlegroups.com>,
"Stu" <beefstu350@hotmail.com> wrote:
> Can somebody show me the best way to pass set parameters back from a
> function within a shell script. I know the following code should work
> (see below) since parm1 and parm2 are global variables.
>
> setit()
> {
> parm1="hello"
> parm2="world"
>
> }
>
> parm1=""
> parm2=""
> setit parm1 parm2
>
> At this point I would like to see $parm1="Hello"
setit ()
{
eval \$$1='"hello"'
eval \$$2='"world"'
}
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|