| Author |
How To Issue Exit By Sourcing?
|
|
| Big Red 2005-02-12, 5:49 pm |
| 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?
| |
| Janis Papanagnou 2005-02-12, 5:49 pm |
| Big Red wrote:
> 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
| |
| Ed Morton 2005-02-12, 5:49 pm |
|
Big Red wrote:
> 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.
| |
| Big Red 2005-02-12, 5:49 pm |
| 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?
| |
| Big Red 2005-02-12, 5:49 pm |
| Ed,
That's very interesting. I'll put an exit function in "main.sh" to make
a decision. Much thanks.
| |
| Janis Papanagnou 2005-02-12, 5:49 pm |
| Big Red wrote:
> 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.
> Any suggestions?
Though the suggestion was, not to do it this way, but use exit codes
instead.
Janis
| |
| John W. Krahn 2005-02-12, 5:49 pm |
| Big Red wrote:
> I have to bash scripts:
What do you use? I usually use a baseball bat.
John
--
use Perl;
program
fulfillment
| |
| Big Red 2005-02-13, 5:50 pm |
| I didn't catch that "to" typo . Funny...
|
|
|
|