Unix Shell - Souring in a file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2006 > Souring in a file





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 Souring in a file
Stu

2006-02-01, 6:03 pm

I have the following

cat a.sh
========
#!/bin/ksh
.. ./c.sh
../b.sh
return 0

cat b.sh
=======
#!/bin/ksh

whence -v t1 >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo "found" > junk.out
else
echo "not found" > junk.out
fi

return 0

cat c.sh
=======
#!/bin/ksh

t1()
{
echo "Hello"
}

return 0

How come the function t1 is not available in b.sh when I run the script
a.sh.

I thought since I am sourcing in the file in the parent process should
it be available in the child process (b.sh).

Can somebody explain.

Thanks

Anthony Greene

2006-02-01, 6:03 pm

you have to explicitly export the variables in c.sh, export VAR='foo', and
for functions you need the function argument. export -f 'funcname'.

exalted sysfault$ cat c.sh
#!/bin/bash

bar() {
echo "foobarman"
}

export -f bar
export VAR='foo'

------------------------

exalted sysfault$ cat b.sh
#!/bin/bash

bar
echo "$VAR"

------------------------

#!/bin/bash

source c.sh

../b.sh
echo "$VAR"

-----------------

exalted sysfault$ ./a.sh
foobarman
foo
exalted sysfault$



On Wed, 01 Feb 2006 05:59:44 -0800, Stu wrote:

> I have the following
>
> cat a.sh
> ========
> #!/bin/ksh
> . ./c.sh
> ./b.sh
> return 0
>
> cat b.sh
> =======
> #!/bin/ksh
>
> whence -v t1 >/dev/null 2>&1
> if [ $? -eq 0 ]
> then
> echo "found" > junk.out
> else
> echo "not found" > junk.out
> fi
>
> return 0
>
> cat c.sh
> =======
> #!/bin/ksh
>
> t1()
> {
> echo "Hello"
> }
>
> return 0
>
> How come the function t1 is not available in b.sh when I run the script
> a.sh.
>
> I thought since I am sourcing in the file in the parent process should
> it be available in the child process (b.sh).
>
> Can somebody explain.
>
> Thanks


--
A wise man knows he knows nothing.

Chris F.A. Johnson

2006-02-01, 6:03 pm

On 2006-02-01, Stu wrote:
> I have the following
>
> cat a.sh
> ========
> #!/bin/ksh
> . ./c.sh
> ./b.sh
> return 0
>
> cat b.sh
> =======
> #!/bin/ksh
>
> whence -v t1 >/dev/null 2>&1
> if [ $? -eq 0 ]
> then
> echo "found" > junk.out
> else
> echo "not found" > junk.out
> fi
>
> return 0
>
> cat c.sh
> =======
> #!/bin/ksh
>
> t1()
> {
> echo "Hello"
> }
>
> return 0
>
> How come the function t1 is not available in b.sh when I run the script
> a.sh.
>
> I thought since I am sourcing in the file in the parent process should
> it be available in the child process (b.sh).


Functions are not exported. In bash, but not ksh, you can export
functions so that child processes will see them:

export -f tl

--
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
Stu

2006-02-01, 6:03 pm

Chris,

Is there any quick way to do something similiar in ksh. If so, can you
provide an example.

Thanks

Bill Marcum

2006-02-01, 6:03 pm

On 1 Feb 2006 05:59:44 -0800, Stu
<beefstu350@hotmail.com> wrote:
> I have the following
>
> cat a.sh
>========
> #!/bin/ksh
> . ./c.sh
> ./b.sh
> return 0
>
> cat b.sh
>=======
> #!/bin/ksh
>
> whence -v t1 >/dev/null 2>&1
> if [ $? -eq 0 ]
> then
> echo "found" > junk.out
> else
> echo "not found" > junk.out
> fi
>
> return 0
>
> cat c.sh
>=======
> #!/bin/ksh
>
> t1()
> {
> echo "Hello"
> }
>
> return 0
>
> How come the function t1 is not available in b.sh when I run the script
> a.sh.
>
> I thought since I am sourcing in the file in the parent process should
> it be available in the child process (b.sh).
>
> Can somebody explain.
>

Functions are not usually exported. You could write ". ./c.sh" in b.sh,
or, since you are using ksh, you could add the directory containing
c.sh to FPATH.


--
<netgod> Feanor: u have no idea of the depth of the stupidty of american law
Chris F.A. Johnson

2006-02-01, 6:03 pm

On 2006-02-01, Stu wrote:
> Chris,
>
> Is there any quick way to do something similiar in ksh. If so, can you
> provide an example.


From the ksh93 man page,
<http://www.cs.princeton.edu/~jlk/ko....html#Functions>

Ordinarily, functions are unset when the shell executes a shell
script. Functions that need to be defined across separate
invocations of the shell should be placed in a directory and the
FPATH variable should contain the name of this directory. They
may also be specified in the ENV file.

--
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
bsh

2006-02-01, 8:52 pm

Chris F.A. Johnson wrote:
> Functions are not exported. In bash, but not ksh, you can export
> functions so that child processes will see them:


This is not necessarily true. ksh88 had typeset -fx to export
function definitions via the environment. Such behavior is
deprecated and removed in [most ports of] ksh93 in favor of
autoloaded functions via the FPATH envvar.

The 1st and 2nd editions of B&Ks "The [New] KornShell
Command And programming Language" both say that arrays
are not exportable; however, this doesn't mean that virtually
every port I have used allows it!

=Brian

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com