Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing return value of one function to another function 1

Status
Not open for further replies.

KPKIND

Technical User
Sep 8, 2003
130
IN
Dear All,

I am trying to write a script which has 2 functions. I need to pass the final value of function 1 to function 2. Not sure how should I pass the value of one fucntion to another. Any help is very much appreciated.

I have tried putting the below little code, where I want to see the output "Hi" 5 times, I want to pass the value of "a" from func_one to func_two when the func_two should stop.

It could be a very simple one for who know scripting well, but unfortnately I am not one among them

____________________________________________________
#!/usr/bin/ksh

func_one()
{
for j in 1 2 3 4 5
do
a=$j
func_two
done
}

func_two()
{
while test "$a" -le "5"
do
echo "Hi"
done
}

func_one
____________________________________________________


TIA
KPKIND
 
I replaced your while loop with an if statement:

Code:
#!/usr/bin/ksh

func_two()
{
if [[ $1 -le 5 ]]
then
   echo "Hi"
fi
}

func_one()
{
for j in 1 2 3 4 5
do
func_two "$j"
done
}

func_one
 
You wanted this ?
func_one(){
for j in 1 2 3 4 5
do
func_two $j
done
}

func_two(){
while test $1 -le 5
do
echo "Hi"
done
}

func_one

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks both of you for the quick response. Olded your idea has worked like a trick for me. Thanks for that and you deserve a star for that.

PHV, for some reason the code you have given was ending up in a infinite loop...

Anyway thanks a lot.

KPKIND
 
PHV, for some reason the code you have given was ending up in a infinite loop
Sorry for that, I forgot to replace the while loop ...
func_two(){
[ "$1" -le 5 ] && echo "Hi"
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top