Home > Archive > Unix Programming > November 2006 > About Bash Scripts , modularize and var scropes
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 |
About Bash Scripts , modularize and var scropes
|
|
|
| Hi All
I am stilling study bash scripts.
I have question about how to organize scripts as modularize part.
I think all problem focused on var scropes :
Q1
under c , if we want to using function "void foo()" defined in "foo.h"
we just need to #include foo.h ,then foo() can be seen under scrope of all
application.
but how to do that under bash scripts?
for example we have "app.sh" and "file.sh"
If in "app.sh" I want to invoke "file.sh"'s function , how to do that?
Q2
In what scene bash will fork() an other shell to execute command ? and what
about not?
Q3
about c/c++:
In c application , how execute a bash scripts and read it's output to
application directly? by fork()?
Thank you very much!
your key9
| |
|
| > but how to do that under bash scripts?
> for example we have "app.sh" and "file.sh"
> If in "app.sh" I want to invoke "file.sh"'s function , how to do that?
btw If in "file.sh" I have a var keep some resoult
for example
resoult="this is resoult"
how make this know by app.sh?
I 've tried
export $resoult
no lucky :(
| |
| Bit Twister 2006-11-17, 6:59 pm |
| On Fri, 17 Nov 2006 16:54:31 +0800, key9 wrote:
> Hi All
>
> I am stilling study bash scripts.
This might help
http://tldp.org/LDP/abs/html/index.html
>
> I think all problem focused on var scropes :
Then you would look in the shell's documentation about source (.)
documentation.
I find looking at the sytem init scripts (/etc/init.d/network on linux
for your case) helps to see working examples.
the *export* command does not set variables in the parent process.
so your script calls myexport.sh, myexport.sh will not dink with your
current process environment. Now if you you use the source feature, it will.
| |
| Barry Margolin 2006-11-17, 6:59 pm |
| In article <ejjtc8$4bn$1@news.yaako.com>, "key9" <iamkey9@126.com>
wrote:
> Hi All
>
> I am stilling study bash scripts.
>
> I have question about how to organize scripts as modularize part.
>
> I think all problem focused on var scropes :
>
> Q1
> under c , if we want to using function "void foo()" defined in "foo.h"
> we just need to #include foo.h ,then foo() can be seen under scrope of all
> application.
You also need to link with foo.o.
>
> but how to do that under bash scripts?
> for example we have "app.sh" and "file.sh"
> If in "app.sh" I want to invoke "file.sh"'s function , how to do that?
.. file.sh
> Q2
> In what scene bash will fork() an other shell to execute command ? and what
> about not?
Shells fork a subshell when you use (command) syntax, or when a shell
command has its input or output redirected. So
(cd foo)
if [ $var -eq 0 ]; then echo foo; fi | cat
will run in a subshell, but:
cd foo
if [ $var -eq 0 ]; then echo foo; fi
will run in the original shell.
> Q3
> about c/c++:
> In c application , how execute a bash scripts and read it's output to
> application directly? by fork()?
Use popen(). This works with any type of executable, not just bash
scripts.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
|
|
|
|
|