niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
How To Issue Exit By Sourcing?
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
Author Message
Big Red
*nix forums beginner


Joined: 12 Feb 2005
Posts: 4

PostPosted: Sun Feb 13, 2005 1:24 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

I didn't catch that "to" typo Smile . Funny...
Back to top
John W. Krahn
*nix forums Guru


Joined: 27 Feb 2005
Posts: 602

PostPosted: Sat Feb 12, 2005 9:40 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Big Red wrote:
Quote:
I have to bash scripts:

What do you use? I usually use a baseball bat.


John
--
use Perl;
program
fulfillment
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Sat Feb 12, 2005 5:33 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Big Red wrote:
Quote:
Janis,

Which file/where are you thinking the "exit $1" should go? If you want
to put it in "functions.sh" in place of "make_calling_code_exit" it
would simply exit out of functions.sh and "Echo three." would echo...

I'm trying to do some fancy schmancy stuff and make functions.sh tell
main.sh to exit without adding any tests in main.sh. So, in main.sh I
want to see something like this:

#!/bin/bash
# These are all functions called from "functions.sh"
call_function_1
call_function_2
determine_if_I_should_exit
call_function_3
# EOF

% cat File1
#!/bin/bash
test1()
{
if [ $1 -eq 1 ]; then
exit $1
fi
}

% cat File2
#!/bin/bash
.. ./File1
echo "Echo one."
test1 2
echo "Echo two."
test1 1
echo "Echo three."

% File2
Echo one.
Echo two.


Quote:
Any suggestions?

Though the suggestion was, not to do it this way, but use exit codes
instead.

Janis
Back to top
Big Red
*nix forums beginner


Joined: 12 Feb 2005
Posts: 4

PostPosted: Sat Feb 12, 2005 4:52 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Ed,

That's very interesting. I'll put an exit function in "main.sh" to make
a decision. Much thanks.
Back to top
Big Red
*nix forums beginner


Joined: 12 Feb 2005
Posts: 4

PostPosted: Sat Feb 12, 2005 4:46 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Janis,

Which file/where are you thinking the "exit $1" should go? If you want
to put it in "functions.sh" in place of "make_calling_code_exit" it
would simply exit out of functions.sh and "Echo three." would echo...

I'm trying to do some fancy schmancy stuff and make functions.sh tell
main.sh to exit without adding any tests in main.sh. So, in main.sh I
want to see something like this:

#!/bin/bash
# These are all functions called from "functions.sh"
call_function_1
call_function_2
determine_if_I_should_exit
call_function_3
# EOF

Any suggestions?
Back to top
Ed Morton
*nix forums Guru


Joined: 20 Feb 2005
Posts: 1073

PostPosted: Sat Feb 12, 2005 4:09 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Big Red wrote:

Quote:
I have to bash scripts:

File one:
#!/bin/bash
#File name: functions.sh
test1()
{
if [ $1 -eq 1 ]; then
make_calling_code_exit
fi
}
#EOF

File two:
#!/bin/bash
#File name: main.sh
. functions.sh
echo "Echo one."
test1 2
echo "Echo two."
test1 1
Echo "Echo three."
#EOF

What can I do to have the test1 function make main.sh exit so that
"Echo three." does not echo?


It would be bad programming style to do that. Having a lower level
entity (function, script, procedure, whatever) decide what a higher
level one should do is caled "inversion of control" and generally leads
to tight coupling and loose cohesion - 2 of the worst sins in software
design. Having a lower-level entity decide that the whole process should
die is one of the worst forms of inversion of control. Instead, have the
subordinate report to it's superior that it has a problem and leave all
decisions about what the process as a whole should do to the highest
level entity.

Ed.
Back to top
Janis Papanagnou
*nix forums Guru


Joined: 24 Feb 2005
Posts: 450

PostPosted: Sat Feb 12, 2005 2:20 pm    Post subject: Re: How To Issue Exit By Sourcing? Reply with quote

Big Red wrote:
Quote:
I have to bash scripts:

File one:
#!/bin/bash
#File name: functions.sh
test1()
{
if [ $1 -eq 1 ]; then
make_calling_code_exit
fi
}
#EOF

File two:
#!/bin/bash
#File name: main.sh
. functions.sh
echo "Echo one."
test1 2
echo "Echo two."
test1 1
Echo "Echo three."
#EOF

What can I do to have the test1 function make main.sh exit so that
"Echo three." does not echo?


Doesn't it work to simply exit, writing

exit $1

instead of "make_calling_code_exit"?

Though my suggestion would be to make the function _return_ an exit
status and not exit directly; the calling script should check for
the status and exit (or pass the exit status further up the calling
hierarchy).

Janis
Back to top
Big Red
*nix forums beginner


Joined: 12 Feb 2005
Posts: 4

PostPosted: Sat Feb 12, 2005 1:56 pm    Post subject: How To Issue Exit By Sourcing? Reply with quote

I have to bash scripts:

File one:
#!/bin/bash
#File name: functions.sh
test1()
{
if [ $1 -eq 1 ]; then
make_calling_code_exit
fi
}
#EOF

File two:
#!/bin/bash
#File name: main.sh
.. functions.sh
echo "Echo one."
test1 2
echo "Echo two."
test1 1
Echo "Echo three."
#EOF

What can I do to have the test1 function make main.sh exit so that
"Echo three." does not echo?
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [8 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 11:40 pm | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts TLS cert issue Nick Smith Postfix 3 Thu Jul 20, 2006 7:40 pm
No new posts Root relay issue Johnson, S Postfix 4 Thu Jul 20, 2006 6:50 pm
No new posts Weird MemoryError issue jedi200581@yahoo.co.uk python 4 Wed Jul 19, 2006 3:58 pm
No new posts Basic IP Tables issue spec networking 3 Wed Jul 19, 2006 3:57 pm
No new posts Solaris 9 (intel) on Vmware 5.5 - X86 issue - Shareef Solaris 1 Wed Jul 19, 2006 2:58 pm

Mortgage | Web Advertising | Web Advertising | Free Credit Report | MPAA
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1967s ][ Queries: 20 (0.1120s) ][ GZIP on - Debug on ]