 |
|
 |
|
|
 |
How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
I believe I can accomplish this by comparing the value of $0 with the
name of the file. $0 appears to be the calling script if a file is
being dotted or the file itself if it is being run directly. But what
if I don't know (or don't want to hardcode) the name of the file that
contains the code being run? Is there a *direct* way of telling in ksh
or bash that code is being run from a dotted file rather than from a
file being executed directly by the interpreter?
In other words, can you distinguish the following two cases, without
using the string "myfile"?
. ./myfile
./myfile
-- John Wiersba
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
2006-01-10, 11:57(-08), jrw32982@yahoo.com:
> I believe I can accomplish this by comparing the value of $0 with the
> name of the file. $0 appears to be the calling script if a file is
> being dotted or the file itself if it is being run directly. But what
> if I don't know (or don't want to hardcode) the name of the file that
> contains the code being run? Is there a *direct* way of telling in ksh
> or bash that code is being run from a dotted file rather than from a
> file being executed directly by the interpreter?
[...]
What if the code is in a function? What do you want to know?
Where that function was defined? Which file the shell was
interpreting when the function was defined? The file the shell
is interpreting at the time the function is called?
a.sh:
. ./b.sh
g
b.sh:
. ./c.sh
f
c.sh:
f() {
g() {
where am I?
}
}
When running a.sh, what to you want "where am I" to return?
What about "eval" statements?
See the BASH_SOURCE and FUNCNAME arrays in bash3.
See print -P %N in zsh.
--
Stéphane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
I have a file/script which defines a couple of functions. If dotted,
the functions can be used within the context of the script which dotted
the functions. But if called directly, I want the file to
automatically call the "main" function defined in that script. In
other words, I have two usage patterns in mind:
. ./myfile
# other stuff here
myfunc 1 2 3 # runs in calling process
or
./myfile 1 2 3 # runs in separate process
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
jrw32982@yahoo.com wrote:
> I believe I can accomplish this by comparing the value of $0 with the
> name of the file. $0 appears to be the calling script if a file is
> being dotted or the file itself if it is being run directly. But what
> if I don't know (or don't want to hardcode) the name of the file that
> contains the code being run? Is there a *direct* way of telling in ksh
> or bash that code is being run from a dotted file rather than from a
> file being executed directly by the interpreter?
>
> In other words, can you distinguish the following two cases, without
> using the string "myfile"?
>
> . ./myfile
> ./myfile
>
bash (2.x) seems to always set $_ to bash when a new shell is invoked.
So this should mostly work:
#!/bin/bash
if [ "${_##*/}" = "bash" ]; then
echo "invoked"
else
echo "included"
fi
--
Michael Tosch @ hp : com
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
2006-01-10, 12:30(-08), jrw32982@yahoo.com:
> I have a file/script which defines a couple of functions. If dotted,
> the functions can be used within the context of the script which dotted
> the functions. But if called directly, I want the file to
> automatically call the "main" function defined in that script. In
> other words, I have two usage patterns in mind:
>
> . ./myfile
> # other stuff here
> myfunc 1 2 3 # runs in calling process
>
> or
>
> ./myfile 1 2 3 # runs in separate process
With bash, you can put at the end of myfile:
eval return 2> /dev/null
myfunc "$@"
"return" will return only from a doted file.
That won't necessarily work with other shells as the behavior of
return in a non-dotted script is /unspecified/.
--
Stéphane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
2006-01-10, 12:30(-08), jrw32982@yahoo.com:
> I have a file/script which defines a couple of functions. If dotted,
> the functions can be used within the context of the script which dotted
> the functions. But if called directly, I want the file to
> automatically call the "main" function defined in that script. In
> other words, I have two usage patterns in mind:
>
> . ./myfile
> # other stuff here
> myfunc 1 2 3 # runs in calling process
>
> or
>
> ./myfile 1 2 3 # runs in separate process
You may start your file with:
#! /bin/sh /path/to/run
And /path/to/run being a file that contains:
if . "$0"; then
shift
myfunc "$@"
fi
--
Stéphane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
jrw32982@yahoo.com wrote:
> I have a file/script which defines a couple of functions. If dotted,
> the functions can be used within the context of the script which dotted
> the functions. But if called directly, I want the file to
> automatically call the "main" function defined in that script. In
> other words, I have two usage patterns in mind:
>
> . ./myfile
> # other stuff here
> myfunc 1 2 3 # runs in calling process
>
> or
>
> ./myfile 1 2 3 # runs in separate process
>
So you would be fine if you could test if a certain function is defined,
without calling that function.
This is possible with aliases, but with functions??
Of course your design is odd.
You should have the functions in the dotted file, not in the calling script.
--
Michael Tosch @ hp : com
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
2006-01-10, 22:54(+01), Michael Tosch:
> jrw32982@yahoo.com wrote:
>
> So you would be fine if you could test if a certain function is defined,
> without calling that function.
[...]
You can use "type", but how would that help?
--
Stéphane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
I don't see how the design is odd. The functions are defined in the
file (function library) being dotted. But in the case where these
functions can be run in a different process, there's no need to dot the
file and then call the function; instead I can just call the file as if
it were a script (which in fact it would be). This will work if I can
detect the difference between dotting a function library and calling a
script.
As an example, say the file were called trace.sh, and it's purpose were
to trace (whatever that means) the calling of a shell function or
external command. Trace.sh would define a function called "do_trace"
which takes an argument of a shell function name or an external command
name. Then I can call:
. ./trace.sh
do_trace myfunction # trace a shell function
do_trace /bin/cat ~/.profile # trace an external command
However, if I don't want to trace shell functions, but only external
commands, then it is simpler to just say:
./trace.sh /bin/cat ~/.profile # trace an external command
That would work if trace.sh called
do_trace "$@"
internally trace.sh were being called directly, but not if it were
being dotted as a function library.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: How to tell if a (script) file is being dotted? |
 |
 |
|
|
01-14-06 03:40 AM
On 10 Jan 2006 15:02:36 -0800, jrw32982@yahoo.com wrote:
> I don't see how the design is odd. The functions are defined in the
> file (function library) being dotted. But in the case where these
> functions can be run in a different process, there's no need to dot the
> file and then call the function; instead I can just call the file as if
> it were a script (which in fact it would be). This will work if I can
> detect the difference between dotting a function library and calling a
> script.
>
> As an example, say the file were called trace.sh, and it's purpose were
> to trace (whatever that means) the calling of a shell function or
> external command. Trace.sh would define a function called "do_trace"
> which takes an argument of a shell function name or an external command
> name. Then I can call:
>
> . ./trace.sh
> do_trace myfunction # trace a shell function
> do_trace /bin/cat ~/.profile # trace an external command
>
> However, if I don't want to trace shell functions, but only external
> commands, then it is simpler to just say:
>
> ./trace.sh /bin/cat ~/.profile # trace an external command
[...]
That's what the autoload and fpath is for in zsh (you may have
something similar in ksh).
the fpath is an array that contains a list of directories where
to find definitions of functions that are elligible for an
autoload. Upon the first call of an autoloaded function, the
corresponding script is loaded and stored in memory as a
function. Those scripts can also be used as scripts:
$ mkdir /tmp/a
$ cat > /tmp/a/f <<EOF
heredoc> #! /bin/zsh
heredoc> echo been there
heredoc> EOF
$ chmod +x /tmp/a/f
$ /tmp/a/f
been there
$ fpath+=(/tmp/a)
$ autoload f
$ which f
f () {
# undefined
builtin autoload -X
}
$ f
been there
$ which f
f () {
echo been there
}
--
Stephane
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 02:59 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|