| Author |
Message |
Big Red *nix forums beginner
Joined: 12 Feb 2005
Posts: 4
|
Posted: Sun Feb 13, 2005 1:24 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
I didn't catch that "to" typo . Funny... |
|
| Back to top |
|
 |
John W. Krahn *nix forums Guru
Joined: 27 Feb 2005
Posts: 602
|
Posted: Sat Feb 12, 2005 9:40 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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
|
Posted: Sat Feb 12, 2005 5:33 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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.
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
|
Posted: Sat Feb 12, 2005 4:52 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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
|
Posted: Sat Feb 12, 2005 4:46 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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
|
Posted: Sat Feb 12, 2005 4:09 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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
|
Posted: Sat Feb 12, 2005 2:20 pm Post subject:
Re: How To Issue Exit By Sourcing?
|
|
|
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
|
Posted: Sat Feb 12, 2005 1:56 pm Post subject:
How To Issue Exit By Sourcing?
|
|
|
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 |
|
 |
|