Unix Shell - BASH function: does the definition order of the function matter?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > BASH function: does the definition order of the function matter?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author BASH function: does the definition order of the function matter?
linq936@hotmail.com

2006-02-26, 10:18 am

Hi,
Let's say I have a file called, func.sh. In it I define the following
functions,

function f1() {
.......
}

function f2() {
f1
.......
}

function f3() {
.......
}

You can see that f2() calls f1(), not f3().

Then I have 2 scripts like this,
script_1.sh is
source func.sh
f2()
source script_2.sh

and script_2.sh is
f3()


I run the scripts like this,
source script_1.sh

But when it comes to script_2.sh, it says f3() is undefined.

I did some experiment and find that if in func.sh I move the
definition of f3() before f2(), then everything is ok.

This confuses me. in script_1.sh, I "source func.sh" on the top, how
come it does not work.

Grant

2006-02-26, 10:18 am

On 23 Feb 2006 10:38:10 -0800, linq936@hotmail.com wrote:

> But when it comes to script_2.sh, it says f3() is undefined.


undefined is the clue
>
> I did some experiment and find that if in func.sh I move the
>definition of f3() before f2(), then everything is ok.
>
> This confuses me. in script_1.sh, I "source func.sh" on the top, how
>come it does not work.


just make sure functions are defined before you use them, since the
script is run in a single-pass

Grant.
--
.... The computer scientist, who had listened to all of this said,
"Yes, but where do you think the chaos came from?"
Chris F.A. Johnson

2006-02-26, 10:18 am

On 2006-02-23, linq936@hotmail.com wrote:
> Hi,
> Let's say I have a file called, func.sh. In it I define the following
> functions,
>
> function f1() {
> ......
> }


The POSIX (i.e., portable) syntax for defining a function does not
use the function keyword:

f1() {
: ...
}

> function f2() {
> f1
> ......
> }
>
> function f3() {
> ......
> }
>
> You can see that f2() calls f1(), not f3().
>
> Then I have 2 scripts like this,
> script_1.sh is
> source func.sh


The portable syntax for sourcing a script is:

.. func.sh

> f2()


Are you defining or calling the function? If you are calling it,
you do not use ():

f2

If you are defining it, it is incomplete.

> source script_2.sh
>
> and script_2.sh is
> f3()


See above.

> I run the scripts like this,
> source script_1.sh
>
> But when it comes to script_2.sh, it says f3() is undefined.


A function must be defined before it is called.

> I did some experiment and find that if in func.sh I move the
> definition of f3() before f2(), then everything is ok.
>
> This confuses me. in script_1.sh, I "source func.sh" on the top, how
> come it does not work.


First, correct the errors, then post exactly what your scripts
contain and exactly how you run then, and the exact error messages
produced.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com