Operating System - HP-UX
1833523 Members
2792 Online
110061 Solutions
New Discussion

Re: return behaves differently in HP than in Sun

 
Nigel McGuinness_1
Occasional Advisor

return behaves differently in HP than in Sun

Can someone please explain how I can get my script to behave the same on HP as on Sun.

Script s1:
========================
#!/usr/bin/ksh
echo about to call s2
. s2
echo have come out of s2 now


Script s2:
========================
#!/usr/bin/ksh
echo inside s2 now
return

When I run on SunOS I get:
===========================
>. s1
about to call s2
inside s2 now
have come out of s2 now
>

When I run on HP-UX I get:
===========================
>. s1
about to call s2
inside s2 now
>

!!! Pleaser note I am using . before I call

Thanks Nigel.
9 REPLIES 9
Patrick Wallek
Honored Contributor

Re: return behaves differently in HP than in Sun

What happens if you take the return out of script2 on HP?
harry d brown jr
Honored Contributor

Re: return behaves differently in HP than in Sun

A "." (dot/period) is NOT a call to a script, its called SOURCING.

If sun behaves differently, then it is wrong, not HP, because this is the NORMAL behavior expected.

If I was at work I'd test it with my sparky-5.

live free or die
harry
Live Free or Die
Nigel McGuinness_1
Occasional Advisor

Re: return behaves differently in HP than in Sun

Patrick,
With the return removed from s2 on HP:
====================================
>. s1
about to call s2
inside s2 now
have come out of s2 now

- so that is what I want, however the
real s2 is a script, which, if I take the return out, it doesn't work because the return
gets out of:

select option in blah blah
do
case reply in
*) return;;
esac
done
A. Clay Stephenson
Acclaimed Contributor

Re: return behaves differently in HP than in Sun

The return on a sourced file is supposed to exit the process. There is only one process because the sourced file is simply a part of the parent process. No child process is spawned.

The HP behavior is actually correct. One NEVER issues an exit or a return from a sourced file for this reason (unless one really wants to exit the foreground process).
If it ain't broke, I can fix that.
Nigel McGuinness_1
Occasional Advisor

Re: return behaves differently in HP than in Sun

Harry,
Thanks for the correct terminology.

The manpage for SunOS says about return:
=========================================
return causes a shell function or '.' script to return to the invoking script. If return is invoked while not in a function or a '.' script, then it is the same as an exit .

.... so at least Sun is doing what it syas it should do

...... I can't find an HP manpage for return
Patrick Wallek
Honored Contributor

Re: return behaves differently in HP than in Sun

Excerpt from the Shell users guide:

************
The return command stops execution of a function and returns to the calling shell script with an exit status of n. If n is not specified, the returning status is that of the last command executed within the function. When return is invoked outside the boundaries of a function, it acts as an exit.
******************

Here is the shell users guide:
http://docs.hp.com/hpux/onlinedocs/B2355-90046/B2355-90046.html

The part about 'return' is in the Chapter 'POSIX and Korn Shell' in the 'Command Referecne section'.
Nigel McGuinness_1
Occasional Advisor

Re: return behaves differently in HP than in Sun

Clay,
Thanks for the xplanantion. So maybe I should take it up at a Sun forum!
harry d brown jr
Honored Contributor

Re: return behaves differently in HP than in Sun

Nigel,

It should be reported to sun as a bug. BTW, what version of Solaris?

live free or die
harry
Live Free or Die
Nigel McGuinness_1
Occasional Advisor

Re: return behaves differently in HP than in Sun

Harry,

I have found this behaviour on
5.5.1
5.6
5.7
5.8

nb - the manpage says
"...return causes a shell function or '.' script to return to the invoking script ..."

But I tried without being source files & get the same !?

>cat s1
#!/usr/bin/ksh
echo about to call s2
s2
echo have come out of s2 now
>cat s2
#!/usr/bin/ksh
echo inside s2 now
return
>s1
about to call s2
inside s2 now
have come out of s2 now
>uname -rv
5.8 Generic_108528-14
>