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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return from function without exiting 1

Status
Not open for further replies.

art15t

MIS
Apr 8, 2002
77
GB
Hi All,

This is my first post, so here goes!

#! /bin/ksh

If I do a rsh within a function then the script terminates as it leaves the function (instead of returning to main and finishing off any other processing). I think this may have something to do with re-direction and simply having the correct syntax when calling the rsh(sub-shell), I'm sure I cracked this one before but can't remember how.

Alternatively, could I call a dummy function just before the function is supposed to return to main and have that function just simply return?

I'm sure other shell programmers must have come across this before, so.......here's hoping!

Adam
 
Here is an example:

#!/bin/sh

rshfunc()
{

exec rsh hostname uname -a
return 0

}


echo "Calling rshfunc() ....."
rshfunc

exit 0


-Tony
 
Tony,

Won't that exec always make the calling process go away as it is overwritten in memory by the thing it's exec'ing? (rsh, in this case) Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Sorry Tony/Mike but I didn't paint a full picture.

The problem occurrs when the function is recursively called from main, more specifically from a while loop, within a nested IF statement.

It also occurrs with a nohup statement, so I think the problem is that main is receiving a SIGTERM from the sub-shell. I've tried trapping for this but the script still exits. Surely it must be possible to spawn a subshell and return to do more processing??

.....so Tony, the exec will definately not work because exec will terminate the current shell and only perform the rsh once
 
A recursively called function that rsh's to another machine -- sounds a bit heavy.

Is there some way you could simplify your script? Or perhaps use another language that's more comfortable with recursion? (Perl/C) Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks for your thoughts on this one Mike but I know this can be overcome with shell. In fact, I have a work-around that calls another function and that function in turn calls the calling function, but.....there is a limitation to the number of times this recursion can occur and the code is a bit harder to follow.


Adam
 
I new it could be done and it was real simple and this has sorted a whole host of problems in previous scripts. Here goes......

If you ever put rsh statements inside complicated nested if and while loops (possibly, these are also called from a function), then all you have to do to stop them exiting, is put a 'sleep 2' immediately after the rsh call, this gives your script time to move on to the next set of commands, otherwise it picks up the exit from the 'rsh' and terminates.

Damn, it was so simple and I spent hours and days trying to debug this. I now have scripts that can loop through a list of hosts, interrogate them and then based on their status perform the necessary tasks.

hope this helps you guys!


art.
 
Art :) Thanks very much, I'll bear that in mind. Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top