Bit Twister wrote:
^^^^^^^^^^^
Get a real name.
> On Sun, 15 Jan 2017 20:56:40 -0600, Marek Novotny wrote:
>> I'm confused by this command, type.
>>
>> type ls cat do
>> ls is aliased to `ls --color=auto'
>> cat is /usr/bin/cat
>> do is a shell keyword
>>
>> I'm supposed to be able to identify a function with the *type* command.
>
> Yep, works for me.
Of course, one should use the “-t” option and compare against the string
result, which would be "function" if it were a function. One could
additionally use the “-f” option to ignore built-in functions.
RTFM.
>> But I don't understand this. Is the function a file I can create?
>
> You can but may not be able to run it. :(
Nonsense. A function exists only in memory while the shell is being
executed. You can write a file (shell script) declaring a function and
source that (“source” or “.” command) to import the function into the
current shell context.
>> Do I create a shell script, with functions inside of it, and use type
>> against the file name?
>
> Nope, just use the function name to be checked.
>
>> That doesn't seem to identify functions inside the file.
>
> Hmmm, works for me.
You need to read more carefully.
>> How do I create a situation in which I can see type return a function?
>
> I would guess something like
> #!/bin/bash
>
> function me ()
> {
> echo "from me (type me)"
> }
>
> type do
> me
See above.
The OP’s question was whether it is possible to determine if a file declares
a function. In theory, there is, but without source code parsing only if
you source the file. Then you can check before and after sourcing the file
whether a function of a certain name exists. (But that carries with it the
risk of executing other code if the file does not serve only as a function
library.)
If not before but after, then something in that file or something sourced by
that file declared the function.
If neither before nor after, then the file does not declare the function or
execution does not reach a point where the function is declared.
If before, then a function with that name exists already, and sourcing the
file could only overwrite it.
Most interestingly, if you find that there is no built-in function of a
certain name before, you can declare the function in the same script or
source a file as a polyfill for the builtin, and avoid declaring/sourcing it
if the more efficient builtin appears to be available:
[ "$(type -t foo)" == "function" ] || foo () {
echo 42
}
Likewise for features that are not universally available, e.g.:
type seq >/dev/null 2>&1 || seq () {
# seq polyfill
}
[Careful, though: there are varying implementations of seq; when it doubt,
stick to the standard implementation and invocation. OTOH, the features
that a seq(1) implementation supports can be easily tested with dummy data.]
For this article I learned that one can undeclare a function with
unset -f "$name"
While usually advertized as a Bash feature, it is actually a standard one:
<
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset>
--
PointedEars
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.